PyTorch Guide with Code and Explanations
Introduction to PyTorch
PyTorch is an open-source deep learning framework developed by Facebook AI Research. It is widely used
for building and training neural networks.
# Installation: pip install torch torchvision
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
Tensors
Tensors are the fundamental data structures in PyTorch, similar to NumPy arrays but with GPU support.
# Tensors
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a + b
print(c)
print(a.device)
CUDA Support (GPU)
Move tensors to GPU using `.to('cuda')` or `.cuda()` if available.
# CUDA
a = torch.tensor([1.0, 2.0]).to('cuda') if torch.cuda.is_available() else
torch.tensor([1.0, 2.0])
Tensor Operations
Includes indexing, slicing, reshaping, and arithmetic.
# Operations
t = torch.randn(3, 3)
PyTorch Guide with Code and Explanations
print(t[0])
print(t.view(-1))
Autograd and Backpropagation
Autograd automatically computes gradients for tensor operations.
# Autograd
x = torch.ones(2, 2, requires_grad=True)
y = x + 2
z = y * y * 3
out = z.mean()
out.backward()
print(x.grad)
Neural Network Module
Use `nn.Module` to define a neural network architecture.
# Neural Net
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
Loss Functions
PyTorch provides multiple loss functions like MSE, CrossEntropy, etc.
PyTorch Guide with Code and Explanations
# Loss
criterion = nn.CrossEntropyLoss()
Optimizers
Used to update weights using gradients (SGD, Adam, etc.).
# Optimizer
optimizer = optim.Adam(model.parameters(), lr=0.001)
Training Loop
Training involves forward pass, loss calculation, backward pass, and optimizer step.
# Training Loop
for epoch in range(10):
for data, target in dataloader:
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
Dataset and DataLoader
PyTorch provides `Dataset` and `DataLoader` for loading and batching data.
# DataLoader
transform = transforms.ToTensor()
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True,
transform=transform)
dataloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
Saving and Loading Models
Save and load model weights using `torch.save` and `torch.load`.
PyTorch Guide with Code and Explanations
# Save
torch.save(model.state_dict(), 'model.pth')
# Load
model.load_state_dict(torch.load('model.pth'))
model.eval()