当前位置:网站首页>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()
边栏推荐
- JS rotation chart
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- 2022 hoisting machinery command registration examination and hoisting machinery command examination registration
- Programs and processes, process management, foreground and background processes
- 2. Use of classlist (element class name)
- Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
- Pytorch(四) —— 可视化工具 Visdom
- [deep learning] (4) decoder mechanism in transformer, complete pytoch code attached
- Pytorch(三) —— 函数优化
- MySQL function variable stored procedure
猜你喜欢
![[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached](/img/ec/96e3188902e399f536ebca79042af9.png)
[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached

Kodori tree board

离线安装wireshark2.6.10

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

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

Grey correlation cases and codes

CF1638E colorful operations
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends

Pytorch(三) —— 函数优化
![[send email with error] 535 error:authentication failed](/img/58/8cd22fed1557077994cd78fd29f596.png)
[send email with error] 535 error:authentication failed
随机推荐
How to view the changes and opportunities in the construction of smart cities?
[leetcode skimming] February summary (updating)
Rule method: number of effective triangles
2022-02-15 (399. Division evaluation)
Summary of testing experience - Testing Theory
MySQL winter vacation self-study 2022 12 (5)
Possible problems and solutions of using scroll view to implement slider view
离线安装wireshark2.6.10
How to do the performance pressure test of "Health Code"
做网站数据采集,怎么选择合适的服务器呢?
The index is invalid
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
About the transmission pipeline of stage in spark
MySQL advanced -- you will have a new understanding of MySQL
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
软件研发的十大浪费:研发效能的另一面
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
slf4j 简单实现