Skip to content

A self‐guided introduction to Pytorch

Carlos Lizarraga-Celaya edited this page Sep 12, 2024 · 6 revisions

Self-Guided Introductory Session on PyTorch


1. Advantages of Using PyTorch for Deep Learning Tasks:

  • Dynamic Computational Graphs: PyTorch uses dynamic computation graphs, allowing for flexibility in model building and debugging.
  • Pythonic Nature: PyTorch integrates seamlessly with Python, making it easy to learn and use, especially for those familiar with Python.
  • Strong Community and Ecosystem: A large, active community with extensive resources, tutorials, and libraries that enhance productivity.
  • Interoperability with Other Libraries: PyTorch works well with other libraries, like NumPy and sci-kit-learn, enabling smooth integration of various tools.
  • GPU Acceleration: PyTorch has built-in support for CUDA, allowing models to run on GPUs for faster computation.
  • Extensive Model Zoo: Access to a wide variety of pre-trained models that can be easily fine-tuned for specific tasks.

2. Sequential Gradual List of Short PyTorch Examples:

Step 1: Installing PyTorch

pip install torch torchvision torchaudio

Step 2: Importing PyTorch and Checking for GPU Availability

import torch

# Check for GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'Using device: {device}')

Step 3: Creating Tensors

# Create a 2x3 tensor
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(tensor)

# Creating a random tensor with GPU support
tensor_gpu = torch.randn((3, 3), device=device)
print(tensor_gpu)

Step 4: Basic Operations with Tensors

# Addition
tensor_sum = tensor + tensor
print(tensor_sum)

# Element-wise multiplication
tensor_mul = tensor * tensor
print(tensor_mul)

# Matrix multiplication
tensor_matmul = torch.matmul(tensor, tensor.T)
print(tensor_matmul)

Step 5: Building a Simple Neural Network

import torch.nn as nn

# Define a simple feedforward neural network
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(3, 2)
        self.fc2 = nn.Linear(2, 1)

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

# Initialize the model, loss function, and optimizer
model = SimpleNN().to(device)
loss_fn = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

Step 6: Training the Neural Network

# Dummy input and target
input_data = torch.tensor([[1.0, 2.0, 3.0]], device=device)
target = torch.tensor([[1.0]], device=device)

# Forward pass
output = model(input_data)
loss = loss_fn(output, target)

# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()

print(f'Output: {output.item()}, Loss: {loss.item()}')

Step 7: Saving and Loading the Model

# Saving the model
torch.save(model.state_dict(), 'simple_nn.pth')

# Loading the model
model_loaded = SimpleNN()
model_loaded.load_state_dict(torch.load('simple_nn.pth'))
model_loaded.to(device)

3. PyTorch Learning Resources:

4. General References:


Created: 08/18/2024 (C. Lizárraga)

Updated: 09/11/2024 (C. Lizárraga)

DataLab, Data Science Institute, University of Arizona.

CC BY-NC-SA 4.0