C.W.K.
Stream
Lesson 04 of 06 · published

model.fit() — The Training Loop You Don't Write

~12 min · fit, history, validation

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

The one-call training loop

model.fit() runs the whole loop: forward, loss, backward, update, metric tracking — for every batch, every epoch. It also handles validation, callbacks, and progress display.

The returned history object is your primary diagnostic. Plot history.history['loss'] vs history.history['val_loss'] to see overfitting in real time. Same for accuracy or any other metric you tracked.

Pass a tf.data.Dataset for large datasets. NumPy arrays load everything into memory. Datasets stream in lazily and parallelize preprocessing. We'll dive into tf.data in the next track — for now, know that it integrates seamlessly with fit().

Code

Full MNIST training·python
import tensorflow as tf
from tensorflow import keras
from keras import layers

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 784).astype("float32") / 255.0
x_test  = x_test.reshape(-1, 784).astype("float32") / 255.0

model = keras.Sequential([
    keras.Input(shape=(784,)),
    layers.Dense(256, activation='relu'),
    layers.BatchNormalization(),
    layers.Dropout(0.3),
    layers.Dense(10),
])

model.compile(
    optimizer=keras.optimizers.Adam(1e-3),
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'],
)

history = model.fit(
    x_train, y_train,
    batch_size=128,
    epochs=20,
    validation_split=0.1,
    shuffle=True,
    verbose=1,
)

print(history.history.keys())
# dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
Plot history — overfitting 보이기·python
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
plt.title('Accuracy')
plt.legend()
plt.show()

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.