This four-call workflow trains the majority of Keras models in practice.
Before training, compile the model with three things:
- an optimizer: a name like
"adam"or an object such askeras.optimizers.Adam(learning_rate=1e-3)to set hyperparameters; - a loss: what to minimise;
- metrics: what to report, like
"accuracy".
`fit(x, y, batch_size, epochs, validation_split or validation_data, callbacks)` runs the training loop, compiled for the backend (XLA by default on JAX), and returns a History whose .history dict holds per-epoch losses and metrics, including val_ versions when you validate.
`evaluate(x, y)` returns the loss and metrics on new data. `predict(x)` runs the model in batches and returns NumPy arrays.
Choose loss and output layer together. For integer class labels, use SparseCategoricalCrossentropy; if the last layer has no softmax, pass from_logits=True, which is more numerically stable. For one-hot labels use CategoricalCrossentropy, and for regression "mse" or "mae".
validation_split=0.2 takes the last 20% of the arrays without shuffling, so shuffle your data first if it is ordered. verbose=0 silences the progress bar, useful in scripts and notebooks.
Example
import numpy as np
import keras
from keras import layers
rng = np.random.default_rng(0)
x = rng.random((1000, 8)).astype("float32")
y = (x[:, 0] + x[:, 1] > 1).astype("int32")
model = keras.Sequential([keras.Input((8,)),
layers.Dense(32, activation="relu"),
layers.Dense(2)]) # logits
model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-3),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"])
history = model.fit(x, y, batch_size=32, epochs=20, validation_split=0.2, verbose=0)
print(list(history.history)) # includes 'loss', 'accuracy', 'val_loss', 'val_accuracy'
loss, acc = model.evaluate(x, y, verbose=0)
logits = model.predict(x[:2], verbose=0)
print(type(logits).__name__, logits.shape) # ndarray (2, 2)Remember
- compile = optimizer + loss + metrics
- fit returns History; evaluate measures; predict returns NumPy
from_logits=Truewhen the last layer has no softmax
Watch out for
- Using
validation_spliton sorted data, so validation contains only the last classes.