Array-based supervised training
Train a model on in-memory arrays with mini-batches and a held-out fraction.
Keras · Function
Model.fit trains a compiled Keras model for a chosen number of epochs using array-, tensor-, or dataset-like inputs. It supports batching, validation, weighting, callbacks, and step controls.
After compilation, fit divides training data into batches, performs gradient updates, and repeats dataset iteration for the requested epochs. It can accept arrays, tensors, named-input mappings, or supported dataset and generator interfaces, while optionally applying validation, weights, callbacks, and step limits.
Model.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, tensor, mapping, PyDataset, Dataset, DataLoader, or generator | Input samples or an input-producing dataset, loader, or generator. |
y | array, tensor, or None | Target samples; omit when x yields targets. |
batch_size | integer or None | Samples per gradient update; defaults to 32 when unspecified. |
epochs | integer | Number of training epochs. |
verbose | "auto", 0, 1, or 2 | Verbosity mode. |
callbacks | list or None | Callbacks invoked during training. |
validation_split | float | Fraction of array or tensor data reserved for validation. |
validation_data | tuple, Dataset, or None | Explicit validation data. |
shuffle | boolean | Whether to shuffle training data. |
class_weight | dict or None | Class-level weighting factors. |
sample_weight | array or None | Per-sample weighting factors. |
initial_epoch | integer | Epoch at which to begin training. |
A History object containing the training history.
import keras
import numpy as np
x = np.random.random((100, 4))
y = np.random.randint(2, size=(100, 1))
model = keras.Sequential([
keras.layers.Dense(8, activation="relu"),
keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
history = model.fit(x, y, batch_size=16, epochs=2, validation_split=0.2)Compiles a binary classifier and trains it for two epochs while reserving 20% of the arrays for validation.
Train a model on in-memory arrays with mini-batches and a held-out fraction.
Feed batches from a PyDataset, tf.data Dataset, DataLoader, or generator.
Monitor generalization during training with explicit validation inputs.
Handle unequal class frequencies or varying example importance.
fit trains a compiled model for a fixed number of epochs. Each epoch is an iteration over the dataset, and batch_size controls the number of samples used per gradient update.
Follow-up: How would you train from a Dataset? Pass the Dataset as x, omit y when it yields targets, and control iteration with steps_per_epoch if needed.
validation_split reserves a fraction of array or tensor training data for validation. For dataset-based input, use validation_data because validation_split is not available there.
Follow-up: How can validation frequency be controlled? Set validation_freq to an integer or a container of epoch indices.
fit returns a History object containing records of loss and metrics for the training and validation phases. callbacks can observe training events and alter behavior during the run.
Follow-up: How can class imbalance be addressed? Supply class_weight for per-class weighting or sample_weight for per-example weighting.
Model.compileModel.evaluateModel.predictkeras.callbacks.Callbackkeras.utils.PyDataset