Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Deep Learning Notes
Topic #562

Keras Training

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?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →