Input doesn't same dimension as tensor for result?

Thanks for the information!
Apparently the order of your nn.Sequential container was changed.

Your initial code snippet should work:

model = models.AlexNet()
encoder = list(model.children())[:-2]
encoder.append(nn.AdaptiveAvgPool2d(1))
encoder = nn.Sequential(*encoder)

but make sure you are not rewrapping this container somewhere.

Thanks for your reply!
Actuallt, my initial code is


It is just like yours, and I am so confused about the AdaptiveAvgPool2d(output_size=(6,6), So I looked at the alexnet model in pretrainedmodels, and found that there is a “self.avgpool = nn.AdaptiveAvgPool2d((6, 6))”, I just don’t know how it changes its order.

I’m not sure, what causes the error, but if you post the model definition as code (wrapping it in three backticks), we could have a look at it.

OK, the definition of alexnet is follows:
‘’’
class AlexNet(nn.Module):

def __init__(self, num_classes=1000):
    super(AlexNet, self).__init__()
    self.features = nn.Sequential(
        nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=3, stride=2),
        nn.Conv2d(64, 192, kernel_size=5, padding=2),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=3, stride=2),
        nn.Conv2d(192, 384, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(384, 256, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(256, 256, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=3, stride=2)

    )
    self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
    self.classifier = nn.Sequential(
        nn.Dropout(),
        nn.Linear(256 * 6 * 6, 4096),
        nn.ReLU(inplace=True),
        nn.Dropout(),
        nn.Linear(4096, 4096),
        nn.ReLU(inplace=True),
        nn.Linear(4096, num_classes),
    )

def forward(self, x):
    x = self.features(x)
    x = self.avgpool(x)
    x = x.view(x.size(0), 256 * 6 * 6)
    x = self.classifier(x)
    return x

def alexnet(pretrained=False, **kwargs):
r"""AlexNet model architecture from the
"One weird trick..." <https://arxiv.org/abs/1404.5997>_ paper.

Args:
    pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = AlexNet(**kwargs)
if pretrained:
    model.load_state_dict(model_zoo.load_url(model_urls['alexnet']))
return model

‘’’