当前位置:网站首页>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()
边栏推荐
- 洗个冷水澡吧
- How to use maixll dock
- LeetCode_35(搜索插入位置)
- 科研狗可能需要的一些工具
- AssertionError assert I.ndim == 4 and I.shape[1] == 3
- All in all, the low code still needs to solve these four problems
- LeetCode_ 58 (length of last word)
- Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
- 无器械健身
- Basic exercise of test questions hexadecimal to decimal
猜你喜欢

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

Openresty rewrites the location of 302

Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)

2022年煤气考试题库及在线模拟考试

VIM简易使用教程

Common methods in transforms

2022 polymerization process test questions and simulation test

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

Section 27 remote access virtual private network workflow and experimental demonstration

Pytorch(三) —— 函数优化
随机推荐
Technology sharing | broadcast function design in integrated dispatching
JS rotation chart
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Shell之Unix运维常用命令
C - detailed explanation of operators and summary of use cases
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
细数软件研发效能的七宗罪
LeetCode_66(加一)
Thoughts on the construction of Meizhou cell room
2022 gas examination question bank and online simulation examination
Codeforces Round #771 (Div. 2) ABCD|E
How to use maixll dock
Pytorch(三) —— 函数优化
最长递增子序列及最优解、动物总重量问题
Introduction to JVM stack and heap
This sideline workload is small, 10-15k, free unlimited massage
Leecode record 1351 negative numbers in statistical ordered matrix
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
Seven crimes of counting software R & D Efficiency
Why is Internet thinking not suitable for AI products?