[英]nn.Linear should be mismatch, but it works successfully
我对nn.linear的特征感到困惑。 对于模型VGG-19的最后一个nn.MaxPool2d的out-feature,张量大小为(512,7,7)。 下面的模型使用池函数并将张量调整为(512,49),然后直接使用nn.linear(512,7)。 如果没有不匹配问题,为什么它不能成功运行?
'''VGG11/13/16/19 in Pytorch.'''
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
cfg = {
'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class VGG(nn.Module):
def __init__(self, vgg_name):
super(VGG, self).__init__()
self.features = self._make_layers(cfg[vgg_name])
self.classifier = nn.Linear(512, 7)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = F.dropout(out, p=0.5, training=self.training)
out = self.classifier(out)
return out
def _make_layers(self, cfg):
layers = []
in_channels = 3
for x in cfg:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
为什么假设这个代码有效? 我测试了它,并得到了以下形状,以及预期的大小不匹配错误。
def forward(self, x):
out = self.features(x) # torch.Size([1, 512, 7, 7])
out = out.view(out.size(0), -1) # torch.Size([1, 25088])
out = F.dropout(out, p=0.5, training=self.training) # torch.Size([1, 25088])
out = self.classifier(out) # RuntimeError: size mismatch, m1: [1 x 25088], m2: [512 x 7]
return out
您通过推断尺寸而犯的一个错误是您省略了批量维度。 这就是为什么你可能错误地断定out.view(out.size(0), -1)
的形状变化是[ out.view(out.size(0), -1)
] - > [ out.view(out.size(0), -1)
]而不是正确的[b, out.view(out.size(0), -1)
] ,7] - > [b,25088]其中b是批量大小。
正如预期的那样,当分类器改为
self.classifier = nn.Linear(25088, 7)
然后前进功能工作,没有大小不匹配错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.