Overview of pytorch, environment settings and implementation examples

Machine Learning Artificial Intelligence Natural Language Processing Semantic Web Python Deep Learning Digital Transformation Reinforcement Learning Probabilistic Generative Modeling Image Processing Natural Language Processing Graph Neural Network Python Navigate This blog
Pytorch Overview

PyTorch is a deep learning library developed by Facebook and provided as open source, with features such as flexibility, dynamic computation graphs, and GPU acceleration, enabling the implementation of various machine learning tasks. The features and advantages of PyTorch are described below.

1. Dynamic Computational Graphs: PyTorch uses dynamic computational graphs, which are constructed at runtime, thereby facilitating control flow changes and conditional execution. Also, compared to frameworks with static computation graphs, such as TensorFlow, PyTorch is easier to debug and more like a natural Python syntax.

2. flexibility and simplicity: PyTorch is Pythonic and provides a simple API. This allows users to build and experiment with models intuitively. It also allows for both high-level abstraction and low-level control, making it a framework that can be used according to your needs.

3. GPU support and fast computation: PyTorch supports GPUs to accelerate model training and inference, and is a GPU-enabled library using CUDA. In addition, automatic differentiation automatically computes gradients for fast and efficient training. 4.

4 Rich community and resources: PyTorch has a very active and large community, with a wealth of resources including documentation, tutorials, and sample code. In addition, extension libraries such as PyTorch Hub and TorchVision are available, providing pre-trained models and datasets.

Various applications: PyTorch can be applied to a wide range of machine learning tasks such as image processing, natural language processing, speech processing, and reinforcement learning.

6. support for transfer learning: Transfer learning described in “Overview of Transfer Learning and Examples of Algorithms and Implementations“, which takes a pre-trained model and applies it to a new task, can be easily implemented in PyTorch.

Open Source and Expanded Community: PyTorch is an open source project, allowing users to provide feedback and contribute to improvements.

pytorch environment settings

The following is a basic setup for using PyTorch, which can be installed and configured by following these steps:

1. Download and install the Python installer from the official Python website.

2 Installing PyTorch: There are several ways to install PyTorch, but the usual way is to use pip and install as follows

Installing the CPU version: install the CPU version of PyTorch using pip.

pip install torch torchvision torchaudio

Installing the GPU version using CUDA: If using a GPU, make sure the CUDA Toolkit is installed and install PyTorch with the following command

pip install torch torchvision torchaudio cudatoolkit=xx.x -c pytorch

Where xx.x is the CUDA version that corresponds to your environment. For example, if you are using CUDA 11.1, it would look like this.

pip install torch torchvision torchaudio cudatoolkit=11.1 -c pytorch

3. version check: To check if PyTorch has been installed correctly, run the following code in a Python interactive shell or script

import torch

print(torch.__version__)
print(torch.cuda.is_available())  # GPU available or not 

This completes the basic installation and configuration of PyTorch. You will also need to install additional libraries if you wish to use PyTorch Geometric or other libraries. 4.

4. Installing PyTorch Geometric(option): If you wish to use PyTorch Geometric, follow these steps

pip install torch-geometric

5 Install Jupyter Notebook (optional): Jupyter Notebook allows you to run interactive Python programs and visualize the results. To install, run the following command

pip install jupyterlab

6. importing libraries: Finally, import the necessary libraries to use the installed libraries with Python scripts and Jupyter Notebook.

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader

This completes the PyTorch environment setup. To implement using them, you can install them in a virtual environment or another Python version, if necessary.

pytorch sample code

Below is a simple sample code using PyTorch. This example uses a linear regression model to perform a simple linear function fitting.

<Example Implementation of a Linear Regression Model>

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

# Data Preparation
x = np.random.rand(100, 1)  # Random input data
y = 3 * x + 2 + 0.2 * np.random.randn(100, 1)  # Output data with noise added to the true function

x_tensor = torch.from_numpy(x).float()
y_tensor = torch.from_numpy(y).float()

# Definition of a linear regression model
class LinearRegression(nn.Module):
    def __init__(self):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(1, 1)  # Input dimension: 1, Output dimension: 1

    def forward(self, x):
        return self.linear(x)

model = LinearRegression()

# Definition of loss functions and optimization methods
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Model Learning
num_epochs = 100
for epoch in range(num_epochs):
    optimizer.zero_grad()
    outputs = model(x_tensor)
    loss = criterion(outputs, y_tensor)
    loss.backward()
    optimizer.step()

    if (epoch+1) % 10 == 0:
        print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))

# Evaluation of the learned model
model.eval()
predicted = model(x_tensor).detach().numpy()

# Plot the fitting results of the original data and the learned model
plt.scatter(x, y, label='Original data')
plt.plot(x, predicted, label='Fitted line', color='r')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.title('Linear Regression Example')
plt.show()

The code defines a linear regression model, trains, evaluates, and plots the results in the following steps:

1. Data preparation: Generate random input data x and corresponding output data y.

2. define the linear regression model: define the LinearRegression class and define the linear model using the nn.Linear module.

3. Define loss function and optimization method: Define Mean Square Error (MSE) as the loss function and Stochastic Gradient Descent (SGD) as the optimization method.

4 Training the model: train the model in 100 epochs and calculate the loss in each epoch.

5 Evaluate the learned model: Set the model in the evaluation mode, infer the input data x_tensor with the learned model, and obtain the prediction results.

6 Plot: Plot the original data and the predictions of the learned model to visualize the fitting results.

<Example Implementation of Deep Learning>

The following are examples of deep learning implementations using PyTorch. For more information on deep learning techniques, see “About Deep Learning.

1. Image Classification: Using an image dataset (e.g. CIFAR-10, MNIST, etc.), build a model to classify images into different classes.

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
from torch.utils.data import DataLoader

# Dataset download and preprocessing
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

train_dataset = CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)

# Network Definition
class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, 3, 1, 1)
        self.conv2 = nn.Conv2d(16, 32, 3, 1, 1)
        self.fc1 = nn.Linear(32 * 8 * 8, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = torch.relu(self.conv1(x))
        x = torch.relu(self.conv2(x))
        x = x.view(-1, 32 * 8 * 8)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

model = SimpleCNN()

# Loss functions and optimization methods
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# learning loop
for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

# test
test_dataset = CIFAR10(root='./data', train=False, download=True, transform=transform)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

correct = 0
total = 0
with torch.no_grad():
    for inputs, labels in test_loader:
        outputs = model(inputs)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f"Accuracy on test set: {(correct/total)*100}%")

2. transfer learning: taking a pre-trained model and applying it to a new data set to build a high-performance model.

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
from torch.utils.data import DataLoader
import torchvision.models as models

# Data set preprocessing
transform = transforms.Compose([
    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

train_dataset = CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)

# Load pre-trained ResNet18 models
model = models.resnet18(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10)  # Match the number of classes in CIFAR-10

# Loss functions and optimization methods
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# learning loop
for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

# test
test_dataset = CIFAR10(root='./data', train=False, download=True, transform=transform)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

correct = 0
total = 0
with torch.no_grad():
    for inputs, labels in test_loader:
        outputs = model(inputs)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f"Accuracy on test set: {(correct/total)*100}%")

3. natural language processing (NLP): sentiment analysis with LSTM: Using the IMDb movie review dataset, we build a model to classify the sentiment of movie reviews.

import torch
import torch.nn as nn
import torch.optim as optim
from torchtext.legacy import data
from torchtext.legacy import datasets
import random

# Data Preparation
SEED = 1234

torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True

TEXT = data.Field(tokenize='spacy', tokenizer_language='en_core_web_sm')
LABEL = data.LabelField(dtype=torch.float)

train_data, test_data = datasets.IMDB.splits(TEXT, LABEL)

train_data, valid_data = train_data.split(random_state=random.seed(SEED))

MAX_VOCAB_SIZE = 25000

TEXT.build_vocab(train_data, max_size=MAX_VOCAB_SIZE)
LABEL.build_vocab(train_data)

BATCH_SIZE = 64

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

train_iterator, valid_iterator, test_iterator = data.BucketIterator.splits(
    (train_data, valid_data, test_data), 
    batch_size=BATCH_SIZE, 
    device=device)

# Network Definition
class SentimentLSTM(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, bidirectional, dropout):
        super().__init__()

        self.embedding = nn.Embedding(vocab_size, embedding_dim)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=n_layers, bidirectional=bidirectional, dropout=dropout)
        self.fc = nn.Linear(hidden_dim * 2, output_dim)
        self.dropout = nn.Dropout(dropout)

    def forward(self, text):
        embedded = self.dropout(self.embedding(text))
        output, (hidden, cell) = self.lstm(embedded)
        hidden = self.dropout(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim=1))
        return self.fc(hidden.squeeze(0))

INPUT_DIM = len(TEXT.vocab)
EMBEDDING_DIM = 100
HIDDEN_DIM = 256
OUTPUT_DIM = 1
N_LAYERS = 2
BIDIRECTIONAL = True
DROPOUT = 0.5

model = SentimentLSTM(INPUT_DIM, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM, N_LAYERS, BIDIRECTIONAL, DROPOUT)

# Loss functions and optimization methods
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters())

model = model.to(device)
criterion = criterion.to(device)

# learning loop
def binary_accuracy(preds, y):
    rounded_preds = torch.round(torch.sigmoid(preds))
    correct = (rounded_preds == y).float()
    acc = correct.sum() / len(correct)
    return acc

def train(model, iterator, optimizer, criterion):
    epoch_loss = 0
    epoch_acc = 0

    model.train()

    for batch in iterator:
        optimizer.zero_grad()
        predictions = model(batch.text).squeeze(1)
        loss = criterion(predictions, batch.label)
        acc = binary_accuracy(predictions, batch.label)
        loss.backward()
        optimizer.step()
        epoch_loss += loss.item()
        epoch_acc += acc.item()

    return epoch_loss / len(iterator), epoch_acc / len(iterator)

def evaluate(model, iterator, criterion):
    epoch_loss = 0
    epoch_acc = 0

    model.eval()

    with torch.no_grad():
        for batch in iterator:
            predictions = model(batch.text).squeeze(1)
            loss = criterion(predictions, batch.label)
            acc = binary_accuracy(predictions, batch.label)
            epoch_loss += loss.item()
            epoch_acc += acc.item()

    return epoch_loss / len(iterator), epoch_acc / len(iterator)

N_EPOCHS = 5

for epoch in range(N_EPOCHS):
    train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
    valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)
    print(f'Epoch: {epoch+1:02}')
    print(f'tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
    print(f't Val. Loss: {valid_loss:.3f} |  Val. Acc: {valid_acc*100:.2f}%')

# test
test_loss, test_acc = evaluate(model, test_iterator, criterion)
print(f'Test Loss: {test_loss:.3f} | Test Acc: {test_acc*100:.2f}%')

<Example of Reinforcement Learning Implementation>

The following is an example of implementing reinforcement learning. Here, the CartPole environment is used to implement the Q-Learning (Q-Learning) algorithm. For more information on reinforcement learning techniques, see “Theory and Algorithms of Various Reinforcement Learning Techniques and Their Implementation in Python.

First, import the necessary libraries and environment.

import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import gym

Next, we define the agent for Q learning.

class QLearningAgent:
    def __init__(self, state_size, action_size, learning_rate=0.01, discount_rate=0.99, exploration_rate=1.0, min_exploration_rate=0.01, exploration_decay_rate=0.99):
        self.state_size = state_size
        self.action_size = action_size
        self.learning_rate = learning_rate
        self.discount_rate = discount_rate
        self.exploration_rate = exploration_rate
        self.min_exploration_rate = min_exploration_rate
        self.exploration_decay_rate = exploration_decay_rate
        self.q_table = np.zeros((state_size, action_size))

    def choose_action(self, state):
        if np.random.uniform(0, 1) < self.exploration_rate:
            return np.random.choice(self.action_size)
        else:
            return np.argmax(self.q_table[state, :])

    def update_q_table(self, state, action, reward, next_state):
        best_next_action = np.argmax(self.q_table[next_state, :])
        td_target = reward + self.discount_rate * self.q_table[next_state, best_next_action]
        td_error = td_target - self.q_table[state, action]
        self.q_table[state, action] += self.learning_rate * td_error

    def decay_exploration_rate(self):
        self.exploration_rate = max(self.min_exploration_rate, self.exploration_rate * self.exploration_decay_rate)

Next, create the CartPole environment and initialize the agent.

env = gym.make('CartPole-v1')
state_size = env.observation_space.shape[0]
action_size = env.action_space.n

agent = QLearningAgent(state_size, action_size)

Study.

num_episodes = 1000

for episode in range(num_episodes):
    state = env.reset()
    done = False
    total_reward = 0

    while not done:
        action = agent.choose_action(state)
        next_state, reward, done, _ = env.step(action)
        total_reward += reward
        agent.update_q_table(state, action, reward, next_state)
        state = next_state

    agent.decay_exploration_rate()

    if episode % 100 == 0:
        print(f"Episode {episode}, Total Reward: {total_reward}")

env.close()

The code uses a Q-learning agent to learn in a CartPole environment. The agent acts in the environment, receiving rewards and updating its Q values, and as the learning progresses, the agent will select the optimal behavior.

<Example of Gaussian Process Implementation>

Below is an example of implementing Gaussian Processes (GP). In this example, Gaussian process regression is performed. For more information on Gaussian processes, see “Nonparametric Bayesian and Gaussian Processes.

First, import the necessary libraries.

import torch
import gpytorch
from matplotlib import pyplot as plt

Next, sample data is created. Here, a sin function plus noise is used as a sample.

train_x = torch.linspace(0, 1, 100)
train_y = torch.sin(train_x * (2 * 3.1416)) + torch.randn(train_x.size()) * 0.2

Next, we define a Gaussian process model. In this section, we define the model for Gaussian process regression.

class ExactGPModel(gpytorch.models.ExactGP):
    def __init__(self, train_x, train_y, likelihood):
        super(ExactGPModel, self).__init__(train_x, train_y, likelihood)
        self.mean_module = gpytorch.means.ConstantMean()
        self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)

likelihood = gpytorch.likelihoods.GaussianLikelihood()
model = ExactGPModel(train_x, train_y, likelihood)

Next, train the model.

model.train()
likelihood.train()

optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)

training_iterations = 50
for i in range(training_iterations):
    optimizer.zero_grad()
    output = model(train_x)
    loss = -mll(output, train_y)
    loss.backward()
    optimizer.step()

Finally, test the model and plot the results.

model.eval()
likelihood.eval()

test_x = torch.linspace(0, 1, 51)
with torch.no_grad(), gpytorch.settings.fast_pred_var():
    observed_pred = likelihood(model(test_x))

mean = observed_pred.mean
lower, upper = observed_pred.confidence_region()

plt.figure(figsize=(8, 6))
plt.plot(train_x.numpy(), train_y.numpy(), 'k.')
plt.plot(test_x.numpy(), mean.numpy(), 'b')
plt.fill_between(test_x.numpy(), lower.numpy(), upper.numpy(), alpha=0.5)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Gaussian Process Regression')
plt.show()

A Gaussian process regression model has now been created and plotted to see if it fits the sample data. The confidence interval above indicates the uncertainty of the prediction. In this example, PyTorch is used to implement Gaussian process regression and fit simple sample data.

<Example of GNN Implementation>

Below is an example of implementing a Graph Neural Network (GNN) using PyTorch Geometric. Here, a graph dataset, Cora, is used to perform the node classification task. For more information on GNNs, see “Graph Neural Networks.

First, import the necessary libraries.

import torch
import torch.nn.functional as F
from torch_geometric.datasets import Planetoid
import torch_geometric.nn as pyg_nn
import torch_geometric.utils as pyg_utils

Next, the Cora data set is read.

dataset = Planetoid(root='data/Cora', name='Cora')
data = dataset[0]

Next, graph neural networks (GNNs) are defined.

class GNN(torch.nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(GNN, self).__init__()
        self.conv1 = pyg_nn.GCNConv(input_dim, hidden_dim)
        self.conv2 = pyg_nn.GCNConv(hidden_dim, output_dim)

    def forward(self, data):
        x, edge_index = data.x, data.edge_index

        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = F.dropout(x, training=self.training)

        x = self.conv2(x, edge_index)
        return F.log_softmax(x, dim=1)

The model uses two GCNConv layers, with the input dimension, hidden layer dimension, and output dimension of each layer specified.

Next, an instance of the model is created and the optimization method is set.

model = GNN(dataset.num_node_features, hidden_dim=16, output_dim=dataset.num_classes)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)

Learning models.

def train():
    model.train()
    optimizer.zero_grad()
    out = model(data)
    loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
    loss.backward()
    optimizer.step()
    return loss.item()

def test():
    model.eval()
    out = model(data)
    pred = out.argmax(dim=1)
    test_correct = pred[data.test_mask] == data.y[data.test_mask]
    test_acc = int(test_correct.sum()) / int(data.test_mask.sum())
    return test_acc

for epoch in range(200):
    loss = train()
    if epoch % 10 == 0:
        test_acc = test()
        print(f'Epoch: {epoch}, Loss: {loss:.4f}, Test Accuracy: {test_acc:.4f}')

The code uses the Cora dataset to train the GNN model and perform the node classification task. The accuracy on the test dataset is displayed after each training.

Reference Information and Reference Books

For more information on natural language processing in general, see “Natural Language Processing Technology” and “Overview of Natural Language Processing and Examples of Various Implementations.

Reference books include “Natural language processing (NLP): Unleashing the Power of Human Communication through Machine Intelligence“.

Practical Natural Language Processing: A Comprehensive Guide to Building Real-World NLP Systems

Natural Language Processing With Transformers: Building Language Applications With Hugging Face

Hands-On Image Processing with Python: Expert techniques for advanced image analysis and effective interpretation of image data

Introduction to Image Processing Using R: Learning by Examples

Deep Learning for Vision Systems

コメント

Exit mobile version
タイトルとURLをコピーしました