PyTorch Cheat Sheet

Here’s a cheat sheet for PyTorch, a popular deep learning framework in Python:

Tensor Basics

Import PyTorch:

import torch

Create Tensors:

# Scalar
x = torch.tensor(5)

# Vector
v = torch.tensor([1, 2, 3])

# Matrix
m = torch.tensor([[1, 2], [3, 4]])

Operations

Element-wise Operations:

# Addition
result = x + y

# Multiplication
result = x * y

Matrix Operations:

# Matrix Multiplication
result = torch.mm(matrix1, matrix2)

# Element-wise Matrix Multiplication
result = matrix1 * matrix2

Autograd

Automatic Differentiation:

x = torch.tensor([2.0], requires_grad=True)
y = x**2
y.backward()

Neural Networks

Define a Neural Network:

import torch.nn as nn

class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc = nn.Linear(10, 5)
        
    def forward(self, x):
        x = self.fc(x)
        return x

Loss Function and Optimization:

import torch.optim as optim

model = SimpleNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

Training a Model

Training Loop:

for epoch in range(num_epochs):
    for inputs, labels in data_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

Data Handling

Loading Data:

from torch.utils.data import DataLoader

data_loader = DataLoader(dataset, batch_size=32, shuffle=True)

Save and Load Models

Save a Model:

torch.save(model.state_dict(), 'model_weights.pth')

Load a Model:

model.load_state_dict(torch.load('model_weights.pth'))

GPU Acceleration

Move Model to GPU:

model.to('cuda')

Move Tensor to GPU:

tensor.cuda()

Common Layers

Linear Layer:

linear = nn.Linear(in_features, out_features)

Convolutional Layer:

conv = nn.Conv2d(in_channels, out_channels, kernel_size)

Visualization

Plotting Tensors:

import matplotlib.pyplot as plt

plt.imshow(tensor.numpy())
plt.show()

This cheat sheet provides an overview of essential PyTorch concepts. Adapt it based on your specific use case and explore the extensive PyTorch documentation for more in-depth information.