【问题标题】:Pytorch RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120]Pytorch RuntimeError:大小不匹配,m1:[1 x 7744],m2:[400 x 120]
【发布时间】:2018-11-27 13:29:22
【问题描述】:

在一个对 5 个对象进行分类的简单 CNN 中,出现大小不匹配错误:

"RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120]" in the convolutional layer . 

我的 model.py 文件:

import torch.nn as nn
import torch.nn.functional as F

class FNet(nn.Module):


    def __init__(self,device):
        # make your convolutional neural network here
        # use regularization
        # batch normalization
        super(FNet, self).__init__()
        num_classes = 5
        self.conv1 = nn.Conv2d(3, 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, 5)

    def forward(self, x):

        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))

        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

if __name__ == "__main__":
    net = FNet()

完全错误:

Traceback (most recent call last):
  File "main.py", line 98, in <module>
    train_model('../Data/fruits/', save=True, destination_path='/home/mitesh/E yantra/task1#hc/Task 1/Task 1B/Data/fruits')
  File "main.py", line 66, in train_model
    outputs = model(images)
  File "/home/mitesh/anaconda3/envs/HC#850_stage1/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/mitesh/E yantra/task1#hc/Task 1/Task 1B/Code/model.py", line 28, in forward
    x = F.relu(self.fc1(x))
  File "/home/mitesh/anaconda3/envs/HC#850_stage1/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/mitesh/anaconda3/envs/HC#850_stage1/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/mitesh/anaconda3/envs/HC#850_stage1/lib/python3.6/site-packages/torch/nn/functional.py", line 1024, in linear
    return torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120] at /opt/conda/conda-bld/pytorch-cpu_1532576596369/work/aten/src/TH/generic/THTensorMath.cpp:2070

【问题讨论】:

    标签: python tensorflow machine-learning pytorch


    【解决方案1】:

    如果您的网络中有nn.Linear 层,您无法“即时”决定该层的输入大小。
    在您的网络中,您为每个 x 计算 num_flat_features 并期望您的 self.fc1 处理您提供网络的任何大小的 x。但是,self.fc1 有一个大小为 400x120 的固定大小权重矩阵(即期望输入尺寸为 16*5*5=400 并输出 120 暗淡特征)。在您的情况下,x 的大小转换为 self.fc1 根本无法处理的 7744 个暗淡特征向量。

    如果您确实希望您的网络能够处理任何大小的x,您可以在self.fc1 之前有一个无参数插值层将所有x 调整为正确的大小:

    x = F.max_pool2d(F.relu(self.conv2(x)), 2)  # output of conv layers
    x = F.interpolate(x, size=(5, 5), mode='bilinear')  # resize to the size expected by the linear unit
    x = x.view(x.size(0), 5 * 5 * 16)
    x = F.relu(self.fc1(x))  # you can go on from here...
    

    更多信息请参见torch.nn.functional.interpolate。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 2020-06-11
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多