pytorch神经网络之卷积层与全连接层参数的设置
2018-09-25 14:46
926 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zw__chen/article/details/82839061
当使用pytorch写网络结构的时候,本人发现在卷积层与第一个全连接层的全连接层的input_features不知道该写多少?一开始本人的做法是对着pytorch官网的公式推,但是总是算错。
后来发现,写完卷积层后可以根据模拟神经网络的前向传播得出这个。
全连接层的input_features是多少。首先来看一下这个简单的网络。这个卷积的Sequential本人就不再啰嗦了,现在看
nn.Linear(???, 4096)这个全连接层的第一个参数该为多少呢?请看下文详解。
class AlexNet(nn.Module): def __init__(self): super(AlexNet, self).__init__() self.conv = nn.Sequential( nn.Conv2d(3, 96, kernel_size=11, stride=4), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2) ) self.fc = nn.Sequential( nn.Linear(???, 4096) ...... ...... )
首先,我们先把forward写一下:
def forward(self, x): x = self.conv(x) print x.size()
就写到这里就可以了。其次,我们初始化一下网络,随机一个输入:
import torch from Alexnet.AlexNet import * from torch.autograd import Variable if __name__ == '__main__': net = AlexNet() data_input = Variable(torch.randn([1, 3, 96, 96])) # 这里假设输入图片是96x96 print data_input.size() net(data_input)
结果如下:
(1L, 3L, 96L, 96L) (1L, 256L, 1L, 1L)
显而易见,咱们这个全连接层的input_features为256。
阅读更多相关文章推荐
- 卷积神经网络参数设置
- pytorch:在网络中添加可训练参数,修改预训练权重文件
- caffe中使用draw_net.py绘制卷积神经网络结构图
- centos 设置网络参数正常无法连接外网问题
- 神经网络架构PYTORCH-宏观分析
- 神经网络架构PYTORCH-前馈神经网络
- PyTorch笔记4-快速构建神经网络(NN)
- pytorch + visdom 应用神经网络、CNN 处理手写字体分类
- PyTorch快速入门教程三(神经网络)
- 利用tensorflow实现神经网络卷积层、池化层、全连接层
- 神经网络参数设置注意事项
- 神经网络matlab工具箱有关参数设置
- 卷积神经网络参数说明
- PyTorch中使用预训练的模型初始化网络的一部分参数
- 神经网络学习之参数的设置原则
- 如何利用神经网络做回归问题(全连接以及一维卷积)
- pytorch中的pre-train函数模型或者旧的模型的引用及修改(增减网络层,修改某层参数等) finetune微调等
- JVM通过代理服务器连接网络的参数设置
- PyTorch快速搭建神经网络及其保存提取方法详解
- 神经网络参数设置注意事项