use*_*814 1 python-3.x conv-neural-network pytorch
我正在尝试训练 CNNFashion-MNIST
使用Conv2d
、Maxpool
和Linear
层对数据中的图像进行分类。我in_features = 12*4*4
在nn.Linear
层中遇到了如下所述的代码。
我能否获得有关如何in_features
为 nn.Linear 层选择参数的帮助?
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5)
self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
self.fc2 = nn.Linear(in_features=120, out_features=60)
self.out = nn.Linear(in_features=60, out_features=10)
Run Code Online (Sandbox Code Playgroud)
Fashion-MNIST
数据集中的每个示例都是一个28 x 28
灰度图像。
28 x 28
5 x 5
没有填充的情况下进行卷积(因为default padding=0
)并且stride=1(by default)
,所以我们2
在每一侧丢失像素,我们下降到24 x 24
,即(28-5)/1 + 12 x 2
,我们将每个维度减半,减少到12 x 12
5 x 5
没有padding
and 的卷积stride=1
,我们下降到8 x 8
,即 (12-5)/1 + 14 x 4
这就是为什么,self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
。基本上,n_features_conv * height * width
高度和宽度分别4
为 ,n_features_conv
并且与位于其正上方out_channels
的conv2D
图层相同。
请注意,如果您更改输入图像的大小,则必须执行上述计算并相应地调整第一Linear
层。
希望这对你有帮助!
归档时间: |
|
查看次数: |
3711 次 |
最近记录: |