投稿日:2025年2月8日

Basic course on deep learning using PyTorch

Introduction to Deep Learning and PyTorch

Deep learning is a subfield of machine learning that mimics the workings of the human brain in processing data for use in decision-making.
It involves neural networks with many layers.
The PyTorch library is a popular open-source framework for deep learning that provides flexibility and ease of use.

It is developed by Facebook’s AI Research lab and is widely used both in academia and industry.
In this article, we will explore the basic concepts of deep learning and how to implement them using PyTorch.

Understanding Neural Networks

At the core of deep learning are neural networks.
These are computational models that are inspired by the way neurons in the brain communicate with each other.
Neural networks consist of layers of nodes or neurons, where each node represents a function.
These layers are divided into three types: input layer, hidden layers, and output layer.

The input layer receives input data, the hidden layers process this data, and the output layer provides the final prediction.
The true power of deep learning comes from having multiple hidden layers, which allow the network to learn complex patterns in the data.

The Role of Activation Functions

Activation functions help neural networks learn complex patterns by introducing non-linearity.
Without activation functions, a neural network would simply be a linear regression model.
Common activation functions include the Sigmoid, Tanh, and ReLU (Rectified Linear Unit) functions.
Each activation function has its characteristics and is chosen based on the specific needs of the neural network.

Setting Up Your Environment

To get started with PyTorch, you need to set up your programming environment.
First, ensure you have Python installed on your machine as PyTorch is a Python-based framework.
You can download it from the official Python website.

Next, using a package manager like pip, you can install PyTorch.
Run the command `pip install torch` in your command prompt or terminal.
Verify your installation by importing PyTorch in a Python script or an interactive environment like Jupyter Notebook using `import torch`.

Building Your First Neural Network with PyTorch

Let’s dive into creating a simple neural network with PyTorch.
For illustration, we will build a network to classify digit images from the MNIST dataset, a popular dataset in machine learning.

Step 1: Import Libraries

To begin, import the necessary libraries.
You’ll need `torch`, `torch.nn`, and `torch.optim` for building the model and training it.
Additionally, import datasets and transforms from `torchvision`, a library that provides datasets and pre-trained models.

“`python
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
“`

Step 2: Load and Pre-process the Data

Load the MNIST dataset and apply transforms.
Transforms are operations applied to images before feeding them to the model.
For this example, convert the images to tensors and normalize them.

“`python
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainset = datasets.MNIST(root=’./data’, train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
“`

Step 3: Define the Neural Network Model

Define a simple feedforward neural network using `torch.nn.Module`.
Our network consists of two hidden layers.

“`python
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)

def forward(self, x):
x = x.view(x.shape[0], -1)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
“`

Step 4: Initialize the Model, Define Loss, and Optimizer

Instantiate the model, choose a loss function, and select an optimizer.
In this example, we use CrossEntropyLoss and SGD.

“`python
model = SimpleNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
“`

Step 5: Train the Model

Train the network by looping through the data and updating the weights based on the loss.

“`python
for epoch in range(5):
running_loss = 0
for images, labels in trainloader:
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f”Epoch {epoch+1}, Loss: {running_loss/len(trainloader)}”)
“`

Conclusion

Congratulations! You have built your first simple neural network using PyTorch.
This is just the tip of the iceberg in the world of deep learning.
PyTorch offers a rich ecosystem of tools and libraries for building complex models for various types of data, including images, text, and audio.

As you delve deeper into deep learning, explore concepts such as convolutional neural networks and recurrent neural networks, which are more suited for image and sequence data, respectively.
Given the growing importance of AI and machine learning, mastering PyTorch will surely open many doors in both research and industry.

Happy coding!

You cannot copy content of this page