当前位置:网站首页>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
边栏推荐
- Kali Notes(1)
- Where is CentOS mysql5.5 configuration file
- About ASM disk space full, clean up ASM disk
- centos mysql5.5配置文件在哪
- Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
- [learning notes] simulation
- Unity gets the coordinate point in front of the current object at a certain angle and distance
- [introduction to SQL for 10 days] day4 Combined Query & specified selection
- Integer partition
- [learning notes] shortest path + spanning tree
猜你喜欢

Build an integrated kubernetes in Fedora

App automated testing appium Tutorial Part 1 - advanced supplementary content

PMP从报考到拿证基本操作,了解PMP必看篇

Two tips for block level elements

Operating principle of Rogowski coil

抖音服務器帶寬有多大,才能供上億人同時刷?

与普通探头相比,差分探头有哪些优点

Why MySQL cannot insert Chinese data in CMD

叠加阶梯图和线图及合并线图和针状图

利尔达低代码数据大屏,铲平数据应用开发门槛
随机推荐
FatMouse and Cheese
[learning notes] linear basis
nuxt3入门
【转载】STM32 GPIO类型
Loss loss function
找合适的PMP机构只需2步搞定,一查二问
NPM clean cache
Tree
三体攻击(三维拆分加二分)
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
Cloudcompare & PCL point cloud SVD decomposition
Where is CentOS mysql5.5 configuration file
Login common test case
[introduction to SQL in 10 days] day5+6 consolidated table
AWS builds a virtual infrastructure including servers and networks (2)
CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)
The Falling Leaves
B_ QuRT_ User_ Guide(26)
Super Jumping! Jumping! Jumping!
Dell r730 server startup error: [xxx] USB 1-1-port4: disabled by hub (EMI?), re-enabling...