Model Implementation
- This lesson explains how machine learning and deep learning models are implemented and used in real-world systems.
Model Implementation in Deep Learning (ANN Example)
In this module, we will cover the complete workflow:
Building ANN Model
Model Compilation
Model Training
Model Evaluation
Saving & Loading ModelWe’ll use a simple Binary Classification example.
Building ANN Model
We will create a simple Artificial Neural Network using TensorFlow / Keras.
Example: Binary Classification
Building a Simple ANN for Binary Classification using TensorFlow Keras
This Python example demonstrates how to build a feedforward Artificial Neural Network (ANN) for binary classification using TensorFlow Keras. The model consists of an input layer for 20 features, two hidden Dense layers with ReLU activation, and an output layer with Sigmoid activation for predicting binary outcomes.
import tensorflow as tf
from tensorflow.keras import layers, models
# Build ANN model
model = models.Sequential([
layers.Dense(64, activation='relu', input_shape=(20,)), # 20 input features
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid') # Binary output
])
Explanation:
Dense(64) → Hidden layer
ReLU → Activation function
Dense(1) → Output layer
Sigmoid → For binary classification
Model Compilation
Compilation defines:
Optimizer
Loss Function
Evaluation Metric
Compiling a Binary Classification ANN in TensorFlow Keras
This Python example demonstrates how to compile a binary classification neural network using TensorFlow Keras. The model uses the Adam optimizer, binary_crossentropy as the loss function for binary outcomes, and accuracy as the evaluation metric to track performance during training.
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
Why These?
Adam → Fast & adaptive optimizer
Binary Crossentropy → Binary classification loss
Accuracy → Evaluation metric
Model Training
We train model using .fit().
Training a Binary Classification ANN in TensorFlow Keras
This Python example demonstrates how to train a binary classification neural network using TensorFlow Keras. The model is trained on the training dataset for multiple epochs with a specified batch size, while using validation data to monitor performance. The training history stores metrics such as loss and accuracy for both training and validation sets.
history = model.fit(
X_train, y_train,
epochs=20,
batch_size=32,
validation_data=(X_val, y_val)
)
Parameters:
epochs → Number of full passes over dataset
batch_size → Number of samples per update
validation_data → To monitor overfitting
Sample Output:
Epoch 1/20
loss: 0.65 - accuracy: 0.70 - val_loss: 0.60 - val_accuracy: 0.75
Epoch 2/20
loss: 0.50 - accuracy: 0.82 - val_loss: 0.45 - val_accuracy: 0.85
Model Evaluation
Evaluate performance on test data.
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", test_accuracy)
Other Evaluation Methods:
predictions = model.predict(X_test)
For classification:
Accuracy
Precision
Recall
F1-score
Confusion Matrix
Saving & Loading Model
Very important for real projects.
Save Model
model.save("ann_model.h5")
OR newer format:
model.save("ann_model.keras")
Load Model
Loading a Saved Keras Model and Making Predictions in Python
This Python example demonstrates how to load a previously saved TensorFlow Keras model from an .h5 file and use it to make predictions. After loading the model with load_model(), you can call predict() on new input data (X_test) to generate predictions without retraining the model.
from tensorflow.keras.models import load_model
loaded_model = load_model("ann_model.h5")
Now you can use:
loaded_model.predict(X_test)