Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using PyTorch

Introduction

PyTorch is an open-source machine learning library developed by Facebook's AI Research lab. It is widely used for deep learning applications and supports dynamic computation graphs, which makes it easier to work with and debug. This tutorial will guide you through the basics of using PyTorch, from installation to building and training a simple neural network.

Installation

Before we can start using PyTorch, we need to install it. You can install PyTorch using pip. Open your terminal and run the following command:

pip install torch torchvision torchaudio

Once the installation is complete, you can verify it by running:

python -c "import torch; print(torch.__version__)"

Basic Operations

PyTorch provides a variety of functions and operations to work with tensors, which are the fundamental data structures of PyTorch. Let's start by importing PyTorch and creating some tensors.

import torch

# Create a tensor
x = torch.tensor([1, 2, 3])
print(x)

# Basic operations
y = torch.tensor([4, 5, 6])
print(x + y)
print(x * y)
                
tensor([1, 2, 3])
tensor([5, 7, 9])
tensor([4, 10, 18])
                

Creating a Neural Network

PyTorch makes it easy to define and train neural networks. We use the torch.nn module to create a simple feedforward neural network. Here's an example:

import torch.nn as nn

# Define the network
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(3, 3)
        self.fc2 = nn.Linear(3, 1)

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

# Create the network
net = SimpleNN()
print(net)
                
SimpleNN(
  (fc1): Linear(in_features=3, out_features=3, bias=True)
  (fc2): Linear(in_features=3, out_features=1, bias=True)
)
                

Training the Network

To train the network, we need to define a loss function and an optimizer. We'll use Mean Squared Error (MSE) as the loss function and Stochastic Gradient Descent (SGD) as the optimizer. Here's how we can train the network:

# Define the loss function and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01)

# Training data
inputs = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
targets = torch.tensor([[1.0], [0.0]])

# Training loop
for epoch in range(100):
    optimizer.zero_grad()   # Zero the gradient buffers
    outputs = net(inputs)   # Forward pass
    loss = criterion(outputs, targets)   # Compute the loss
    loss.backward()   # Backward pass
    optimizer.step()   # Update the weights

    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/100], Loss: {loss.item():.4f}')
                
Epoch [10/100], Loss: 0.0152
Epoch [20/100], Loss: 0.0081
Epoch [30/100], Loss: 0.0044
Epoch [40/100], Loss: 0.0024
Epoch [50/100], Loss: 0.0013
Epoch [60/100], Loss: 0.0007
Epoch [70/100], Loss: 0.0004
Epoch [80/100], Loss: 0.0002
Epoch [90/100], Loss: 0.0001
Epoch [100/100], Loss: 0.0001
                

Saving and Loading Models

After training a model, you might want to save it for later use. PyTorch provides easy methods to save and load models. Here's how you can do it:

# Save the model
torch.save(net.state_dict(), 'model.pth')

# Load the model
net = SimpleNN()
net.load_state_dict(torch.load('model.pth'))
net.eval()
                

Conclusion

This tutorial has covered the basics of using PyTorch, including installation, basic operations, creating and training a neural network, and saving/loading models. PyTorch is a powerful and flexible library for machine learning and deep learning, and this tutorial should serve as a good starting point for further exploration.