Introduction
PyTorch has become one of the most popular deep learning frameworks due to its flexibility and ease of use. In this tutorial, we'll walk through the process of building a simple neural network using PyTorch. By the end, you'll have a solid understanding of how to create, train, and evaluate a basic neural network.
Setting Up the Environment
Before we dive in, make sure you have PyTorch installed. You can install it using pip:
pip install torch torchvision
Now, let's import the necessary libraries:
import torch import torch.nn as nn import torch.optim as optim
Defining the Neural Network
We'll create a simple feedforward neural network with one hidden layer. Here's how we define it:
class SimpleNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(SimpleNN, self).__init__() self.hidden = nn.Linear(input_size, hidden_size) self.relu = nn.ReLU() self.output = nn.Linear(hidden_size, output_size) def forward(self, x): x = self.hidden(x) x = self.relu(x) x = self.output(x) return x
Let's break down what's happening here:
- We define a class
SimpleNN
that inherits fromnn.Module
. - In the
__init__
method, we create two linear layers and a ReLU activation function. - The
forward
method defines how data flows through our network.
Preparing the Data
For this example, we'll create some dummy data:
# Generate random input data X = torch.randn(100, 10) # Generate random target data y = torch.randint(0, 2, (100,)).float()
Here, we've created 100 samples with 10 features each, and corresponding binary target values.
Initializing the Model
Now, let's create an instance of our neural network:
input_size = 10 hidden_size = 5 output_size = 1 model = SimpleNN(input_size, hidden_size, output_size)
Defining Loss Function and Optimizer
We'll use Binary Cross Entropy as our loss function and Adam as our optimizer:
criterion = nn.BCEWithLogitsLoss() optimizer = optim.Adam(model.parameters(), lr=0.01)
Training the Model
Let's train our model for 100 epochs:
num_epochs = 100 for epoch in range(num_epochs): # Forward pass outputs = model(X) loss = criterion(outputs.squeeze(), y) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() if (epoch + 1) % 10 == 0: print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
This loop does the following:
- Performs a forward pass through the network.
- Calculates the loss.
- Performs backpropagation and updates the weights.
- Prints the loss every 10 epochs.
Evaluating the Model
After training, we can evaluate our model:
model.eval() with torch.no_grad(): test_output = model(X) predicted = (test_output.squeeze() > 0.5).float() accuracy = (predicted == y).float().mean() print(f'Accuracy: {accuracy.item():.4f}')
This code:
- Sets the model to evaluation mode.
- Disables gradient calculation.
- Makes predictions on our data.
- Calculates and prints the accuracy.
Conclusion
Congratulations! You've just built and trained a simple neural network using PyTorch. This example covers the basics, but PyTorch offers much more for creating complex models and working with large datasets.
As you continue your journey with PyTorch, explore more advanced topics like convolutional neural networks, recurrent neural networks, and transfer learning. Happy coding!