Menu

Deep Learning With TensorFlow and Keras

Understanding neural network mathematics is important, but practical development also requires preparing datasets, defining architectures, configuring optimisation, and evaluating predictions.

TensorFlow provides tools for numerical computation and machine learning, while Keras offers a high-level interface for building neural networks.

Deep learning model development involves preparing input features, defining a neural network, training its parameters, evaluating generalisation, and generating predictions on new data.

A reliable workflow also includes preprocessing controls, reproducibility, and monitoring.

What Are TensorFlow and Keras?

TensorFlow is a machine learning framework supporting automatic differentiation, tensor operations, and model execution.

Keras provides high-level APIs for defining, training, and evaluating models.

Common Keras components include:

Component Purpose
Sequential Defines a linear stack of layers
Dense Fully connected neural network layer
Dropout Applies dropout regularisation
compile() Configures optimiser and loss
fit() Trains the model
evaluate() Evaluates model performance
predict() Generates predictions
EarlyStopping Controls training based on a monitored metric

How Do You Prepare Data for Deep Learning?

A typical workflow includes:

  1. Collect relevant observations.
  2. Validate target labels.
  3. Separate training and test data.
  4. Handle missing or invalid values.
  5. Encode categorical features when necessary.
  6. Scale numerical features when appropriate.
  7. Train and evaluate the model.

Preprocessing statistics must be learned from training data to prevent leakage.

How Do You Build a Binary Classification Neural Network?

Consider a synthetic dataset containing customer features and binary outcomes.

The objective is to predict whether an observation belongs to the positive class.

Step 1 Import Libraries and Generate Data

import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom sklearn.datasets import make_classificationfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerX, y = make_classification(    n_samples=2000,    n_features=10,    n_informative=6,    n_redundant=2,    random_state=42)X_train, X_test, y_train, y_test = train_test_split(    X,    y,    test_size=0.2,    stratify=y,    random_state=42)

make_classification() generates synthetic observations for learning and demonstration.

stratify=y helps preserve class proportions across the two subsets.

Step 2 Scale Numerical Features

scaler = StandardScaler()X_train = scaler.fit_transform(X_train)X_test = scaler.transform(X_test)

The scaler is fitted using training data only.

The same parameters are applied to the test data.

Step 3 Define the Neural Network

tf.keras.utils.set_random_seed(42)model = keras.Sequential([    keras.layers.Input(shape=(10,)),    keras.layers.Dense(32, activation="relu"),    keras.layers.Dense(16, activation="relu"),    keras.layers.Dense(1, activation="sigmoid")])

The network contains two hidden Dense layers.

The sigmoid output represents an estimated positive-class probability.

Step 4 Compile the Model

model.compile(    optimizer=keras.optimizers.Adam(        learning_rate=0.001    ),    loss="binary_crossentropy",    metrics=["accuracy"])

The configuration uses:

  • Adam optimisation
  • Binary cross-entropy loss
  • Accuracy as a monitoring metric

For imbalanced datasets, additional metrics such as precision, recall, and PR-AUC may be more informative.

Step 5 Train the Model

early_stopping = keras.callbacks.EarlyStopping(    monitor="val_loss",    patience=5,    restore_best_weights=True)history = model.fit(    X_train,    y_train,    epochs=50,    batch_size=32,    validation_split=0.2,    callbacks=[early_stopping],    verbose=0)

The model trains for up to 50 epochs.

Early stopping may terminate training sooner.

The validation split is drawn from the supplied training data rather than the separate test set.

Step 6 Evaluate the Model

test_loss, test_accuracy = model.evaluate(    X_test,    y_test,    verbose=0)print("Test Loss:", test_loss)print("Test Accuracy:", test_accuracy)

The evaluation measures model performance on observations excluded from model fitting.

The exact output varies with training conditions and library versions.

Step 7 Generate Predictions

probabilities = model.predict(    X_test,    verbose=0).ravel()predictions = (    probabilities >= 0.5).astype(int)print(predictions[:10])

The code converts predicted probabilities into binary labels using a threshold of 0.50.

The threshold can be adjusted based on validation results and the costs of different errors.

How Do You Visualise Training Performance?

Keras stores training metrics in the History object returned by fit().

import matplotlib.pyplot as pltplt.plot(    history.history["loss"],    label="Training Loss")plt.plot(    history.history["val_loss"],    label="Validation Loss")plt.xlabel("Epoch")plt.ylabel("Loss")plt.title("Neural Network Training")plt.legend()plt.show()

Training and validation curves help identify potential overfitting or unstable optimisation.

A rising validation loss alongside decreasing training loss may indicate overfitting, although the curves require contextual interpretation.

How Do You Evaluate Classification Predictions?

Accuracy alone may not provide sufficient information.

For binary classification, commonly used metrics include:

Metric Purpose
Accuracy Overall proportion of correct predictions
Precision Correctness among positive predictions
Recall Detection rate among actual positives
F1-Score Harmonic mean of precision and recall
ROC-AUC Ranking performance across thresholds
PR-AUC Precision-recall behaviour

Python Example

from sklearn.metrics import classification_reportprint(    classification_report(        y_test,        predictions    ))

This reports precision, recall, F1-score, and support for each class.

For a deployment decision, performance should also be examined across relevant user groups and operational conditions.

What Is Model Checkpointing?

Model checkpointing saves model state during training.

This helps preserve selected model versions and recover from interrupted training.

checkpoint = keras.callbacks.ModelCheckpoint(    "best_model.keras",    monitor="val_loss",    save_best_only=True)

The callback saves a model when the monitored validation loss improves.

The checkpoint can later be loaded using an appropriate Keras model-loading function.

How Do You Save and Load a Neural Network?

model.save("customer_model.keras")

To load:

loaded_model = keras.models.load_model(    "customer_model.keras")

A deployed model also requires the fitted preprocessing transformations.

Saving the neural network alone is insufficient if incoming features are not processed consistently.

What Is Transfer Learning?

Transfer learning reuses representations learned by a previously trained model.

For example, an image classifier trained on a large image dataset may provide useful features for a smaller specialised image classification task.

A common workflow is:

  1. Load a pretrained model.
  2. Reuse its feature extraction layers.
  3. Add a task-specific output layer.
  4. Train the new layers.
  5. Optionally fine-tune selected pretrained layers.

Transfer learning can reduce training requirements, although its effectiveness depends on the similarity between the source and target tasks.

What Is Model Deployment?

Deployment makes a trained model available to process new inputs.

A deployed model may be accessed through an API, batch-processing system, or application.

A prediction service typically performs:

Input Validation → Preprocessing → Model Inference → Output Processing

Production systems may also include authentication, logging, monitoring, and failure handling.

What Is Model Monitoring?

Model monitoring checks whether a deployed model continues to perform reliably.

Common monitoring areas include:

Data Drift: Changes in input feature distributions.

Concept Drift: Changes in relationships between inputs and targets.

Prediction Drift: Changes in model output distributions.

Performance Monitoring: Changes in evaluation metrics when ground-truth outcomes become available.

Operational Monitoring: Latency, resource usage, and service errors.

Drift does not automatically prove that a model is failing, but it may indicate that further investigation is necessary.

Deep Learning Development Workflow

Stage Main Activity Example Tool
Data Preparation Cleaning and transformation Pandas, Scikit-learn
Model Definition Create network layers Keras
Training Optimise parameters TensorFlow
Evaluation Calculate predictive metrics Scikit-learn
Visualisation Examine learning curves Matplotlib
Persistence Save trained model Keras
Monitoring Track predictions and performance Monitoring systems

Common Deep Learning Development Mistakes

  1. Fitting preprocessing transformations before splitting data.
  2. Evaluating only training accuracy.
  3. Using an inappropriate loss function or output activation.
  4. Ignoring classification imbalance.
  5. Selecting the final decision threshold using the test set.
  6. Deploying a model without saving its preprocessing configuration.
  7. Failing to monitor performance after deployment.

Data Science Tutorial for Beginners

VA

Vaishali Ardhana



Learn via Video Course

undefined Logo

0 hrs



Get in Touch For Details