スタートアップから大手まで。
調達・受発注をAIで標準化。

相見積比較も進捗管理もAIが下支え。取引先は招待で完全無料。

14日間 無料で試すクレカ不要・1分/招待企業は完全無料

投稿日:2025年1月7日

Basics and usage of deep learning using PyTorch and programming practice

Introduction to Deep Learning

💡 こうした調達・受発注の属人化、newji なら「ひとつの画面」で解決。見積依頼から発注・進捗・承認までAIが下支えします。
14日間 無料で試す →

Deep learning is a subfield of artificial intelligence (AI) focused on creating and using neural networks that mimic the human brain’s functionality.
These networks consist of layers that enable computers to learn from large amounts of data.
In recent years, deep learning has become essential in various fields, including image recognition, natural language processing, and autonomous vehicles.

What is PyTorch?

PyTorch is an open-source machine learning library developed by Facebook’s AI Research lab.
It has gained popularity for its flexibility and ease of use, especially among researchers and developers.
PyTorch provides a seamless and intuitive interface for building deep learning models, making it ideal for both beginners and experienced practitioners.

Why Use PyTorch?

PyTorch’s ease of use stems from its dynamic computational graph, which allows developers to change the network behavior on the fly.
This characteristic is particularly useful for complex models like those used in research settings, where experiments often require frequent adjustments.
Moreover, with PyTorch, users benefit from a comprehensive set of utilities for deep learning tasks, as well as extensive support and resources from a large and active community.

Flexibility and Customization

PyTorch’s dynamic computational graph system provides unparalleled flexibility when building and modifying models.
This feature empowers users to write code that feels like regular Python programming, facilitating readability and customization.

Extensive Support and Resources

The PyTorch community continuously contributes to its development by providing numerous tutorials, guides, and forums dedicated to problem-solving and learning new techniques.
This makes PyTorch a suitable choice for beginners seeking support during their learning journey.

Setting Up PyTorch

Before diving into programming with PyTorch, you need to install it on your computer.
The installation process is straightforward, but it varies according to your machine’s configuration.

Installing PyTorch

First, you’ll need to verify whether you have Python installed on your system.
If not, download and install it from the official Python website.

After installing Python, you can install PyTorch using a package manager like pip or conda.
Run the following command in your terminal to install PyTorch using pip:

“`
pip install torch torchvision torchaudio
“`

Alternatively, use conda to install PyTorch by executing the following command:

“`
conda install pytorch torchvision torchaudio -c pytorch
“`

Now that PyTorch is installed, you’re ready to explore its functionality and start building deep learning models.

Understanding Neural Networks

Neural networks, the building blocks of deep learning, consist of interconnected layers of nodes, or “neurons.”
Each layer transforms the input data in various ways, allowing the network to learn and make predictions.

Layers and Activation Functions

In a neural network, each layer processes the input from the previous one.
The first layer, known as the input layer, receives the raw data.
Subsequent layers, called hidden layers, apply distinct mathematical operations, including activation functions like ReLU or Sigmoid, to learn complex patterns.

The final layer, called the output layer, returns the network’s predicted output.

Training Neural Networks

Training a neural network involves adjusting the weights and biases of each node to minimize the difference between predicted and actual values.
This process is usually accomplished through optimization techniques like gradient descent.

During training, the network employs a loss function to evaluate its performance.
The loss function measures the discrepancy between predicted and actual values, instructing the network on how to update its parameters for better accuracy.

Building a Simple Neural Network with PyTorch

To illustrate PyTorch’s capabilities, let’s construct a simple neural network for image classification.
This example will focus on classifying handwritten digits from the popular MNIST dataset.

Loading the Dataset

First, import necessary libraries and load the dataset using PyTorch’s built-in utilities:

“`python
import torch
import torchvision
import torchvision.transforms as transforms

# Transformations to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])

# Load the training and test datasets
trainset = torchvision.datasets.MNIST(root=’./data’, train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True)

testset = torchvision.datasets.MNIST(root=’./data’, train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False)
“`

Defining the Neural Network Architecture

Next, define a simple neural network model using PyTorch’s `torch.nn` module:

“`python
import torch.nn as nn
import torch.nn.functional as F

class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.layer1 = nn.Linear(28*28, 128)
self.layer2 = nn.Linear(128, 64)
self.layer3 = nn.Linear(64, 10)

def forward(self, x):
x = x.view(-1, 28*28)
x = F.relu(self.layer1(x))
x = F.relu(self.layer2(x))
x = self.layer3(x)
return x
“`

Training the Neural Network

Instantiate the network, define the loss function, and choose an optimizer:

“`python
net = SimpleNet()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
“`

Now, train the network:

“`python
for epoch in range(5):
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 % 1000 == 999:
print(f”[{epoch + 1}, {i + 1}] loss: {running_loss / 1000:.3f}”)
running_loss = 0.0

print(“Finished Training”)
“`

Evaluating the Model

After training, evaluate your model with the test dataset to measure its accuracy:

“`python
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print(f”Accuracy: {100 * correct / total}%”)
“`

This concludes our introduction to using PyTorch for building and training a simple neural network.
As you become more familiar with PyTorch, you’ll discover its advanced features, which will enable you to tackle increasingly complex deep learning problems.

WHITE PAPER

この記事の理解を深める
無料ホワイトペーパーをプレゼント

製造業の現場で使える実務資料(PDF)を無料でお届けします。"こんな資料が届きます" ↓ 下のボタンからどうぞ。

PRODUCT — 製造業向け 調達・受発注クラウド

この記事の課題、
newji で解決しませんか?

newji は、製造業の調達・受発注に特化したクラウド/AIエージェント。見積依頼・発注書作成・進捗管理・承認をひとつの画面に集約し、AIが比較と異常検知を担当。最後の「GO」だけ人が押す仕組みです。

  • 見積〜発注〜納期を一元管理。催促・転記のムダをゼロに
  • AIが相見積もり比較と異常検知。あなたは判断だけに集中
  • 取引先は「招待」で完全無料。自社コストだけで取引先ごとデジタル化

※ 取引先から招待された企業様は完全無料でご利用いただけます

調達購買アウトソーシング

調達購買アウトソーシング

調達が回らない、手が足りない。
その悩みを、外部リソースで“今すぐ解消“しませんか。
サプライヤー調査から見積・納期・品質管理まで一括支援します。

対応範囲を確認する

OEM/ODM 生産委託

アイデアはある。作れる工場が見つからない。
試作1個から量産まで、加工条件に合わせて最適提案します。
短納期・高精度案件もご相談ください。

加工可否を相談する

NEWJI DX

現場のExcel・紙・属人化を、止めずに改善。業務効率化・自動化・AI化まで一気通貫で設計します。
まずは課題整理からお任せください。

DXプランを見る

受発注AIエージェント

受発注が増えるほど、入力・確認・催促が重くなる。
受発注管理を“仕組み化“して、ミスと工数を削減しませんか。
見積・発注・納期まで一元管理できます。

機能を確認する

You cannot copy content of this page