当前位置:网站首页>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()
边栏推荐
- Thoughts on the construction of Meizhou cell room
- 2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
- 常用的Transforms中的方法
- Leecode question brushing record 1310 subarray XOR query
- 【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
- Dede collection plug-in does not need to write rules
- Pytorch(四) —— 可视化工具 Visdom
- TCP server communication flow
- How to use common datasets in pytorch
- Seven crimes of counting software R & D Efficiency
猜你喜欢
Introduction to JVM stack and heap
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
手动实现一个简单的栈
Odeint and GPU
OdeInt與GPU
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
神经网络-使用Sequential搭建神经网络
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
Maixll dock quick start
Pytorch(二) —— 激活函数、损失函数及其梯度
随机推荐
2022 question bank and answers for safety production management personnel of hazardous chemical production units
How to use maixll dock
Shell之Unix运维常用命令
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
Day 52 - tree problem
2022年T电梯修理题库及模拟考试
Pytest automated testing - compare robotframework framework
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
VIM简易使用教程
C - detailed explanation of operators and summary of use cases
Openresty rewrites the location of 302
One click shell to automatically deploy any version of redis
All in all, the low code still needs to solve these four problems
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
[pat (basic level) practice] - [simple simulation] 1064 friends
Seven crimes of counting software R & D Efficiency
扩展-Fragment
STM32扩展板 温度传感器和温湿度传感器的使用
pytorch中常用数据集的使用方法
2022 t elevator repair question bank and simulation test