Unit III — Neural Networks in Practice

Chapter 7 — Keras Essentials: Building Your First ANN

Framework Fill the placeholders below with your full content.

Chapter 7 — Keras Essentials: Building Your First ANN

Unit III · Neural Networks in Practice

Objectives
Build ANNs using Keras Sequential and Functional APIs · Train models for regression and classification · Evaluate and interpret training curves

1. Keras Workflow

Keras (part of TensorFlow 2.x) follows a clean Build → Compile → Fit → Evaluate workflow:

from tensorflow import keras
from tensorflow.keras import layers

# 1. Build
model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(n_features,)),
    layers.Dense(32, activation='relu'),
    layers.Dense(n_classes, activation='softmax')
])

# 2. Compile
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 3. Fit
history = model.fit(X_train, y_train, epochs=50,
                    batch_size=32, validation_split=0.2)

# 4. Evaluate
model.evaluate(X_test, y_test)
Sequential vs Functional API
Sequential: linear stack of layers — simple, sufficient for most cases.
Functional: graph of layers — use for multi-input/output, shared layers, skip connections.
Exam-ready points
  • model.summary() prints layer shapes and parameter counts.
  • history.history dict holds loss/metric per epoch for plotting.
  • Save/load: model.save('model.h5') / keras.models.load_model('model.h5').

2. ANN for Regression

# Predicting house prices
model = keras.Sequential([
    layers.Dense(128, activation='relu', input_shape=(13,)),
    layers.Dense(64, activation='relu'),
    layers.Dense(1)                  # linear output for regression
])
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2)
Exam-ready points
  • Output layer: 1 unit, no activation (or linear).
  • Loss: MSE or MAE; metric: MAE is more interpretable.
  • Always normalise input features for regression (zero-mean, unit variance).

3. ANN for Classification

# Multi-class (10 classes)
model = keras.Sequential([
    layers.Flatten(input_shape=(28,28)),         # for image input
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Callbacks: early stopping + model checkpoint
callbacks = [
    keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True),
    keras.callbacks.ModelCheckpoint('best.h5', save_best_only=True)
]
model.fit(X_train, y_train, epochs=50, callbacks=callbacks,
          validation_split=0.2)
Exam-ready points
  • Binary: output = Dense(1, sigmoid), loss = binary_crossentropy.
  • Multi-class (integer labels): loss = sparse_categorical_crossentropy.
  • Multi-class (one-hot labels): loss = categorical_crossentropy.
  • EarlyStopping monitors val_loss; patience=5 means stop after 5 epochs with no improvement.

Worked Example — Plotting training curves

import matplotlib.pyplot as plt
history = model.fit(...)
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.legend(); plt.title('Loss')
plt.subplot(1,2,2)
plt.plot(history.history['accuracy'],     label='Train acc')
plt.plot(history.history['val_accuracy'], label='Val acc')
plt.legend(); plt.title('Accuracy')
plt.tight_layout(); plt.show()

If val_loss rises while train_loss falls → overfitting. If both are high → underfitting.

Exercises

  1. Build a Keras model to classify MNIST digits (28×28 grayscale). Report accuracy.
  2. Experiment with 2, 3, and 4 hidden layers. How does depth affect val_accuracy?
  3. Plot training curves and identify the epoch where overfitting begins.

Viva Questions

  1. What is the difference between sparse_categorical_crossentropy and categorical_crossentropy?
  2. What does model.compile() do? What are its required arguments?
  3. When should you use the Functional API instead of Sequential?
  4. What is a callback in Keras? Name two useful ones.
  5. How do you prevent a Keras model from overfitting on a small dataset?
Tip: press Esc to close.