๐Ÿ  VisualStudioTutor.com  ยท  Python Tutorial Home  ยท  Python Lesson 32 of 40
Lesson 32 of 40 AI / ML Expert โฑ 35 min

Deep Learning with PyTorch

Train neural networks with PyTorch โ€” tensors, autograd, nn.Module, DataLoader, GPU training, transfer learning, and ONNX export.

Part 1: What You Will Learn

  • Create PyTorch tensors and move them to the available CPU or GPU device.
  • Define a neural network with nn.Module.
  • Train the network with a loss function, backpropagation, and an optimiser.
  • Use the trained model to make a prediction.

Part 2: Key Concepts

A neural network contains layers of learnable parameters. During training, PyTorch calculates a loss, uses automatic differentiation to compute gradients, and lets an optimiser update the parameters.

  • Tensor: PyTorch's multidimensional numerical object.
  • Forward pass: calculate predictions.
  • Loss: measure prediction error.
  • Backward pass: calculate gradients with autograd.
  • Optimiser: update weights using those gradients.

Part 3: Topic-Specific Code Example

import torch
from torch import nn

device = torch.device(
    "cuda" if torch.cuda.is_available() else "cpu"
)
print("Using:", device)

X = torch.tensor(
    [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]],
    device=device,
)
y = torch.tensor(
    [[3.0], [5.0], [7.0], [9.0], [11.0], [13.0]],
    device=device,
)

class SimpleRegressor(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(1, 8),
            nn.ReLU(),
            nn.Linear(8, 1),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.network(x)

model = SimpleRegressor().to(device)
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.03)

for epoch in range(500):
    prediction = model(X)
    loss = loss_function(prediction, y)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

model.eval()
with torch.no_grad():
    new_x = torch.tensor([[7.0]], device=device)
    predicted_y = model(new_x)

print("Prediction for x=7:", round(predicted_y.item(), 2))
pip install torch

Part 4: How the Example Works

The sample relationship is approximately y = 2x + 1. The network is not given that formula; it learns an approximation from examples. loss.backward() calculates gradients and optimizer.step() changes the model parameters. torch.no_grad() is used during prediction because gradients are unnecessary.

Part 5: Hands-On Practice

Mini project โ€” Exam Score Regressor. Train a network using study hours and attendance as two inputs and exam score as the output. Add a DataLoader when the dataset becomes larger, save the model with torch.save(), and reload it for prediction.

Part 6: Next Steps

Modify the network size and learning rate, then continue to Lesson 33 to connect Python applications to large language models.

๐Ÿ“˜ Want the complete guide with projects? Get the book โ†’