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.
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
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:
SimpleNN
that inherits from nn.Module
.__init__
method, we create two linear layers and a ReLU activation function.forward
method defines how data flows through our network.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.
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)
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)
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:
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:
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!
05/11/2024 | Python
08/12/2024 | Python
25/09/2024 | Python
26/10/2024 | Python
14/11/2024 | Python
15/10/2024 | Python
14/11/2024 | Python
22/11/2024 | Python
14/11/2024 | Python
06/10/2024 | Python
15/10/2024 | Python