当前位置:网站首页>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()
边栏推荐
- Advanced application of ES6 modular and asynchronous programming
- JS rotation chart
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- Daily question - line 10
- TASK04|数理统计
- 2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
- The index is invalid
- RDF query language SPARQL
- 1. Mobile terminal touch screen event
- MySQL winter vacation self-study 2022 12 (5)
猜你喜欢

2022 polymerization process test questions and simulation test

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点

Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

Pytest automated testing - compare robotframework framework

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis

Grey correlation cases and codes

Odeint et GPU
![[learn C and fly] S1E20: two dimensional array](/img/68/34fad73ff23d3e0719ef364fc60cb5.jpg)
[learn C and fly] S1E20: two dimensional array

2022年煤气考试题库及在线模拟考试
随机推荐
一些小知识点
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
How to choose the right server for website data collection?
MySQL function variable stored procedure
One click shell to automatically deploy any version of redis
Maixll dock quick start
扩展-Fragment
Why is Internet thinking not suitable for AI products?
Software testing needs more and more talents. Why do you still not want to take this path?
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
The design points of voice dialogue system and the importance of multi round dialogue
Rule method: number of effective triangles
2022 t elevator repair question bank and simulation test
Daily question - line 10
Internet winter, how to spend three months to make a comeback
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
软件研发的十大浪费:研发效能的另一面
2022年上海市安全员C证考试题模拟考试题库及答案
[pat (basic level) practice] - [simple simulation] 1064 friends
TASK04|數理統計