当前位置:网站首页>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()
边栏推荐
- 细数软件研发效能的七宗罪
- C - detailed explanation of operators and summary of use cases
- One click shell to automatically deploy any version of redis
- 2022 polymerization process test questions and simulation test
- Pytorch(四) —— 可视化工具 Visdom
- Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- Section 27 remote access virtual private network workflow and experimental demonstration
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- This sideline workload is small, 10-15k, free unlimited massage
猜你喜欢

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

软件研发的十大浪费:研发效能的另一面

Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

神经网络的基本骨架-nn.Moudle的使用

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022

2022 question bank and answers for safety production management personnel of hazardous chemical production units

STM32 photoresistor sensor & two channel AD acquisition
随机推荐
LeetCode_35(搜索插入位置)
【硬十宝典】——1.【基础知识】电源的分类
pytorch神经网络搭建 模板
2022年T电梯修理题库及模拟考试
Software testing needs more and more talents. Why do you still not want to take this path?
最长递增子序列及最优解、动物总重量问题
How to do the performance pressure test of "Health Code"
[godot] unity's animator is different from Godot's animplayer
2022 gas examination question bank and online simulation examination
Section 27 remote access virtual private network workflow and experimental demonstration
Difficulties in the development of knowledge map & the importance of building industry knowledge map
js解决浮点数相乘精度丢失问题
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
Applications and features of VR online exhibition
2022 polymerization process test questions and simulation test
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
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
Common interview questions ①
Fitness without equipment
[FTP] common FTP commands, updating continuously