0
点赞
收藏
分享

微信扫一扫

pytorch tutorial pdf

松鼠树屋 2023-12-29 阅读 25

PyTorch Tutorial: Introduction and Code Examples

PyTorch is a widely-used open-source deep learning framework that provides a seamless interface for building and training neural networks. It is known for its flexibility, efficiency, and ease of use, making it a popular choice among researchers and developers. In this tutorial, we will introduce you to the basics of PyTorch and provide some code examples to showcase its capabilities.

Installing PyTorch

Before we get started, make sure you have PyTorch installed on your system. You can install it via pip with the following command:

!pip install torch

Getting Started

To begin, let's import the necessary libraries:

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

Building a Simple Neural Network

Let's start by creating a simple neural network using PyTorch. We will build a basic feed-forward network with one hidden layer and one output layer. Here is the code:

class SimpleNet(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(SimpleNet, self).__init__()
        self.hidden = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.output = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        x = self.hidden(x)
        x = self.relu(x)
        x = self.output(x)
        return x

In the above code, we define a SimpleNet class that inherits from nn.Module and implements the forward method. The forward method describes how the inputs flow through the network layers. We use the nn.Linear module for the linear transformation and the nn.ReLU module for the activation function.

Training the Neural Network

Next, let's train our neural network on a sample dataset. We will use the MNIST dataset, which consists of handwritten digits. Here is the code to load the dataset and define the training loop:

# Load the MNIST dataset
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=torchvision.transforms.ToTensor(), download=True)

# Define the data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)

# Initialize the network
net = SimpleNet(input_size=784, hidden_size=128, output_size=10)

# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)

# Train the network
for epoch in range(num_epochs):
    for images, labels in train_loader:
        # Forward pass
        outputs = net(images.view(-1, 784))
        loss = criterion(outputs, labels)

        # Backward pass and optimization
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

In the above code, we load the MNIST dataset and define a data loader to handle batching and shuffling. We then initialize our neural network, define the loss function (cross-entropy) and optimizer (SGD), and train the network using a nested loop over the dataset.

Evaluating the Trained Model

Once the training is complete, we can evaluate the performance of our model on a separate test dataset. Here is the code to load the test dataset and compute the accuracy:

# Load the test dataset
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=torchvision.transforms.ToTensor(), download=True)

# Define the data loader
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)

# Evaluate the model
correct = 0
total = 0
with torch.no_grad():
    for images, labels in test_loader:
        outputs = net(images.view(-1, 784))
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

accuracy = 100 * correct / total
print(f"Accuracy: {accuracy}%")

In the above code, we load the test dataset and define another data loader. We then evaluate the model by making predictions on the test images and comparing them with the ground truth labels. Finally, we compute the accuracy of the model.

Conclusion

In this tutorial, we introduced PyTorch and demonstrated how to build, train, and evaluate a simple neural network using PyTorch. We covered the basics of defining a network architecture, loading and preprocessing data, and optimizing the network parameters. PyTorch provides a powerful and intuitive framework for deep learning, and we encourage you to further explore its capabilities. Happy coding!

[参考链接](

举报

相关推荐

0 条评论