当前位置:网站首页>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()
边栏推荐
- 一些小知识点
- Internet winter, how to spend three months to make a comeback
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- JS rotation chart
- How to choose the right server for website data collection?
- Why is Internet thinking not suitable for AI products?
- Annual inventory review of Alibaba cloud's observable practices in 2021
- Threejs opening
- What is uid? What is auth? What is a verifier?
- Section 27 remote access virtual private network workflow and experimental demonstration
猜你喜欢

Knowledge supplement: basic usage of redis based on docker

Maixll-Dock 快速上手

测量三相永磁同步电机的交轴直轴电感
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)

Concurrent mode of different performance testing tools

One job hopping up 8K, three times in five years

How to use maixll dock

Daily question - line 10

The design points of voice dialogue system and the importance of multi round dialogue
![[learn C and fly] S1E20: two dimensional array](/img/68/34fad73ff23d3e0719ef364fc60cb5.jpg)
[learn C and fly] S1E20: two dimensional array
随机推荐
Task04 mathematical statistics
All in all, the low code still needs to solve these four problems
OSPF notes [multiple access, two multicast addresses with OSPF]
[2020 overview] overview of link prediction based on knowledge map embedding
Annual inventory review of Alibaba cloud's observable practices in 2021
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
RDF query language SPARQL
CF1638E colorful operations
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
离线安装wireshark2.6.10
细数软件研发效能的七宗罪
This sideline workload is small, 10-15k, free unlimited massage
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
[pat (basic level) practice] - [simple simulation] 1064 friends
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
Simple implementation of slf4j
MySQL function variable stored procedure