Standard supervised training
Train a compiled model on in-memory arrays or tensors over multiple epochs.
TensorFlow · Module
Trains a compiled Keras model for a chosen number of epochs using arrays, tensors, or dataset-like inputs. It supports batching, validation, weighting, callbacks, shuffling, and resuming training.
After a model is compiled with an optimizer, loss, and optional metrics, fit divides suitable in-memory data into batches and repeatedly processes the dataset for the requested epochs. It can also consume dataset-like inputs, run validation, apply weights, invoke callbacks, and resume from a nonzero initial epoch.
fit(x=None, y=None, batch_size=None, epochs=1, verbose='auto', callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_batch_size=None, validation_freq=1)| Name | Type | Description |
|---|---|---|
x | array-like, tensor, or dataset-like | Input data: array-like objects, tensors, dataset-like iterables, or other supported input structures. |
y | array-like or tensor, optional | Target data corresponding to x when targets are not included in x. |
batch_size | int or None | Samples per gradient update; when omitted, the default is 32 for batchable array-like input. |
epochs | int | Number of complete passes over the training data. |
verbose | "auto", 0, 1, or 2 | Verbosity mode for progress reporting. |
callbacks | list or None | Callbacks invoked during training. |
validation_split | float | Fraction of array-like training data reserved for validation. |
validation_data | tuple or dataset-like, optional | Data used for validation instead of validation_split. |
shuffle | bool | Whether to shuffle training data before each epoch when supported. |
class_weight | dict or None | Class weights used to rebalance loss contributions. |
sample_weight | array-like or None | Per-sample or per-timestep loss weights. |
initial_epoch | int | Epoch index from which to resume. |
A History object containing the recorded training and validation metrics for each epoch.
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dense(3, activation="softmax")
])
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
x = tf.random.normal((32, 8))
y = tf.random.uniform((32,), maxval=3, dtype=tf.int32)
history = model.fit(x, y, batch_size=8, epochs=2, verbose=0)
print(history.history.keys())Compiles a small classifier and trains it for two epochs on in-memory tensors.
Train a compiled model on in-memory arrays or tensors over multiple epochs.
Monitor held-out performance during training using validation_split or validation_data.
Automate checkpointing, logging, scheduling, or early termination during a run.
Continue a prior run while preserving epoch numbering.
fit trains a compiled model over a fixed number of epochs. Each epoch iterates through the available training data, usually in batches, and the returned History records training and validation metrics.
Follow-up: How would you train from a tf.data.Dataset? Pass the dataset as x, omit batch_size because the dataset supplies batches, and use steps_per_epoch when the dataset does not define a finite epoch.
validation_split reserves a fraction of array-like training data for validation, while validation_data explicitly supplies validation inputs and targets. validation_steps limits how many validation batches run when validation data is an iterator-like input.
Follow-up: Why might validation_split not work with a dataset? The argument is intended for supported array-like inputs; supply validation_data with a dataset instead.
callbacks receive control points during training and can monitor metrics or alter execution. initial_epoch marks the epoch from which training resumes, while epochs is the final epoch bound for the call.
Follow-up: How can training stop automatically? Use a callback such as EarlyStopping, which checks the monitored value at epoch boundaries and can halt the fit loop.
tf.keras.Model.compiletf.keras.Model.evaluatetf.keras.Model.predicttf.keras.callbacks.EarlyStoppingtf.keras.callbacks.ModelCheckpointtf.data.Dataset