Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 40 ยท Training and Evaluation

compile, fit, evaluate and predict

Master the four Keras training methods: compile, fit with validation, evaluate and predict, read the History object, and rebuild them in a NumPy mini-framework.

Four methods carry almost every Keras project: compile chooses how to learn, fit trains, evaluate measures on held-out data and predict makes predictions on new inputs. This lesson shows each in detail with real Keras code, then rebuilds all four in a 60-line NumPy mini-framework, so you can run a complete compile, fit, evaluate, predict cycle and see where every number comes from.

compile

compile configures the training but does not train anything.

Python
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss=keras.losses.BinaryCrossentropy(),
    metrics=["accuracy", keras.metrics.AUC(name="auc")],
)
  • optimizer: how weights are updated (Optimizers).
  • loss: what is minimized (Loss Functions).
  • metrics: extra numbers reported each epoch; they never affect the gradients. Strings ("accuracy") or metric objects. Keras picks the right kind of accuracy from the loss.
  • Other options: loss_weights (multi-output), weighted_metrics, jit_compile (XLA speed-up on supported setups), run_eagerly (debugging).

You can call compile again later to change the optimizer or loss; the weights are kept.

fit

Python
history = model.fit(
    x_train, y_train,
    epochs=20,
    batch_size=32,
    validation_data=(x_val, y_val),      # or validation_split=0.2
    shuffle=True,
    callbacks=[...],
    verbose=2,
)

Key arguments:

Argument Meaning
epochs passes over the training data
batch_size examples per update (default 32); ignored for tf.data datasets, which are already batched
validation_data / validation_split held-out data evaluated at the end of every epoch, never trained on
shuffle reshuffle every epoch (default True)
class_weight, sample_weight upweight rare classes or important examples
callbacks hooks like early stopping and checkpoints (Overfitting and Callbacks)
initial_epoch resume the epoch counter when continuing training
verbose 0 silent, 1 progress bar, 2 one line per epoch (best in logs and notebooks)

Calling fit again continues from the current weights; it does not restart. To start over, rebuild or re-create the model.

Each epoch prints training loss and metrics and, with validation data, the val_ versions. Watch the pair: training metrics improving while validation ones stall or worsen is the signature of overfitting.

The History object

fit returns a History whose .history dictionary has one list per quantity, one entry per epoch:

Python
history.history.keys()
# dict_keys(['accuracy', 'auc', 'loss', 'val_accuracy', 'val_auc', 'val_loss'])

best_epoch = int(np.argmin(history.history["val_loss"])) + 1

Plot it to see the learning curves:

Python
import matplotlib.pyplot as plt
plt.plot(history.history["loss"], label="train")
plt.plot(history.history["val_loss"], label="validation")
plt.xlabel("epoch"); plt.ylabel("loss"); plt.legend(); plt.show()

evaluate

Python
test_loss, test_acc, test_auc = model.evaluate(x_test, y_test, batch_size=128, verbose=0)

evaluate runs inference over the data and returns the loss followed by the metrics in the order you compiled them (model.metrics_names lists them). Use it on the test set, once, at the end. Using the test set repeatedly to make decisions turns it into a second validation set and makes the final score too optimistic.

predict

Python
probs = model.predict(x_new, batch_size=128, verbose=0)    # NumPy array
labels = (probs > 0.5).astype(int)                         # binary decision
classes = probs.argmax(axis=1)                             # multi-class decision

predict returns the raw model outputs (probabilities, logits or numbers). Turning outputs into decisions (thresholds, argmax) is your job. Unlike calling model(x), predict batches large inputs and returns NumPy arrays.

flowchart LR
    C["compile<br/>optimizer, loss, metrics"] --> F["fit<br/>x_train, y_train + validation"]
    F --> H["History<br/>per-epoch curves"]
    F --> E["evaluate<br/>test set, once"]
    F --> P["predict<br/>new data"]

compile sets the rules, fit learns, evaluate scores, predict deploys.

A mini-framework with the same four methods

The class below is a stand-in for keras.Sequential: it has compile, fit (with mini-batches, shuffling, validation and a history), evaluate and predict. The layers are ReLU Dense layers and a sigmoid output with binary cross-entropy, trained by SGD with momentum. It is small enough to read in a few minutes.

Python Stand-in: MiniModel with compile, fit, evaluate and predict (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both rings are separated by a circle, which no linear model can draw, yet after a few epochs the little network does. The three points at the end are near the inner ring, near the outer ring and near the center, so the predictions should be low, high and low. (Exact numbers vary with the seeds; they are deterministic here because the seeds are fixed.)

Same workflow, real Keras

The following is the real Keras version of that experiment. The code and the learning-curve logic are the same; Keras handles the layers, the gradients and the speed.

Python
import numpy as np
import keras
from keras import layers

keras.utils.set_random_seed(1)

model = keras.Sequential([
    keras.Input(shape=(2,)),
    layers.Dense(16, activation="relu"),
    layers.Dense(16, activation="relu"),
    layers.Dense(1, activation="sigmoid"),
])

model.compile(
    optimizer=keras.optimizers.SGD(learning_rate=0.05, momentum=0.9),
    loss="binary_crossentropy",
    metrics=["accuracy"],
)

history = model.fit(x_train, y_train, epochs=20, batch_size=32,
                    validation_data=(x_val, y_val), verbose=2)

test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
print(model.predict(np.array([[0.0, 1.0], [2.0, 0.0], [0.1, 0.0]])))

Custom training steps

When fit is not flexible enough (GANs, special losses), you can override the training step by subclassing keras.Model and implementing train_step. The inside of that method is the GradientTape loop from Gradient Descent from Scratch, and you still get fit, callbacks and history for free. Chapter 8 shows how.

Typical mistakes

  • Using evaluate on training data and believing the number.
  • Forgetting that fit continues from the current weights; re-run the model-building code for a clean start.
  • Not matching x and y lengths, or passing y with the wrong shape ((n,) versus (n, 1)).
  • Reading accuracy from a loss that is not classification: it is meaningless for regression.
  • Interpreting predict output as labels: it gives probabilities; threshold or argmax yourself.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these does compile NOT do?

    compile only configures training; fit performs it.
  2. 2. What does calling fit a second time do?

    Weights persist between fit calls, so training resumes unless you rebuild the model.
  3. 3. What does model.evaluate return?

    Evaluate returns the loss followed by each metric in compile order.
  4. 4. predict returns [[0.83]] for a binary classifier. What is the predicted class at the usual threshold?

    0.83 is above 0.5, so the decision is class 1; the threshold is yours to choose.
  5. 5. Where should the test set be used?

    Repeated use for decisions makes it a second validation set and inflates the final score.

Summary

  • compile sets optimizer, loss and metrics; fit trains and returns a History; evaluate scores on held-out data; predict returns raw outputs.
  • Validation data is scored every epoch; compare loss with val_loss to detect overfitting.
  • fit resumes from existing weights; the test set is for one final evaluation.
  • A mini-framework in NumPy reproduces all four methods and the learning curves.
  • Turn probabilities into decisions yourself with a threshold or argmax.

Next: Metrics and Validation