import torch
import torch.nn as nn
import torch.optim as optim
class NeuralNetwork(nn.Module):
def __init__(self, input_size):
super(NeuralNetwork, self).__init__()
self.fc1 = nn.Linear(input_size, 64)
self.fc2 = nn.Linear(64, 32)
self.fc3 = nn.Linear(32, 16)
self.fc4 = nn.Linear(16, 8)
self.fc5 = nn.Linear(8, 1)
def forward(self, x):
= torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = torch.relu(self.fc3(x))
x = torch.relu(self.fc4(x))
x = self.fc5(x)
x return x
Building a Neural Network with PyTorch
Introduction
In this blog post, we will explore how to build a neural network using PyTorch. PyTorch is a powerful and flexible deep learning framework that makes it easy to define, train, and evaluate neural networks. We will cover the following steps:
- Setting up the environment
- Defining the neural network
- Preparing the data
- Training the network
- Evaluating the network
Setting Up the Environment
First, let’s ensure that we have PyTorch installed. You can install PyTorch using pip:
pip install torch torchvision
Defining the Neural Network
Next, we will define a simple neural network with five layers. Each hidden layer will use the ReLU activation function.
Preparing the Data
For this example, we will create a simple dataset. In a real-world scenario, you would use a dataset from a file or an online source.
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# Dummy DataFrame for demonstration purposes
= {
data 'feature1': [1, 2, 3, 4, 5],
'feature2': [5, 4, 3, 2, 1],
'target': [1, 3, 2, 5, 4]
}= pd.DataFrame(data)
df
# Data preparation
= df[['feature1', 'feature2']].values
X = df['target'].values
y
= train_test_split(X, y, test_size=0.2, random_state=42)
X_train, X_test, y_train, y_test
= StandardScaler()
scaler = scaler.fit_transform(X_train)
X_train = scaler.transform(X_test)
X_test
# Convert data to PyTorch tensors
= torch.tensor(X_train, dtype=torch.float32)
X_train_tensor = torch.tensor(X_test, dtype=torch.float32)
X_test_tensor = torch.tensor(y_train, dtype=torch.float32).view(-1, 1)
y_train_tensor = torch.tensor(y_test, dtype=torch.float32).view(-1, 1) y_test_tensor
Training the Network
Now, let’s define the training function and train our neural network.
def train_nn(model, criterion, optimizer, X_train, y_train, epochs=100, batch_size=2):
= torch.utils.data.TensorDataset(X_train, y_train)
dataset = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
dataloader for epoch in range(epochs):
model.train()for batch_X, batch_y in dataloader:
optimizer.zero_grad()= model(batch_X)
outputs = criterion(outputs, batch_y)
loss
loss.backward()
optimizer.step()
# Initialize and train the neural network
= X_train.shape[1]
input_size = NeuralNetwork(input_size)
nn_model = nn.MSELoss()
criterion = optim.Adam(nn_model.parameters(), lr=0.01)
optimizer
=100, batch_size=2) train_nn(nn_model, criterion, optimizer, X_train_tensor, y_train_tensor, epochs
Evaluating the Network
After training, we need to evaluate our network’s performance on the test set.
# Evaluate the model
eval()
nn_model.with torch.no_grad():
= nn_model(X_test_tensor).numpy()
y_pred_nn
= mean_squared_error(y_test, y_pred_nn)
mse_nn = r2_score(y_test, y_pred_nn)
r2_nn
print(f'MSE (Neural Network): {mse_nn}')
print(f'R2 (Neural Network): {r2_nn}')
MSE (Neural Network): 3.292425297941236
R2 (Neural Network): nan
/Users/kt536/Library/Python/3.12/lib/python/site-packages/sklearn/metrics/_regression.py:996: UndefinedMetricWarning:
R^2 score is not well-defined with less than two samples.
Visualizing the Network
Finally, let’s visualize the network using torchviz
.
from torchviz import make_dot
from IPython.display import Image
= torch.randn(1, input_size)
dummy_input = nn_model(dummy_input)
output = make_dot(output, params=dict(nn_model.named_parameters()))
vis_graph format = 'png'
vis_graph.'neural_network_visualization')
vis_graph.render(
='neural_network_visualization.png') Image(filename
Conclusion
In this blog post, we covered the basics of building a neural network using PyTorch. We went through setting up the environment, defining the neural network, preparing the data, training the network, and evaluating its performance. PyTorch provides a flexible and intuitive framework for developing deep learning models, making it a popular choice for researchers and practitioners alike.
Feel free to experiment with different architectures, hyperparameters, and datasets to further enhance your understanding and build more complex models.
Happy coding!