当前位置:网站首页>Neural networks - use sequential to build neural networks
Neural networks - use sequential to build neural networks
2022-07-01 04:46:00 【booze-J】
article
Let's take this neural network diagram as an example , To build a comparison to see the normal situation of building a neural network and using Sequential The difference between building Neural Networks , And some points needing attention in building neural network .
Under normal circumstances, build a neural network
Build neural network code :
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
class Booze(nn.Module):
def __init__(self):
super(Booze, self).__init__()
# 1. When building a network according to the network diagram , Some parameters are not given in the network diagram , It needs to be calculated by yourself , Like padding,stride wait
self.conv1 = Conv2d(3,32,5,padding=2)
self.maxpool1 = MaxPool2d(2)
self.conv2 = Conv2d(32,32,5,padding=2)
self.maxpool2 = MaxPool2d(2)
self.conv3 = Conv2d(32,64,5,padding=2)
self.maxpool3 = MaxPool2d(2)
self.flatten = Flatten()
# 2. When setting this linear layer in_feature and out_feature You may also need to do it yourself , This in_feature You can also print flatten Check it out.
self.linear1 = Linear(1024,64)
self.linear2 = Linear(64,10)
def forward(self,x):
x = self.conv1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.maxpool3(x)
x = self.flatten(x)
x = self.linear1(x)
x = self.linear2(x)
return x
obj = Booze()
print(obj)
'''3. A simple test of the network structure '''
input = torch.ones((64,3,32,32))
output = obj(input)
print(output.shape)
There are some points to note in the above code , It needs to be presented separately .
1. When building a network according to the network diagram , Some parameters are not given in the network diagram , It needs to be calculated by yourself , Like padding,stride wait
It's like building the first convolution layer , You need to calculate by yourself padding and stride. So how to calculate ? We need to use Official documents The calculation formula provided .
2. When building this linear layer in_feature You may also need to do it yourself , This in_feature You can also print flatten Check it out.
torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)
Flatten Can pass Official documents To use the .
# (batch_size,channels,H,W)=(32, 1, 5, 5)
input = torch.randn(32, 1, 5, 5)
# With default parameters
m = nn.Flatten()
output = m(input)
output.size()
# torch.Size([32, 25]) batch_size=32
# With non-default parameters
m = nn.Flatten(0, 2)
output = m(input)
output.size()
# torch.Size([160, 5]) batch_size=160
Use Sequential Building neural networks
Build neural network code :
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
class Booze(nn.Module):
def __init__(self):
super(Booze, self).__init__()
self.model1 = Sequential(
Conv2d(3, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self,x):
x = self.model1(x)
return x
obj = Booze()
print(obj)
''' A simple test of the network structure '''
input = torch.ones((64,3,32,32))
output = obj(input)
print(output.shape)
''' Visualize the network model '''
writer = SummaryWriter("logs")
writer.add_graph(obj,input)
writer.close()
There are also some points to note in the above code , It needs to be presented separately .
3. After setting up the network , A simple test of the network structure is needed
obj = Booze()
print(obj)
''' A simple test of the network structure '''
input = torch.ones((64,3,32,32))
output = obj(input)
print(output.shape)
Just like the code above , After the operation, no error will be reported .
4. After the network is set up , Yes, you can use tensorboard To visualize the network model
''' Visualize the network model '''
writer = SummaryWriter("logs")
writer.add_graph(obj,input)
writer.close()
It's used here add_graph This method , Please refer to Official documents , How to use it and add_images and add_scalar almost .
The results are as follows :
Specific differences
In fact, it is easy to see from the code .
Normal condition :
def __init__(self):
super(Booze, self).__init__()
self.conv1 = Conv2d(3,32,5,padding=2)
self.maxpool1 = MaxPool2d(2)
self.conv2 = Conv2d(32,32,5,padding=2)
self.maxpool2 = MaxPool2d(2)
self.conv3 = Conv2d(32,64,5,padding=2)
self.maxpool3 = MaxPool2d(2)
self.flatten = Flatten()
self.linear1 = Linear(1024,64)
self.linear2 = Linear(64,10)
def forward(self,x):
x = self.conv1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.maxpool3(x)
x = self.flatten(x)
x = self.linear1(x)
x = self.linear2(x)
return x
Sequential build :
class Booze(nn.Module):
def __init__(self):
super(Booze, self).__init__()
self.model1 = Sequential(
Conv2d(3, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self,x):
x = self.model1(x)
return x
```
边栏推荐
- 2022 polymerization process test questions and simulation test
- Cmake selecting compilers and setting compiler options
- Grey correlation cases and codes
- pytorch中常用数据集的使用方法
- Dede collection plug-in does not need to write rules
- Construction of Meizhou nursing laboratory: equipment configuration
- LeetCode_ 35 (search insertion position)
- 【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
- CF1638E. Colorful operations Kodori tree + differential tree array
- LeetCode_ 66 (plus one)
猜你喜欢

Question bank and answers for chemical automation control instrument operation certificate examination in 2022

STM32 extended key scan

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

分布式-总结列表

How to use common datasets in pytorch

2022年化工自动化控制仪表操作证考试题库及答案

Simple implementation of slf4j
![AssertionError assert I.ndim == 4 and I.shape[1] == 3](/img/b1/0109bb0f893eb4c8915df36c100907.png)
AssertionError assert I.ndim == 4 and I.shape[1] == 3

测量三相永磁同步电机的交轴直轴电感

Extension fragment
随机推荐
JS to solve the problem of floating point multiplication precision loss
Pytorch(四) —— 可视化工具 Visdom
Ten wastes of software research and development: the other side of research and development efficiency
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
神经网络-使用Sequential搭建神经网络
手动实现一个简单的栈
[FTP] common FTP commands, updating continuously
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
About the transmission pipeline of stage in spark
STM32扩展板 数码管显示
Difficulties in the development of knowledge map & the importance of building industry knowledge map
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
Use of dataloader
How to do the performance pressure test of "Health Code"
Shell之分析服务器日志命令集锦
【FTP】FTP常用命令,持续更新中……
Day 52 - tree problem
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
2022危险化学品生产单位安全生产管理人员题库及答案