当前位置:网站首页>pytorch神经网络搭建 模板
pytorch神经网络搭建 模板
2022-07-01 04:35:00 【booze-J】
我们就以这串代码来讲解神经网络搭建的流程
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)
# 注意dataset中transform参数接收的是个对象,所以要加上括号,还有就是之后使用神经网络进行运算的时候需要的数据类型是tensor类型,所以transforms参数要加上。
dataloader = DataLoader(dataset,batch_size=64)
# 搭建神经网络
class Booze(nn.Module):
# 继承nn.Module的初始化
def __init__(self):
super(Booze, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
# 重写forward函数
def forward(self,x):
output = self.maxpool1(x)
return output
# 生成一个神经网络对象
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()
搭建的流程大体上可以分为三部分:
- 1.数据的读取
- 2.搭建神经网络
- 3.使用tensorboard可视化
1.数据的读取
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# 注意dataset中transform参数接收的是个对象,所以要加上括号,还有就是之后使用神经网络进行运算的时候需要的数据类型是tensor类型,所以transforms参数要加上。
dataloader = DataLoader(dataset,batch_size=64)
2.搭建神经网络
# 搭建神经网络
class Booze(nn.Module):
# 继承nn.Module的初始化
def __init__(self):
super(Booze, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
# 重写forward函数
def forward(self,x):
output = self.maxpool1(x)
return output
继承nn.Module,继承nn.Module的初始化加上自己的初始化,重写forword方法。
3.使用tensorboard可视化
# 生成一个神经网络对象
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()
边栏推荐
- 什么是uid?什么是Auth?什么是验证器?
- Summary of acl2021 information extraction related papers
- Maixll dock quick start
- 2022 hoisting machinery command registration examination and hoisting machinery command examination registration
- Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
- VIM简易使用教程
- Cmake selecting compilers and setting compiler options
- 2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
- Odeint and GPU
- 2022年聚合工艺考试题及模拟考试
猜你喜欢

How to do the performance pressure test of "Health Code"
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends

扩展-Fragment

Pytorch(四) —— 可视化工具 Visdom

Basic usage, principle and details of session

VIM easy to use tutorial

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

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

VR线上展览所具备应用及特色

Leetcode learning - day 36
随机推荐
Kodori tree board
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
Basic exercise of test questions hexadecimal to decimal
One click shell to automatically deploy any version of redis
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
Advanced application of ES6 modular and asynchronous programming
2022 question bank and answers for safety production management personnel of hazardous chemical production units
TCP server communication flow
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
How to view the changes and opportunities in the construction of smart cities?
Cmake selecting compilers and setting compiler options
Knowledge supplement: redis' basic data types and corresponding commands
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Software testing needs more and more talents. Why do you still not want to take this path?
Basic usage, principle and details of session
VIM easy to use tutorial
Odeint et GPU
Offline installation of Wireshark 2.6.10
Learn Chapter 20 of vue3 (keep alive cache component)