当前位置:网站首页>Construire le premier réseau neuronal avec pytorch et optimiser
Construire le premier réseau neuronal avec pytorch et optimiser
2022-06-28 08:36:00 【Sol - Institute】
J'ai étudié récemmentpytorch,Cette fois, j'a I construit un réseau neuronal avec le tutoriel,Le plus classiqueCIFAR10,Regardez d'abord le principe
Entrée3Accès32*32,Dernier passage3Convolution,3Mise en commun maximale,Et1- Oui.flatten,Et deux linéarisations,Obtenir dix sorties
La procédure est la suivante::
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear
class NetWork(nn.Module):
def __init__(self):
super(NetWork, self).__init__()
self.conv1=Conv2d(3,32,5,padding=2)
self.maxpool1=MaxPool2d(2)
self.conv2=Conv2d(32,32,5,padding=2)
self.maxpool2=MaxPool2d(2)
self.conv3=Conv2d(32,64,5,padding=2)
self.maxpool3=MaxPool2d(2)
self.flatten=Flatten()
self.linear1=Linear(1024,64)#1024=64*4*4
self.linear2=Linear(64,10)
def forward(self,x):
x=self.conv1(x)
x=self.maxpool1(x)
x=self.conv2(x)
x=self.maxpool2(x)
x=self.conv3(x)
x=self.maxpool3(x)
x=self.flatten(x)
x=self.linear1(x)
x=self.linear2(x)
return x
network=NetWork()
print(network)
On peut encore utilisertensorboardRegarde ça.,Je m'en souviens.import
input=torch.ones((64,3,32,32))
output=network(input)
writer=SummaryWriter("logs_seq")
writer.add_graph(network,input)
writer.close()
IntensorboardC'est comme ça.
Ouvre.NetWork
Peut être agrandi
Les réseaux neuronaux ont tous des erreurs , Nous avons donc utilisé la descente par Gradient pour réduire l'erreur
Les codes sont les suivants:
import torchvision.datasets
from torch import nn
from torch.nn import Sequential,Conv2d,MaxPool2d,Flatten,Linear
from torch.utils.data import DataLoader
import torch
dataset=torchvision.datasets.CIFAR10("./dataset2",train=False,transform=torchvision.transforms.ToTensor(),
download=True)
dataloader=DataLoader(dataset,batch_size=1)
class NetWork(nn.Module):
def __init__(self):
super(NetWork, self).__init__()
self.conv1=Conv2d(3,32,5,padding=2)
self.maxpool1=MaxPool2d(2)
self.conv2=Conv2d(32,32,5,padding=2)
self.maxpool2=MaxPool2d(2)
self.conv3=Conv2d(32,64,5,padding=2)
self.maxpool3=MaxPool2d(2)
self.flatten=Flatten()
self.linear1=Linear(1024,64)#1024=64*4*4
self.linear2=Linear(64,10)
self.model1=Sequential(
Conv2d(3,32,5,padding=2),
MaxPool2d(2),
Conv2d(32,32,5,padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self,x):
# x=self.conv1(x)
# x=self.maxpool1(x)
# x=self.conv2(x)
# x=self.maxpool2(x)
# x=self.conv3(x)
# x=self.maxpool3(x)
# x=self.flatten(x)
# x=self.linear1(x)
# x=self.linear2(x)
x=self.model1(x)
return x
loss=nn.CrossEntropyLoss()
network=NetWork()
optim=torch.optim.SGD(network.parameters(),lr=0.01)## Utiliser la descente en gradient comme optimiseur
for epoch in range(20):##Cycle20Une fois
running_loss=0.0
for data in dataloader:
imgs, targets=data
outputs=network(imgs)
result_loss=loss(outputs, targets)
optim.zero_grad()## Zéro chaque baisse
result_loss.backward()
optim.step()
running_loss=running_loss+result_loss
print(running_loss)
Mon ordinateur.GPU- Oui.RTX2060 C'est plus vieux , Trois fois, ça a dû prendre 1Minutes, C'est si lent que j'ai fini de courir
Résultats obtenus:
tensor(18733.7539, grad_fn=<AddBackward0>)
tensor(16142.7451, grad_fn=<AddBackward0>)
tensor(15420.9199, grad_fn=<AddBackward0>)
On peut voir que l'erreur est de plus en plus petite , Mais courir dans l'application 20 Il y a trop peu de couches. , Quand mon nouvel ordinateur arrivera, je m'enfuirai 100Couche
边栏推荐
猜你喜欢

The 6th smart home Asia 2022 will be held in Shanghai in October

Infinite penetration test

Unity gets the coordinate point in front of the current object at a certain angle and distance

Reverse mapping of anonymous pages

Quelle est la largeur de bande du serveur de bavardage sonore pour des centaines de millions de personnes en même temps?

Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19

第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开

TCP那点事

【云原生 | Kubernetes篇】深入了解Pod(六)

887. egg drop
随机推荐
罗氏线圈工作原理
TCP那点事
CloudCompare&PCL 点云SVD分解
Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
找合适的PMP机构只需2步搞定,一查二问
Super Jumping! Jumping! Jumping!
Tree
【无标题】
[learning notes] matroid
Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
duilib 入门基础十二 样式类
PMP从报考到拿证基本操作,了解PMP必看篇
Selenium+chromedriver cannot open Google browser page
How do individuals open accounts to speculate in stocks? Is online account opening safe?
Where is CentOS mysql5.5 configuration file
Robot Rapping Results Report
[learning notes] simulation
Selenium reptile
【力扣10天SQL入门】Day5+6 合并表