Deep Learning with PyTorch
101 words
1 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Deep Learning with PyTorch [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**Feature Engineering**](/notes/04-degree-electives-bsda4001-ds-ai-lab-week02-02b-feature-engineering)[Next**Computer Vision**](/notes/04-degree-electives-bsda4001-ds-ai-lab-week04-04-computer-vision)

Deep Learning with PyTorch
pythonimport torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset # Generate synthetic data X = torch.randn(1000, 10) y = torch.randn(1000, 1) # Define model model = nn.Sequential( nn.Linear(10, 64), nn.ReLU(), nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 1) ) # Training criterion = nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=0.001) dataset = TensorDataset(X, y) loader = DataLoader(dataset, batch_size=32, shuffle=True) for epoch in range(100): for batch_X, batch_y in loader: pred = model(batch_X) loss = criterion(pred, batch_y) optimizer.zero_grad() loss.backward() optimizer.step() if epoch % 20 == 0: print(f"Epoch {epoch}, Loss: {loss.item():.4f}")