Working with PyTorch

Loading

PyTorch is an open-source machine learning and deep learning framework developed by Facebook AI. It provides dynamic computation graphs, making it easier to modify models during runtime. PyTorch is widely used in research, deep learning, and reinforcement learning.


Key Features of PyTorch

Dynamic Computation Graphs – Modify models on-the-fly.
GPU Acceleration – Uses CUDA for fast training.
Easy Debugging – Uses standard Python debugging tools.
Supports NumPy-like Tensors – Works seamlessly with NumPy arrays.


🔹 Installing PyTorch

pip install torch torchvision torchaudio

To verify installation:

import torch
print(torch.__version__) # Output PyTorch version

Step 1: Creating Tensors in PyTorch

A tensor is a multi-dimensional array, similar to NumPy arrays.

import torch

# Create tensors
tensor1 = torch.tensor(5) # Scalar tensor
tensor2 = torch.tensor([1, 2, 3]) # Vector tensor
tensor3 = torch.tensor([[1, 2], [3, 4]]) # Matrix tensor

print(tensor1)
print(tensor2)
print(tensor3)

Performing Tensor Operations

a = torch.tensor([2, 3])
b = torch.tensor([4, 5])

print(a + b) # Addition
print(a * b) # Multiplication
print(torch.matmul(a, b)) # Dot product

Step 2: Building a Simple Neural Network

We’ll use torch.nn to create a basic feedforward neural network.

import torch.nn as nn
import torch.optim as optim

# Define a neural network
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(10, 16) # Input layer
self.relu = nn.ReLU()
self.fc2 = nn.Linear(16, 8) # Hidden layer
self.fc3 = nn.Linear(8, 1) # Output layer

def forward(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = torch.sigmoid(self.fc3(x))
return x

# Instantiate model
model = SimpleNN()
print(model)

Step 3: Training the Model

We generate random data to train the model.

# Generate dummy data
X_train = torch.rand(1000, 10) # 1000 samples, 10 features
Y_train = torch.randint(0, 2, (1000, 1)).float() # Binary labels

# Loss function and optimizer
criterion = nn.BCELoss() # Binary Cross-Entropy Loss
optimizer = optim.Adam(model.parameters(), lr=0.01)

# Training loop
epochs = 10
for epoch in range(epochs):
optimizer.zero_grad() # Clear previous gradients
outputs = model(X_train) # Forward pass
loss = criterion(outputs, Y_train) # Compute loss
loss.backward() # Backpropagation
optimizer.step() # Update weights

print(f"Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}")

Step 4: Making Predictions

# Generate test data
X_test = torch.rand(5, 10)

# Make predictions
predictions = model(X_test)
print(predictions)

Step 5: Saving and Loading Models

Saving a Model

torch.save(model.state_dict(), "model.pth")  # Save model weights

Loading a Model

model.load_state_dict(torch.load("model.pth"))  # Load weights

PyTorch Applications

Image Classification – Face detection, medical imaging
Natural Language Processing – Text generation, chatbots
Reinforcement Learning – AI in gaming
Time-Series Forecasting – Stock price prediction

Leave a Reply

Your email address will not be published. Required fields are marked *