当前位置:网站首页>Pytorch neural network construction template
Pytorch neural network construction template
2022-07-01 04:45:00 【booze-J】
Let's use this code to explain the process of neural network construction
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# Be careful dataset in transform The parameter receives an object , So we need to add parentheses , In addition, when using neural network for operation, the data type required is tensor type , therefore transforms Parameters to add .
dataloader = DataLoader(dataset,batch_size=64)
# Building neural networks
class Booze(nn.Module):
# Inherit nn.Module The initialization
def __init__(self):
super(Booze, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
# rewrite forward function
def forward(self,x):
output = self.maxpool1(x)
return output
# Generate a neural network object
obj = Booze()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs,targets = data
writer.add_images("input",imgs,step)
output = obj(imgs)
writer.add_images("output",output,step)
step+=1
writer.close()
The process can be divided into three parts :
- 1. Data reading
- 2. Building neural networks
- 3. Use tensorboard visualization
1. Data reading
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# Be careful dataset in transform The parameter receives an object , So we need to add parentheses , In addition, when using neural network for operation, the data type required is tensor type , therefore transforms Parameters to add .
dataloader = DataLoader(dataset,batch_size=64)
2. Building neural networks
# Building neural networks
class Booze(nn.Module):
# Inherit nn.Module The initialization
def __init__(self):
super(Booze, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
# rewrite forward function
def forward(self,x):
output = self.maxpool1(x)
return output
Inherit nn.Module, Inherit nn.Module The initialization of plus their own initialization , rewrite forword Method .
3. Use tensorboard visualization
# Generate a neural network object
obj = Booze()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs,targets = data
writer.add_images("input",imgs,step)
output = obj(imgs)
writer.add_images("output",output,step)
step+=1
writer.close()
边栏推荐
猜你喜欢

VIM简易使用教程

The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears

LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target

分布式-总结列表

Section 27 remote access virtual private network workflow and experimental demonstration

STM32扩展板 温度传感器和温湿度传感器的使用

2022 hoisting machinery command registration examination and hoisting machinery command examination registration

C#读写应用程序配置文件App.exe.config,并在界面上显示

Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling

2022年聚合工艺考试题及模拟考试
随机推荐
分布式数据库数据一致性的原理、与技术实现方案
Common methods in transforms
Take a cold bath
C -- array
2022 question bank and answers for safety production management personnel of hazardous chemical production units
2022年聚合工艺考试题及模拟考试
Leecode question brushing record 1332 delete palindrome subsequence
JS rotation chart
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
TCP server communication flow
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
STM32 光敏电阻传感器&两路AD采集
Applications and features of VR online exhibition
2022 t elevator repair question bank and simulation test
STM32 photoresistor sensor & two channel AD acquisition
LeetCode_ 53 (maximum subarray and)
LeetCode_ 35 (search insertion position)
2022 gas examination question bank and online simulation examination
2022年煤气考试题库及在线模拟考试
Solve the problem that the external chain file of Qiankun sub application cannot be obtained