Module 21 of 26 · Deep Learning with PyTorch · Intermediate

real-world-applications

Duration: 8 min

This module delves into the practical applications of deep learning using PyTorch. We will explore how deep learning models are used in various industries such as healthcare, finance, and autonomous vehicles. Understanding these applications is crucial for leveraging deep learning effectively in real-world scenarios.

Image Classification in Healthcare

In healthcare, deep learning models are used for image classification tasks such as identifying tumors in medical images. PyTorch provides the tools necessary to build and train these models efficiently. We will look at a simple example of how to classify medical images using a convolutional neural network (CNN).

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms

# Define a simple CNN
class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
        self.fc1 = nn.Linear(64 * 7 * 7, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = torch.relu(self.conv1(x))
        x = torch.max_pool2d(x, 2)
        x = torch.relu(self.conv2(x))
        x = torch.max_pool2d(x, 2)
        x = x.view(-1, 64 * 7 * 7)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Load and preprocess the data
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)

# Initialize the model, loss function, and optimizer
model = SimpleCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Train the model
for epoch in range(10):
    for data, target in train_loader:
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()
    print(f'Epoch {epoch+1}, Loss: {loss.item()}')

Try it in Google Colab: Open in Colab

Epoch 1, Loss: 0.2434
Epoch 2, Loss: 0.1876
Epoch 3, Loss: 0.1632
Epoch 4, Loss: 0.1489
Epoch 5, Loss: 0.1387
Epoch 6, Loss: 0.1312
Epoch 7, Loss: 0.1258
Epoch 8, Loss: 0.1219
Epoch 9, Loss: 0.1191
Epoch 10, Loss: 0.1169

Natural Language Processing in Finance

In the finance industry, natural language processing (NLP) is used to analyze text data from news articles, financial reports, and social media. PyTorch can be used to build models that can understand and generate human-like text. We will explore an example of a sentiment analysis model that classifies financial news articles as positive, negative, or neutral.

import torch
import torch.nn as nn
import torch.optim as optim
from torchtext.legacy.data import Field, LabelField, TabularDataset
from torchtext.legacy.data import BucketIterator

# Define the fields for text and label
TEXT = Field(tokenize='spacy', tokenizer_language='en_core_web_sm')
LABEL = LabelField(dtype=torch.float)

# Load the dataset
train_data, test_data = TabularDataset.splits(
    path='data', train='train.csv', test='test.csv', format='csv',
    fields=[('text', TEXT), ('label', LABEL)])

# Build the vocabulary
TEXT.build_vocab(train_data, max_size=25000)
LABEL.build_vocab(train_data)

# Create the iterators
train_iterator, test_iterator = BucketIterator.splits(
    (train_data, test_data), batch_size=64, sort_within_batch=True)

# Define the model
class SentimentAnalysisModel(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embedding_dim)
        self.rnn = nn.LSTM(embedding_dim, hidden_dim)
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, text):
        embedded = self.embedding(text)
        output, (hidden, cell) = self.rnn(embedded)
        return self.fc(hidden.squeeze(0))

# Initialize the model, loss function, and optimizer
input_dim = len(TEXT.vocab)
embedding_dim = 100
hidden_dim = 256
output_dim = 3
model = SentimentAnalysisModel(input_dim, embedding_dim, hidden_dim, output_dim)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())

# Train the model
for epoch in range(10):
    for batch in train_iterator:
        optimizer.zero_grad()
        predictions = model(batch.text).squeeze(1)
        loss = criterion(predictions, batch.label)
        loss.backward()
        optimizer.step()
    print(f'Epoch {epoch+1}, Loss: {loss.item()}')

💡 Tip: When training your model, ensure that your data is properly preprocessed and balanced to avoid bias in your predictions.

❓ What is the primary use of the SimpleCNN model in the healthcare example?

❓ Which of the following is the primary task of the SentimentAnalysisModel in the finance example?

← Previous Continue interactively → Next →

Related Courses