在PyTorch中神经网络一节中有:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
中 self.fc1 = nn.Linear(16 * 5 * 5, 120),因为16*5*5恰好与卷积核的参数数目相等,故很容易被误解为参数数目,其实这里代表的是输入,至于为什么是16*5*5,我们看该章节中给出的例子:
在给出的例子中,一开始的input是32*32,经过两次卷积池化之后的大小就是16(卷积核)*5(宽)*5(高),然后再把它通过view后。全部输入到第一个全连接层的时候,input大小就是16*5*5。
PS:如若文中有理解有误,欢迎大家批评指正。