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, 16)
self.fc2 = nn.Linear(16, 8)
self.fc3 = nn.Linear(8, 1)
def forward(self, x):
= torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
x return x
Learning PyTorch: Neural Networks
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 torch
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
# Set random seed for reproducibility
= 42
seed
torch.manual_seed(seed)
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)= True
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark
# Set device
= torch.device("cuda" if torch.cuda.is_available() else "cpu")
device print(f"Using device: {device}")
# Load the California housing dataset (first 100 samples for simplicity)
= fetch_california_housing()
california = california.data[:100], california.target[:100]
X, y
# Split into train/test sets
= train_test_split(
X_train, X_test, y_train, y_test =0.2, random_state=seed
X, y, test_size
)
# Standardize features
= StandardScaler()
scaler = scaler.fit_transform(X_train)
X_train = scaler.transform(X_test)
X_test
# Convert data to PyTorch tensors and move to device
= torch.from_numpy(X_train).float().to(device)
X_train_tensor = torch.from_numpy(X_test).float().to(device)
X_test_tensor = torch.from_numpy(y_train).float().unsqueeze(1).to(device)
y_train_tensor = torch.from_numpy(y_test).float().unsqueeze(1).to(device)
y_test_tensor
# Create a DataLoader for batching
= torch.utils.data.TensorDataset(X_train_tensor, y_train_tensor)
dataset = torch.utils.data.DataLoader(dataset, batch_size=20, shuffle=True) dataloader
Using device: cpu
Training the Network
Now, let’s define the training function and train our neural network.
# Train the neural network model
= X_train.shape[1]
input_size = NeuralNetwork(input_size)
model = nn.MSELoss()
loss_fn = optim.Adam(model.parameters(), lr=0.01)
optimizer = 100
n_epochs
for epoch in range(n_epochs):
model.train()for batch_X, batch_y in dataloader:
# step 1: make prediction (forward pass)
= model(batch_X)
outputs # step 2: compute loss
= loss_fn(outputs, batch_y)
loss # step 3: clear old and compute current gradients
# clear old gradients
optimizer.zero_grad() # compute current gradients
loss.backward() # step 4: update parameters
optimizer.step()
Evaluating the trained Network
After training, we need to evaluate our network’s performance on the test set.
# Evaluate the model
eval()
model.with torch.no_grad():
= 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): 0.143800164172336
R2 (Neural Network): 0.8298946833143164
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.