当前位置:网站首页>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()
边栏推荐
- Summary of acl2021 information extraction related papers
- Take a cold bath
- VIM easy to use tutorial
- C - detailed explanation of operators and summary of use cases
- Question bank and answers for chemical automation control instrument operation certificate examination in 2022
- 神经网络-使用Sequential搭建神经网络
- [FTP] the solution to "227 entering passive mode" during FTP connection
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- Common UNIX Operation and maintenance commands of shell
- VIM简易使用教程
猜你喜欢

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

分布式锁的实现

This sideline workload is small, 10-15k, free unlimited massage

Shell之一键自动部署Redis任意版本

OdeInt与GPU

LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver

Applications and features of VR online exhibition

How to do the performance pressure test of "Health Code"

分布式全局唯一ID解决方案详解

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
随机推荐
Leecode records the number of good segmentation of 1525 strings
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
如何看待智慧城市建设中的改变和机遇?
Common interview questions ①
Introduction to JVM stack and heap
Pytorch(二) —— 激活函数、损失函数及其梯度
STM32 photoresistor sensor & two channel AD acquisition
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
【FTP】FTP连接时出现“227 Entering Passive Mode”的解决方法
Shell之一键自动部署Redis任意版本
2022-02-15 (399. Division evaluation)
神经网络-非线性激活
Overview of the construction details of Meizhou veterinary laboratory
Leecode record 1351 negative numbers in statistical ordered matrix
pytorch中常用数据集的使用方法
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
How to use maixll dock
Kodori tree board