Skip to content

PyTorch Basics — Build Your First Neural Network

DodaTech 1 min read

In this tutorial, you'll learn about PyTorch Basics. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

What You'll Learn

Learn PyTorch fundamentals — tensors, automatic differentiation, building neural networks with torch.nn, and training on GPU.

Why It Matters

PyTorch is the most popular framework for AI research. Most LLMs, diffusion models, and cutting-edge papers use PyTorch.

Real-World Use

Training GPT models, Stable Diffusion, Llama, and most academic ML research.

Installation

pip install torch torchvision

Step 1: Tensors

Tensors are like NumPy arrays but run on GPUs.

import torch

# Create a tensor
x = torch.tensor([[1, 2], [3, 4]])
print(x)

# Random tensor
random = torch.randn(3, 3)
print(random)

# Move to GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using: {device}")

Step 2: Automatic Differentiation

PyTorch tracks operations for automatic gradient computation.

x = torch.tensor([3.0], requires_grad=True)
y = x ** 2 + 2 * x + 1
y.backward()  # Compute gradients
print(x.grad)  # dy/dx = 2x + 2 = 8

Step 3: Build a Neural Network

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = x.view(-1, 784)  # Flatten
        x = F.relu(self.fc1(x))
        return self.fc2(x)

model = Net()
print(model)

Step 4: Train

from torchvision import datasets, transforms

# Load MNIST
train = datasets.MNIST('.', train=True, download=True,
                       transform=transforms.ToTensor())
loader = torch.utils.data.DataLoader(train, batch_size=32, shuffle=True)

optimizer = torch.optim.Adam(model.parameters())
loss_fn = nn.CrossEntropyLoss()

for epoch in range(3):
    for batch_x, batch_y in loader:
        pred = model(batch_x)
        loss = loss_fn(pred, batch_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(f"Epoch {epoch+1}: loss = {loss.item():.4f}")

PyTorch vs TensorFlow

Aspect PyTorch TensorFlow
Ease of learning Easier (Pythonic) Steeper
Debugging Standard Python debugger Can be opaque
Production Via TorchScript/TorchServe TF Serving, TF Lite
Research Most popular Less common
Mobile Via PyTorch Mobile TF Lite (more mature)

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro