Example 2: Building the Neural Network Model

In this step, we will design and build a neural network model for our classification task using TensorFlow’s Keras API. The model will process two input features and classify them into two distinct categories.

Key Objectives

  1. Build a sequential neural network with layers suitable for binary classification.
  2. Choose the right activation functions and loss metrics for the task.
  3. Use optimization strategies to ensure the model learns effectively.

Step 1: Define the Model Architecture

We will create a simple neural network:

  • Input Layer: 2 neurons for the two input features.
  • Hidden Layer: 8 neurons with the ReLU activation function for non-linearity.
  • Output Layer: 1 neuron with the sigmoid activation function for binary classification.
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

# Define the model
model = Sequential([
    Dense(8, activation='relu', input_shape=(2,)),  # Hidden layer with 8 neurons
    Dense(1, activation='sigmoid')  # Output layer with 1 neuron
])

Step 2: Compile the Model

We will compile the model with the following configuration:

  • Loss Function: Binary cross-entropy, suitable for binary classification tasks.
  • Optimizer: Adam, which adapts the learning rate dynamically.
  • Metrics: Accuracy, to monitor model performance.
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

Step 3: Visualize the Model Architecture

TensorFlow provides tools to visualize the model summary and architecture.

# Display the model's architecture
model.summary()

Output:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 8)                24        
 dense_1 (Dense)             (None, 1)                9         
=================================================================
Total params: 33
Trainable params: 33
Non-trainable params: 0
_________________________________________________________________

Step 4: Explain Model Layers

  1. Input Layer: Accepts two input features.
  2. Hidden Layer:
    • 8 neurons, each receiving weighted inputs from the previous layer.
    • ReLU activation introduces non-linearity.
  3. Output Layer:
    • 1 neuron predicts the class (0 or 1).
    • Sigmoid activation outputs probabilities between 0 and 1.

Code Overview

Here’s the complete code to define and compile the model:

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

# Define the neural network model
model = Sequential([
    Dense(8, activation='relu', input_shape=(2,)),  # Hidden layer with 8 neurons
    Dense(1, activation='sigmoid')  # Output layer with sigmoid activation
])

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

# Display model summary
model.summary()

Explanation of Hyperparameters

  • Neurons in Hidden Layer:
    • Experiment with increasing or decreasing the number of neurons to see the effect on performance.
  • Activation Functions:
    • ReLU for hidden layers and Sigmoid for binary classification tasks.
  • Optimizer (Adam):
    • Dynamically adjusts the learning rate, ensuring smooth convergence.

Leave a Comment