Appendix B: Common PyTorch Commands and Cheatsheet

Below is the complete Appendix B: Common PyTorch Commands and Cheatsheet, structured clearly and comprehensively for the book.


Appendix B: Common PyTorch Commands and Cheatsheet

This appendix summarizes essential PyTorch commands used for tensors, neural networks, autograd, data loading, optimization, GPU usage, and model management. It serves as a quick reference for learners, practitioners, and researchers.


1. Tensors: Creation, Inspection, and Operations

1.1 Creating Tensors

import torch

# Basic creation
x = torch.tensor([1, 2, 3])
x_float = torch.tensor([1.0, 2.0], dtype=torch.float32)

# From Python lists
a = torch.tensor([[1, 2], [3, 4]])

# Random tensors
torch.rand(3, 3)           # Uniform random
torch.randn(3, 3)          # Normal random
torch.randint(0, 10, (3,)) # Random integers

# Zeros, ones, identity
torch.zeros(2, 3)
torch.ones(3, 3)
torch.eye(4)               # Identity matrix

# Range
torch.arange(0, 10, 2)
torch.linspace(0, 1, 5)

1.2 Tensor Properties

x.shape
x.size()
x.dtype
x.device

1.3 Tensor Operations

# Element-wise
a + b
a - b
a * b
a / b

# Matrix multiplication
torch.matmul(a, b)
a @ b

# Reshaping
x.view(2, 3)
x.reshape(2, 3)

# Squeeze / Unsqueeze
x.squeeze()
x.unsqueeze(0)

# Concatenate
torch.cat((x, y), dim=0)

# Transpose
x.t()             # For 2D tensors
x.transpose(0, 1)

# Expand / Repeat
x.expand(3, -1)
x.repeat(2, 3)

2. GPU/Device Management

# Check GPU availability
torch.cuda.is_available()

# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Move tensor to device
x = x.to(device)

# Move model to GPU
model.to(device)

3. Autograd: Automatic Differentiation

3.1 Basic Autograd Use

x = torch.randn(3, requires_grad=True)
y = x * 2
z = y.mean()

# Compute gradients
z.backward()

# Gradient values
x.grad

3.2 Disable Gradient Tracking

with torch.no_grad():
    y = model(x)

3.3 Detach Tensor

y = x.detach()

4. Neural Network Building (torch.nn)

4.1 Creating a Simple Model

import torch.nn as nn

class MyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(10, 20)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(20, 1)

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

4.2 Common Layers

nn.Linear(in_features, out_features)
nn.Conv2d(in_channels, out_channels, kernel_size)
nn.MaxPool2d(kernel_size)
nn.Dropout(p=0.5)
nn.BatchNorm2d(num_features)
nn.Embedding(num_embeddings, embedding_dim)

4.3 Activation Functions

nn.ReLU()
nn.Sigmoid()
nn.Tanh()
nn.Softmax(dim=1)
nn.LeakyReLU()

4.4 Loss Functions

nn.MSELoss()
nn.CrossEntropyLoss()
nn.BCELoss()
nn.BCEWithLogitsLoss()
nn.L1Loss()

5. Data Handling with torch.utils.data

5.1 Creating Datasets & DataLoaders

from torch.utils.data import Dataset, DataLoader

class MyDataset(Dataset):
    def __init__(self, X, y):
        self.X = X
        self.y = y

    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):
        return self.X[idx], self.y[idx]

dataset = MyDataset(X, y)
loader = DataLoader(dataset, batch_size=32, shuffle=True)

5.2 Predefined Datasets

from torchvision import datasets, transforms

transform = transforms.ToTensor()

train_data = datasets.MNIST(root="data", train=True,
                            transform=transform, download=True)

6. Optimization: Training Steps

6.1 Define Optimizer

import torch.optim as optim

optimizer = optim.SGD(model.parameters(), lr=0.01)
# or
optimizer = optim.Adam(model.parameters(), lr=0.001)

6.2 Training Loop

for data, target in loader:
    optimizer.zero_grad()
    output = model(data)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()

7. Saving and Loading Models

7.1 Save Model

torch.save(model.state_dict(), "model.pth")

7.2 Load Model

model = MyNet()
model.load_state_dict(torch.load("model.pth"))
model.eval()

7.3 Save/Load Entire Object

torch.save(model, "complete_model.pth")
model = torch.load("complete_model.pth")

8. Common Utilities

8.1 Random Seeds

torch.manual_seed(42)
torch.cuda.manual_seed_all(42)

8.2 Convert Between NumPy and Tensor

# Tensor to numpy
x.numpy()

# Numpy to tensor
torch.from_numpy(numpy_array)

8.3 Progress Bars

from tqdm import tqdm

for batch in tqdm(loader):
    ...

9. Cheatsheet Summary Table

Domain Important Commands
Tensor Creation torch.tensor(), torch.rand(), torch.zeros(), torch.arange()
Tensor Ops view(), reshape(), cat(), matmul()
GPU tensor.to(device), torch.cuda.is_available()
Autograd requires_grad=True, backward(), zero_grad()
NN Layers nn.Linear, nn.Conv2d, nn.ReLU, nn.Dropout
Loss nn.CrossEntropyLoss, nn.MSELoss
Optimizer optim.SGD, optim.Adam, step()
Data Loading Dataset, DataLoader, transforms
Saving Models torch.save(), load_state_dict()

10. Quick Reference: Most Frequently Used Patterns

Move Everything to GPU

model.to(device)
images = images.to(device)
labels = labels.to(device)

Basic Training Step

optimizer.zero_grad()
pred = model(x)
loss = criterion(pred, y)
loss.backward()
optimizer.step()

Disable Gradients During Evaluation

model.eval()
with torch.no_grad():
    output = model(X_test)


Comments