简体   繁体   English

Pytorch RuntimeError:大小不匹配,m1:[1 x 7744],m2:[400 x 120]

[英]Pytorch RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120]

In a simple CNN that classifies 5 objects, I get a size mis-match error: 在分类5个对象的简单CNN中,出现大小不匹配错误:

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

my model.py file: 我的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()

Complete Error: 完成错误:

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

If you have a nn.Linear layer in your net, you cannot decide "on-the-fly" what the input size for this layer would be. 如果您的网络中有nn.Linear层,则无法“即时”确定该层的输入大小。
In your net you compute num_flat_features for every x and expect your self.fc1 to handle whatever size of x you feed the net. 在您的网络中,您为每个x计算num_flat_features ,并期望self.fc1能够处理您喂入网络的x任何大小。 However, self.fc1 has a fixed size weight matrix of size 400x120 (that is expecting input of dimension 16*5*5=400 and outputs 120 dim feature). 但是, self.fc1具有大小self.fc1固定大小权重矩阵(期望输入尺寸为16 * 5 * 5 = 400,并输出120暗淡特征)。 In your case the size of x translated to 7744 dim feature vector that self.fc1 simply cannot handle. 在您的情况下, x的大小转换为self.fc1无法处理的7744个self.fc1特征向量。

If you do want your network to be able to handle any size x , you can have a parameter-free interpolation layer resizing all x to the right size before self.fc1 : 如果确实希望网络能够处理任意大小的x ,则可以在无参数插值层中将所有x调整为self.fc1之前的正确大小:

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...

See torch.nn.functional.interpolate for more information. 有关更多信息,请参见torch.nn.functional.interpolate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初学者 PyTorch:运行时错误:大小不匹配,m1:[16 x 2304000],m2:[600 x 120] - Beginner PyTorch : RuntimeError: size mismatch, m1: [16 x 2304000], m2: [600 x 120] Pytorch GRU 错误 RuntimeError:尺寸不匹配,m1:[1600 x 3],m2:[50 x 20] - Pytorch GRU error RuntimeError : size mismatch, m1: [1600 x 3], m2: [50 x 20] RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136 - RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136 RuntimeError:大小不匹配,m1:[5 x 10],m2:[5 x 32] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp - RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp RuntimeError:尺寸不匹配,m1:[32 x 1],m2:[32 x 9] - RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9] RuntimeError:大小不匹配,m1:[28 x 28],m2:[784 x 128] - RuntimeError: size mismatch, m1: [28 x 28], m2: [784 x 128] 如何修复此 RuntimeError:大小不匹配,m1:[64 x 103],m2:[550 x 50] - How do I fix this RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50] Pytorch:尺寸不匹配错误,尽管矩阵的尺寸匹配(m1:[256 x 200],m2:[256 x 200]) - Pytorch: size mismatch error although the sizes of the matrices do match (m1: [256 x 200], m2: [256 x 200]) python 中的 CNN 模块给出错误大小不匹配,m1:[12288 x 26],m2:[12288 x 26] - CNN module in python gives error size mismatch, m1: [12288 x 26], m2: [12288 x 26] Pytorch vsion 大小不匹配,m1 - Pytorch vsion size mismatch, m1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM