Building an ANN with PyTorch: A Deep Dive into Neural Network Training 🚀

Jillani Soft Tech
5 min readJan 8, 2024

--

By 🌟Muhammad Ghulam Jillani, Senior Data Scientist and Machine Learning Engineer🧑‍💻

Image by Author Jillani SoftTech

Introduction

Welcome back to our enriching journey through the world of deep learning with PyTorch! In case you’re new here, don’t forget to explore the first two parts of this series, where we laid the foundational bricks of PyTorch and delved into backpropagation. Today, we’re taking a significant leap by diving into the construction and training of Artificial Neural Networks (ANNs) using PyTorch, focusing on the Pima Diabetes Dataset.

The Essence of ANNs in PyTorch

ANNs are at the heart of numerous breakthroughs in AI. They mimic the human brain’s structure and have been pivotal in solving complex problems in diverse fields. PyTorch, with its dynamic computational graph, offers an unparalleled platform to build and experiment with ANNs.

Why Choose PyTorch for ANNs?

  1. Simplicity and Flexibility: Its intuitive syntax and dynamic nature make PyTorch a favorite among researchers and developers.
  2. Dynamic Computation Graphs: This allows for on-the-fly adjustments to your network, a boon during experimental phases.
  3. Community and Support: PyTorch is backed by a robust community, ensuring a wealth of resources and support.

Step 1: Preprocessing the Pima Diabetes Dataset

Our journey begins with data preparation. The Pima Diabetes Dataset, ideal for binary classification tasks, will be our focus.

Loading and Standardizing the Data

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Load the dataset
df = pd.read_csv('pima_diabetes.csv') # Adjust the path as necessary
X = df.drop('Outcome', axis=1).values
y = df['Outcome'].values

# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

Explanation: This code block is focused on preparing the Pima Diabetes Dataset for training. We first load the dataset using Pandas, separate the features (X) from the target variable (y), and then split the data into training and testing sets. Finally, we standardize the features to ensure that our ANN receives data in a format that facilitates effective learning.

Step 2: Crafting the ANN in PyTorch

Designing an ANN involves careful consideration of the architecture, including the number of layers and neurons.

Defining the Network Architecture

import torch
import torch.nn as nn
import torch.nn.functional as F

class DiabetesPredictor(nn.Module):
def __init__(self):
super(DiabetesPredictor, self).__init__()
self.fc1 = nn.Linear(8, 16) # 8 features, 16 neurons in first hidden layer
self.fc2 = nn.Linear(16, 16) # 16 neurons in second hidden layer
self.output = nn.Linear(16, 1) # Output layer

def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = torch.sigmoid(self.output(x))
return x

Explanation: Here, we define the architecture of our ANN. The DiabetesPredictor class inherits from nn.Module, which is a base class for all neural network modules in PyTorch. We define two hidden layers and an output layer, each with its number of neurons. The forward method specifies how the data flows through the network, using ReLU activation functions for hidden layers and a sigmoid function for the output layer to suit our binary classification task.

Step 3: Training the Model

Training an ANN in PyTorch involves setting up a loss function, an optimizer, and iterating over the dataset.

The Training Loop

model = DiabetesPredictor()
criterion = nn.BCELoss() # Binary Cross Entropy Loss
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

epochs = 100
for epoch in range(epochs):
# Convert arrays to tensors
inputs = torch.tensor(X_train, dtype=torch.float32)
labels = torch.tensor(y_train, dtype=torch.float32)

# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels.unsqueeze(1))

# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()

if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')

Explanation: In this section, we initiate the training process. We first instantiate our model and define the loss function (Binary Cross Entropy Loss) and optimizer (Adam). The training loop involves iterating over the data for a specified number of epochs. Each iteration includes a forward pass (calculating the output and loss), a backward pass (computing gradients), and an optimization step (updating the model parameters). We also print the loss at regular intervals to monitor the training progress.

Step 4: Evaluating the Model

Post-training, it’s essential to evaluate the model’s performance using the test set.

Accuracy and Performance Metrics

with torch.no_grad():
y_predicted = model(torch.tensor(X_test, dtype=torch.float32))
y_predicted_cls = y_predicted.round()
acc = y_predicted_cls.eq(torch.tensor(y_test).unsqueeze(1)).sum() / float(y_test.shape[0])
print(f'Accuracy: {acc:.4f}')

Explanation: After training, we evaluate the model’s performance on the test dataset. The torch.no_grad() context manager is used to ensure that computations in this block don't track gradients, as we don't need them for evaluation. We calculate the model's accuracy by comparing the predicted class (rounded output of the model) with the actual labels and computing the proportion of correct predictions.

Conclusion

We’ve now successfully traversed the realms of ANNs with PyTorch, utilizing the Pima Diabetes Dataset. This journey from preprocessing the data to training and evaluating our ANN model has given us a hands-on experience of deep learning in action. The road ahead is full of intriguing possibilities in AI, and we’ll continue to explore these in our upcoming series.

Stay tuned as we delve deeper into the world of deep learning with PyTorch, unlocking more sophisticated techniques and applications!

🚀 Connect & Collaborate for Innovation in AI and Data Science 🌐

The dynamic world of AI and data science is powered by collaboration and shared insights. I warmly invite you to join me on this exhilarating journey. Let’s build a community where innovation, knowledge exchange, and growth are at the forefront. Here’s how we can connect:

  • 🔗 LinkedIn: Join me, Muhammad Ghulam Jillani of Jillani SoftTech, on LinkedIn. Engage in insightful discussions and stay updated with our latest projects. Visit My LinkedIn Profile and let’s expand our professional network. #LinkedInNetwork #ProfessionalGrowth
  • 💻 GitHub: Dive into the world of coding with Jillani SoftTech on GitHub. Join our mission to foster open-source innovation and cutting-edge solutions. Explore My GitHub Projects and be part of our thriving developer community. #OpenSource #Innovation
  • 📊 Kaggle: Follow my journey on Kaggle, where I tackle challenging datasets and competitions as Jillani SoftTech. Let’s collaborate to solve complex data puzzles and push the boundaries of data science. Check Out My Kaggle Contributions and let’s make data-driven discoveries together. #DataScience #Kaggle
  • ✍️ Medium & Towards Data Science: For in-depth articles and analyses, follow Jillani SoftTech on Medium and Towards Data Science. Engage with content that shapes the future of AI and technology. Read My Articles on Medium and join a community of enthusiastic learners and thinkers. #TechWriting #AIInsights

Your support, engagement, and contributions are vital to our shared journey in AI and data science. Let’s foster a community where innovation, knowledge sharing, and practical application of AI and data science lead the way. 🌟 #CommunityBuilding #AICollaboration

--

--

Jillani Soft Tech
Jillani Soft Tech

Written by Jillani Soft Tech

Senior Data Scientist & ML Expert | Top 100 Kaggle Master | Lead Mentor in KaggleX BIPOC | Google Developer Group Contributor | Accredited Industry Professional

Responses (1)