Basics and usage of deep learning using PyTorch and programming practice | newji
製造業の見積・発注クラウド

その単価は妥当か。
AI が根拠付きで分析。

相見積の比較も発注も進捗管理も、ひとつの画面に。

サービス資料をダウンロードPDF・無料/1分で受け取れます

投稿日:2025年1月7日

Basics and usage of deep learning using PyTorch and programming practice

Introduction to Deep Learning

💡 こうした調達・受発注の属人化、Newji one なら「ひとつの画面」で解決。見積依頼から発注・進捗・承認までAIが下支えします。
サービス資料を見る(無料)→

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)を無料でお届けします。"こんな資料が届きます" ↓ 下のボタンからどうぞ。

FREE DOCUMENT — サービス資料(PDF・無料)

製造業の見積・受発注クラウド
「Newji one」とは

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

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

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

NEWJI総研

購買・調達や設計・品質の実務を、
研修テキストと実務書式にまとめています。
無料サンプルで中身を確かめられます。

NEWJI総研の資料を見る

OEM/ODM 生産委託

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

加工可否を相談する

AI/DX支援

見積・発注、紙・FAX、品質記録など、
人に頼って回っている業務を、AIと仕組みで回る形に。
まずは無料でご相談ください。

AI/DX支援を見る

見積・発注クラウド Newji one

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

機能を確認する

You cannot copy content of this page