Skip to content

PyTorch examples for specific case studies

PyTorch is highly versatile and can be used for a wide range of applications from basic research to industrial deployment. Here are some practical examples for specific case studies using PyTorch, each targeting a different domain and showing the potential uses of this powerful framework.

1. Image Classification

Objective: Train a convolutional neural network (CNN) to classify images from the CIFAR-10 dataset.

Details: CIFAR-10 is a set of 60,000 32x32 color images in 10 classes, with 6,000 images per class. This example demonstrates building a simple CNN in PyTorch.

Code Example:

import torch
import torchvision
import torchvision.transforms as transforms

# Data preparation
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True)

class SimpleCNN(torch.nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = torch.nn.Conv2d(3, 6, 5)
        self.pool = torch.nn.MaxPool2d(2, 2)
        self.conv2 = torch.nn.Conv2d(6, 16, 5)
        self.fc1 = torch.nn.Linear(16 * 5 * 5, 120)
        self.fc2 = torch.nn.Linear(120, 84)
        self.fc3 = torch.nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(torch.nn.functional.relu(self.conv1(x)))
        x = self.pool(torch.nn.functional.relu(self.conv2(x)))
        x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
        x = torch.nn.functional.relu(self.fc1(x))
        x = torch.nn.functional.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = SimpleCNN()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

# Training loop
for epoch in range(2):  # loop over the dataset multiple times
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data

        optimizer.zero_grad()

        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

2. Natural Language Processing (NLP)

Objective: Build a sentiment analysis model using a recurrent neural network (RNN).

Details: This example demonstrates using an RNN with LSTM units to perform sentiment analysis on the IMDB movie review dataset.

Code Example:

import torch
from torchtext.datasets import IMDB
from torchtext.data.utils import get_tokenizer
from torchtext.vocab import build_vocab_from_iterator

tokenizer = get_tokenizer('basic_english')
def yield_tokens(data_iter):
    for _, text in data_iter:
        yield tokenizer(text)

# Load data and build vocab
train_iter, test_iter = IMDB(split=('train', 'test'))
vocab = build_vocab_from_iterator(yield_tokens(train_iter), specials=["<unk>"])
vocab.set_default_index(vocab["<unk>"])

text_pipeline = lambda x: vocab(tokenizer(x))
label_pipeline = lambda x: 1 if x == 'pos' else 0

class RNNModel(torch.nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim):
        super(RNNModel, self).__init__()
        self.embedding = torch.nn.Embedding(vocab_size, embed_dim)
        self.rnn = torch.nn.LSTM(embed_dim, hidden_dim)
        self.fc = torch.nn.Linear(hidden_dim, 1)

    def forward(self, text):
        embedded = self.embedding(text)
        output, _ = self.rnn(embedded)
        return torch.sigmoid(self.fc(output[-1,:,:]))

model = RNNModel(len(vocab), 100, 256)
criterion = torch.nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

# Simplified training loop
for epoch in range(5):
    for label, text in train_iter:


        optimizer.zero_grad()
        text = text_pipeline(text)
        label = label_pipeline(label)
        output = model(text)
        loss = criterion(output, torch.tensor([label], dtype=torch.float))
        loss.backward()
        optimizer.step()

    print(f'Epoch {epoch}, Loss: {loss.item()}')

print('Finished Training')

These examples illustrate how PyTorch can be applied in different domains, providing foundational knowledge that can be expanded upon for more complex and specific tasks.

Continuing from the practical applications in PyTorch for various domains, here are additional case studies that demonstrate the power and versatility of PyTorch for tackling more complex challenges in different fields.

3. Time Series Prediction

Objective: Use an LSTM network to predict future values in a time series, such as stock prices or energy consumption.

Details: This example illustrates using a Long Short-Term Memory (LSTM) network to model sequences of data over time, which is ideal for time series analysis.

Code Example:

import torch
import numpy as np
import matplotlib.pyplot as plt

# Generate dummy time series data
time = np.arange(0, 100, 0.1)
data = np.sin(time) + np.random.normal(scale=0.5, size=len(time))

# Prepare data for LSTM
def create_inout_sequences(input_data, tw):
    inout_seq = []
    L = len(input_data)
    for i in range(L-tw):
        train_seq = torch.FloatTensor(input_data[i:i+tw])
        train_label = torch.FloatTensor(input_data[i+tw:i+tw+1])
        inout_seq.append((train_seq, train_label))
    return inout_seq

seq_length = 10
train_inout_seq = create_inout_sequences(data, seq_length)

class LSTM(torch.nn.Module):
    def __init__(self, input_size=1, hidden_layer_size=50, output_size=1):
        super().__init__()
        self.hidden_layer_size = hidden_layer_size
        self.lstm = torch.nn.LSTM(input_size, hidden_layer_size)
        self.linear = torch.nn.Linear(hidden_layer_size, output_size)

    def forward(self, input_seq):
        lstm_out, _ = self.lstm(input_seq.view(len(input_seq), 1, -1))
        predictions = self.linear(lstm_out.view(len(input_seq), -1))
        return predictions[-1]

model = LSTM()
loss_function = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

epochs = 10
for i in range(epochs):
    for seq, labels in train_inout_seq:
        optimizer.zero_grad()
        y_pred = model(seq)
        single_loss = loss_function(y_pred, labels)
        single_loss.backward()
        optimizer.step()
    if i%2 == 1:
        print(f'epoch: {i:3} loss: {single_loss.item():10.8f}')

print('Training complete')

4. Anomaly Detection in Videos

Objective: Implement an anomaly detection model to identify unusual activities in surveillance video streams using Convolutional Neural Networks (CNN) and Autoencoders.

Details: This example focuses on using autoencoders for detecting anomalies by learning to reconstruct normal frames and identifying frames that cannot be reconstructed well as anomalies.

Code Example:

import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

# Assume 'VideoFrameDataset' is a custom dataset loading mechanism for video frames
class ConvAutoencoder(torch.nn.Module):
    def __init__(self):
        super(ConvAutoencoder, self).__init__()
        self.encoder = torch.nn.Sequential(
            torch.nn.Conv2d(3, 16, 3, stride=2, padding=1),
            torch.nn.ReLU(),
            torch.nn.Conv2d(16, 32, 3, stride=2, padding=1),
            torch.nn.ReLU(),
            torch.nn.Conv2d(32, 64, 7)
        )
        self.decoder = torch.nn.Sequential(
            torch.nn.ConvTranspose2d(64, 32, 7),
            torch.nn.ReLU(),
            torch.nn.ConvTranspose2d(32, 16, 3, stride=2, padding=1, output_padding=1),
            torch.nn.ReLU(),
            torch.nn.ConvTranspose2d(16, 3, 3, stride=2, padding=1, output_padding=1),
            torch.nn.Sigmoid()
        )

    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x

model = ConvAutoencoder()
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

# Assuming video_frames is a DataLoader containing video frames
for epoch in range(10):
    for data in video_frames:
        imgs = data
        optimizer.zero_grad()
        outputs = model(imgs)
        loss = criterion(outputs, imgs)
        loss.backward()
        optimizer.step()
    print(f'Epoch:{epoch+1}, Loss:{loss.item()}')

print('Model trained for anomaly detection')

These examples demonstrate how PyTorch can be employed in sophisticated applications like time series forecasting and anomaly detection, showing its flexibility and capability to handle various types of data and tasks effectively.

Continuing with diverse and practical PyTorch use cases, let’s explore additional applications that demonstrate PyTorch’s versatility in addressing varied and complex problems across different sectors.

5. Reinforcement Learning for Game AI

Objective: Develop a reinforcement learning agent using PyTorch that learns to play simple games, such as Atari or custom Python games, through interactions.

Details: This example uses the PyTorch implementation of Deep Q-Networks (DQN) to train an agent to make decisions based on game state inputs to maximize its total reward.

Code Example:

import gym
import torch
import random
import numpy as np
from collections import deque
import torch.nn.functional as F

class DQN(torch.nn.Module):
    def __init__(self, input_dim, output_dim):
        super(DQN, self).__init__()
        self.fc1 = torch.nn.Linear(input_dim, 128)
        self.fc2 = torch.nn.Linear(128, 64)
        self.fc3 = torch.nn.Linear(64, output_dim)

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

env = gym.make('CartPole-v1')
model = DQN(env.observation_space.shape[0], env.action_space.n)
optimizer = torch.optim.Adam(model.parameters())
loss_fn = torch.nn.MSELoss()
memory = deque(maxlen=10000)
batch_size = 32
gamma = 0.99

def get_action(state, epsilon):
    if random.random() > epsilon:
        state = torch.tensor(state, dtype=torch.float32).unsqueeze(0)
        q_values = model(state)
        action = torch.argmax(q_values).item()
    else:
        action = env.action_space.sample()
    return action

def optimize_model():
    if len(memory) < batch_size:
        return
    transitions = random.sample(memory, batch_size)
    batch = list(zip(*transitions))
    state_batch = torch.tensor(batch[0], dtype=torch.float32)
    action_batch = torch.tensor(batch[1], dtype=torch.int64)
    reward_batch = torch.tensor(batch[2], dtype=torch.float32)
    next_state_batch = torch.tensor(batch[3], dtype=torch.float32)
    done_batch = torch.tensor(batch[4], dtype=torch.float32)

    current_q_values = model(state_batch).gather(1, action_batch.unsqueeze(1)).squeeze(1)
    next_q_values = model(next_state_batch).max(1)[0]
    expected_q_values = (next_q_values * gamma * (1 - done_batch)) + reward_batch

    loss = loss_fn(current_q_values, expected_q_values)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

for episode in range(500):
    state = env.reset()
    total_reward = 0
    done = False
    while not done:
        epsilon = max(0.01, 0.08 - 0.01 * (episode / 200))  # Epsilon decay
        action = get_action(state, epsilon)
        next_state, reward, done, _ = env.step(action)
        memory.append((state, action, reward, next_state, done))
        state = next_state
        total_reward += reward
        optimize_model()
    if episode % 50 == 0:
        print(f'Episode: {episode}, Total reward: {total_reward}, Epsilon: {epsilon}')

print('Training complete')

6. Medical Image Analysis

Objective: Use convolutional neural networks (CNNs) to segment medical images, such as identifying tumors in MRI scans.

Details: This case study involves building a CNN to perform image segmentation, a critical application in medical diagnostics, to delineate regions of interest (e.g., tumor tissue) within medical scans.

Code Example:

import torch
from torch.utils.data import DataLoader
from torchvision import transforms
import torchvision.datasets as dset

# Define a simple U-Net model for image segmentation
class UNet(torch.nn.Module):
    def __init__(self):
        super(UNet, self).__init__()
        self.down1 = torch.nn.Sequential(
            torch.nn.Conv2d(1, 16, 3, padding=1),
            torch.nn.ReLU(),
            torch.nn.Conv2d(16, 16, 3, padding=1),
            torch.nn.ReLU()
        )
        # Add additional down and up layers similar to this pattern
        self.up1 = torch.nn.Sequential(
            torch.nn.Conv2d(16, 16, 3, padding=1),
            torch.nn.ReLU(),
            torch.nn.Conv2d(16, 1, 3, padding=1),
            torch.nn.ReLU()
        )

    def forward(self, x):
        x

 = self.down1(x)
        # Apply down and up layers similarly
        x = self.up1(x)
        return x

model = UNet()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
loss_fn = torch.nn.BCELoss()  # Assuming binary classification (e.g., tumor vs. non-tumor)

# Assume 'MedicalImageDataset' is a custom dataset class for your medical images
train_loader = DataLoader(MedicalImageDataset(), batch_size=10, shuffle=True)

for epoch in range(10):
    for imgs, masks in train_loader:
        preds = model(imgs)
        loss = loss_fn(preds, masks)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(f'Epoch {epoch+1}, Loss: {loss.item()}')

print('Model trained for medical image segmentation')

These case studies further emphasize the flexibility of PyTorch in handling diverse and sophisticated tasks, from interactive learning in virtual environments to critical healthcare applications.

Let's explore even more use cases where PyTorch can be applied effectively, highlighting its adaptability across various fields and complex challenges.

7. Financial Time Series Forecasting

Objective: Use PyTorch to predict future stock prices using historical data, incorporating LSTM networks for sequence prediction.

Details: Financial time series forecasting involves predicting future values of financial indicators like stock prices or exchange rates based on historical data. LSTMs are particularly well-suited for this task due to their ability to capture temporal dependencies.

Code Example:

import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

# Load and preprocess data
data = pd.read_csv('stock_prices.csv')
prices = data['Close'].values.reshape(-1, 1)
scaler = MinMaxScaler(feature_range=(-1, 1))
prices = scaler.fit_transform(prices)

def create_sequences(data, seq_length):
    xs, ys = [], []
    for i in range(len(data)-seq_length-1):
        x = data[i:(i+seq_length)]
        y = data[i+seq_length]
        xs.append(x)
        ys.append(y)
    return np.array(xs), np.array(ys)

seq_length = 10
X, y = create_sequences(prices, seq_length)
X_train, y_train = torch.FloatTensor(X), torch.FloatTensor(y)

# Define LSTM model
class StockPredictor(nn.Module):
    def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
        super(StockPredictor, self).__init__()
        self.hidden_dim = hidden_dim
        self.num_layers = num_layers
        self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim)
        c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim)
        out, _ = self.lstm(x, (h0, c0))
        out = self.fc(out[:, -1, :])
        return out

model = StockPredictor(input_dim=1, hidden_dim=50, num_layers=2, output_dim=1)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

# Training loop
for epoch in range(100):
    outputs = model(X_train)
    optimizer.zero_grad()
    loss = criterion(outputs, y_train)
    loss.backward()
    optimizer.step()

    if epoch % 10 == 0:
        print(f'Epoch {epoch} Loss {loss.item()}')

print('Training complete')

8. Autonomous Vehicle Control

Objective: Develop a neural network model to control an autonomous vehicle, interpreting sensory data to make driving decisions.

Details: This use case involves using convolutional neural networks (CNNs) combined with reinforcement learning techniques to interpret visual inputs from cameras and other sensors to navigate and control the vehicle without human intervention.

Code Example:

import torch
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
from torchvision.models import resnet34

# Assuming dataset with images labeled by steering angle
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor()
])
dataset = ImageFolder(root='path_to_training_data', transform=transform)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)

# Use a pre-trained ResNet, modify it to predict steering angles
model = resnet34(pretrained=True)
model.fc = torch.nn.Linear(model.fc.in_features, 1)  # Output one continuous value

criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

# Training loop
for epoch in range(10):
    for inputs, labels in dataloader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs.squeeze(), labels.float())
        loss.backward()
        optimizer.step()
    print(f'Epoch {epoch+1}, Loss: {loss.item()}')

print('Model trained for autonomous driving')

These examples illustrate PyTorch’s capability to handle complex, real-world problems in various domains such as finance and autonomous systems, showcasing its flexibility and powerful performance in practical applications.