当前位置:网站首页>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 RAC cannot connect to the database normally after modifying the scan IP. The ora-12514 problem is handled
- WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
- Selenium reptile
- Ffmpeg (I) AV_ register_ all()
- TCP那点事
- AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
- 找合适的PMP机构只需2步搞定,一查二问
- Robot Rapping Results Report
- CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)
- Build an integrated kubernetes in Fedora
猜你喜欢
![[cloud native | kubernetes] in depth understanding of pod (VI)](/img/ae/f16f5c090251ab603b88ddadff7eb3.png)
[cloud native | kubernetes] in depth understanding of pod (VI)

Kali installation configuration

【无标题】

Login common test case

AWS builds a virtual infrastructure including servers and networks (2)

Set the icon for the title section of the page

VMware Workstation related issues

Large current and frequency range that can be measured by Rogowski coil

找合适的PMP机构只需2步搞定,一查二问

B_ QuRT_ User_ Guide(27)
随机推荐
AWS builds a virtual infrastructure including servers and networks (2)
Solve NPM err! Unexpected end of JSON input while parsing near
Comment supprimer le crosstalk SiC MOSFET?
Mysql8.0 forgot the root password
【转载】STM32 GPIO类型
Goldbach`s Conjecture
RAC enable archive log
TCP
Goldbach`s Conjecture
Not so Mobile
On the solution of insufficient swap partition
[untitled]
How to suppress SiC MOSFET crosstalk?
FatMouse and Cheese
Why MySQL cannot insert Chinese data in CMD
B_ QuRT_ User_ Guide(28)
【无标题】
Selenium reptile
Kubernetes notes and the latest k3s installation introduction
Redis deployment under Linux & redis startup