This note covers Keras's high-level training interface โ .compile() and .fit() โ and directly contrasts it with the manual training loop from PyTorch Training Loop that this entire hub has otherwise used throughout.
The Complete Training Setup
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
history = model.fit(
x_train, y_train,
validation_data=(x_val, y_val),
epochs=20,
batch_size=32
)
This single .fit() call internally handles exactly what PyTorch's manual training loop does explicitly: the epoch loop, the batch loop, forward pass, loss computation, backward pass, optimizer step, and validation โ all of it, in one line.
Using Custom Objects Instead of String Names
from tensorflow.keras.optimizers import AdamW
from tensorflow.keras.losses import CategoricalCrossentropy
model.compile(
optimizer=AdamW(learning_rate=1e-4, weight_decay=0.01),
loss=CategoricalCrossentropy(),
metrics=['accuracy']
)
String names like 'adam' use sensible default hyperparameters; passing actual optimizer/loss objects (as shown here) allows full customization, exactly analogous to configuring torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=0.01) in PyTorch.
The history Object
print(history.history.keys()) # dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
import matplotlib.pyplot as plt
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='validation')
plt.legend()
.fit() returns a History object recording every epoch's metrics automatically โ no manual tracking (like PyTorch's metrics_history dictionary from PyTorch Custom Training Loops) needed.
Common Mistakes
- Forgetting to call
.compile()before.fit()โ the model needs an optimizer and loss function configured before it knows how to train at all. - Passing string label names that don't exactly match a Keras-recognized loss/optimizer name โ e.g. a typo in
'sparse_categorical_crossentropy'raises an error rather than silently using a default.
Interview Relevance
Q: "What exactly does Keras's .fit() call handle internally that you'd otherwise write manually in a PyTorch training loop?" The full epoch and batch iteration, the forward pass, loss computation, backward pass (gradient computation), optimizer step, and validation evaluation each epoch โ everything covered explicitly in PyTorch Training Loop and PyTorch Validation Loop is handled internally by a single .fit() call, trading explicit control for convenience.
Practice Question
How would you configure .compile() to train a binary classification model using a custom learning rate of 0.0005 with the Adam optimizer?