当前位置:网站首页>Station B Liu Erden softmx classifier and MNIST implementation -structure 9
Station B Liu Erden softmx classifier and MNIST implementation -structure 9
2022-07-06 05:42:00 【Ning Ranye】
Series articles :
List of articles
softmax classifier


Loss function : Cross entropy

Numpty Realize the cross entropy loss function

Pytorch Realized cross entropy loss 

MNIST Realization




Guide pack
import torch
from torch.utils.data import DataLoader
from torchvision import datasets,transforms
# Use relu()
import torch.nn.functional as F
# Construct optimizer
import torch.optim as optim
1- Prepare the data
# 1- Prepare the data
batch_size = 64
# take PIL Image capture and change to Tensor
transforms = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))])
train_dataset = datasets.MNIST(root='./datasets/mnist', train=True,
transform=transforms,
download=False)
test_dataset = datasets.MNIST(root='./datasets/mnist', train=False,
transform=transforms,
download=False)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size,
shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,
shuffle=False)
2- Design the network model
# 2- Design the network model
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.lay1 = torch.nn.Linear(784,512)
self.lay2 = torch.nn.Linear(512,256)
self.lay3 = torch.nn.Linear(256,128)
self.lay4 = torch.nn.Linear(128,64)
self.lay5 = torch.nn.Linear(64,10)
def forward(self,x):
x = x.view(-1,784)
x = F.relu(self.lay1(x))
x = F.relu(self.lay2(x))
x = F.relu(self.lay3(x))
x = F.relu(self.lay4(x))
x = F.relu(self.lay5(x))
return x
3- Build a model 、 Loss function 、 Optimizer
# 3- Construct loss function and optimizer
model = Net()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.005,momentum=0.5)
4- Training 、 test
# 4- Training test
def train(epoch):
running_loss = 0.0
# enumerate(train_loader, 0): batch_idx from 0 Count
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
optimizer.zero_grad()
# forward + backward + update
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if(batch_idx % 300 == 299):
print('[%d, %5d] loss: %.3f'%(epoch + 1, batch_idx + 1, running_loss/300))
running_loss = 0.0
def test():
correct = 0
total = 0
# The test does not need to generate a calculation diagram , No gradient update is required 、 Back propagation
with torch.no_grad():
# data yes len =2 Of list
# input yes data[0], target yes data[1]
for data in test_loader:
images, label = data
outputs = model(images)
# _ Is the maximum value returned , predicted Is the subscript corresponding to the maximum
_, predicted = torch.max(outputs.data, dim=1)
total += label.size(0)
correct += (predicted == label).sum().item()
print('Accutacy on test set : %d %%'%(100*correct/total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
About :(predicted == label).sum()
Will predicted Each element in is associated with the corresponding position label Opposite edge , Same back True, Different back False. .sum Seeking True The number of 
inputs, target = data Explain the assignment 
Complete code
import torch
from torch.utils.data import DataLoader
from torchvision import datasets,transforms
# Use relu()
import torch.nn.functional as F
# Construct optimizer
import torch.optim as optim
# 1- Prepare the data
batch_size = 64
# take PIL Image capture and change to Tensor
transforms = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))])
train_dataset = datasets.MNIST(root='./datasets/mnist', train=True,
transform=transforms,
download=False)
test_dataset = datasets.MNIST(root='./datasets/mnist', train=False,
transform=transforms,
download=False)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size,
shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,
shuffle=False)
# 2- Design the network model
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.lay1 = torch.nn.Linear(784,512)
self.lay2 = torch.nn.Linear(512,256)
self.lay3 = torch.nn.Linear(256,128)
self.lay4 = torch.nn.Linear(128,64)
self.lay5 = torch.nn.Linear(64,10)
def forward(self,x):
x = x.view(-1,784)
x = F.relu(self.lay1(x))
x = F.relu(self.lay2(x))
x = F.relu(self.lay3(x))
x = F.relu(self.lay4(x))
x = F.relu(self.lay5(x))
return x
# 3- Construct loss function and optimizer
model = Net()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.005,momentum=0.5)
# 4- Training test
def train(epoch):
running_loss = 0.0
# enumerate(train_loader, 0): batch_idx from 0 Count
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
optimizer.zero_grad()
# forward + backward + update
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if(batch_idx % 300 == 299):
print('[%d, %5d] loss: %.3f'%(epoch + 1, batch_idx + 1, running_loss/300))
running_loss = 0.0
def test():
correct = 0
total = 0
# The test does not need to generate a calculation diagram , No gradient update is required 、 Back propagation
with torch.no_grad():
for data in test_loader:
images, label = data
outputs = model(images)
# _ Is the maximum value returned , predicted Is the subscript corresponding to the maximum
_, predicted = torch.max(outputs.data, dim=1)
total += label.size(0)
correct += (predicted == label).sum().item()
print('Accutacy on test set : %d %%'%(100*correct/total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
边栏推荐
- Cuda11.1 online installation
- [Jiudu OJ 07] folding basket
- 移植InfoNES到STM32
- Garbage collector with serial, throughput priority and response time priority
- A master in the field of software architecture -- Reading Notes of the beauty of Architecture
- Solution of QT TCP packet sticking
- Qt TCP 分包粘包的解决方法
- Problems encountered in installing mysql8 on MAC
- Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
- Pytorch代码注意的细节,容易敲错的地方
猜你喜欢

02. 开发博客项目之数据存储

移植InfoNES到STM32

The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
[SQL Server Express Way] - authentification et création et gestion de comptes utilisateurs

Check the useful photo lossless magnification software on Apple computer

Installation de la Bibliothèque de processus PDK - csmc

Vulhub vulnerability recurrence 73_ Webmin

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

数字经济破浪而来 ,LTD是权益独立的Web3.0网站?

26file filter anonymous inner class and lambda optimization
随机推荐
算法-- 爬楼梯(Kotlin)
【SQL server速成之路】——身份验证及建立和管理用户账户
B站刘二大人-线性回归 Pytorch
HAC cluster modifying administrator user password
After the project is released, index Html is cached
SQLite queries the maximum value and returns the whole row of data
【torch】|torch. nn. utils. clip_ grad_ norm_
59. Spiral matrix
[cloud native] 3.1 kubernetes platform installation kubespher
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Jvxetable用slot植入j-popup
Vulhub vulnerability recurrence 68_ ThinkPHP
Redis message queue
28io stream, byte output stream writes multiple bytes
Processes and threads
Qt TCP 分包粘包的解决方法
ArcGIS应用基础4 专题图的制作
Promotion hung up! The leader said it wasn't my poor skills
Graduation design game mall
PDK工艺库安装-CSMC