【问题标题】:Runtime Error: mat1 and mat2 shapes cannot be multiplied in pytorch运行时错误:mat1 和 mat2 形状不能在 pytorch 中相乘
【发布时间】:2021-10-30 09:00:56
【问题描述】:

我是深度学习的新手,我使用以下代码创建了一个模型来预测植物病害

class CNN_Model(nn.Module):
  def __init__(self):
    super(CNN_Model, self).__init__()
    self.cnn_model = nn.Sequential(
        nn.Conv2d(3, 16, 3),    
        nn.ReLU(),
        nn.MaxPool2d(2, 2),     
        nn.Conv2d(16, 32, 5),   
        nn.ReLU(),
        nn.MaxPool2d(2, 2),     
    )

    self.fc_model = nn.Sequential(
        nn.Flatten(),           
        nn.Linear(800, 300),    
        nn.ReLU(),
        nn.Linear(300, 38),     
        nn.Softmax(dim=1)
    )

  def forward(self, x):
      x = self.cnn_model(x)
      x = self.fc_model(x)

      return x
model = CNN_Model()

out = model(imgs)
out

当我尝试运行上述代码时,我收到错误 mat1 and mat2 cannot be multiplied。我已经尝试过发布类似问题的答案,但我的问题仍然没有解决。

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_66/1768380315.py in <module>
----> 1 out = model(imgs)
      2 out

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

/tmp/ipykernel_66/1577403502.py in forward(self, x)
     26   def forward(self, x):
     27       x = self.cnn_model(x)
---> 28       x = self.fc_model(x)
     29 
     30       return x

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
    137     def forward(self, input):
    138         for module in self:
--> 139             input = module(input)
    140         return input
    141 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
     94 
     95     def forward(self, input: Tensor) -> Tensor:
---> 96         return F.linear(input, self.weight, self.bias)
     97 
     98     def extra_repr(self) -> str:

/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1845     if has_torch_function_variadic(input, weight):
   1846         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1847     return torch._C._nn.linear(input, weight, bias)
   1848 
   1849 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x119072 and 800x300)

请有人帮我解决这些错误。

【问题讨论】:

  • 正如错误提示的那样,问题在于矩阵的大小(32x119072 和 800x300),当第一个的行数不等于第二。确保您的所有计算都是正确的,并且输入的图片尺寸正确

标签: python deep-learning pytorch artificial-intelligence


【解决方案1】:

大小不匹配错误显示为32x119072 and 800x300。第一个形状是指输入张量,而第二个是层的参数。如果您查看您的模型定义,您会发现它与第一个完全连接的层相匹配,即在 flatten 之后的那个。事实上,nn.Linear(800, 300) 期待 800-feature 张量,但得到了 119072-feature 张量。

您需要修改此线性层以匹配传入的张量扁平空间形状。但请注意,这个值将如何取决于输入到 CNN 的图像,最终这将决定输入到分类器的张量的大小。解决这个问题的一般方法是使用自适应层:例如nn.AdaptiveMaxPool2d,无论输入尺寸大小如何,它始终提供相同的输出形状。

【讨论】:

  • 请告诉我在哪里更改
  • 我说你的代码有什么问题。请花一些时间来理解答案,只有这样你才能找到问题所在。 确实,nn.Linear(800, 300) 期望有 800 个特征张量,但得到了 119072 个特征张量。...你有什么需要改变的吗?
猜你喜欢
  • 2022-07-15
  • 2021-09-09
  • 2022-08-22
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2021-09-23
  • 2023-01-28
  • 1970-01-01
相关资源
最近更新 更多