小编Sho*_*na 的帖子

运行时错误:mat1和mat2形状无法在pytorch中相乘

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

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
Run Code Online (Sandbox Code Playgroud)
model = CNN_Model()

out = model(imgs)
out
Run Code Online (Sandbox Code Playgroud)

当我尝试运行上面的代码时,出现错误 mat1 和 mat2 无法相乘。我已经尝试过针对类似问题发布的答案,但我的问题仍然没有解决。

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 …
Run Code Online (Sandbox Code Playgroud)

python artificial-intelligence deep-learning pytorch

3
推荐指数
1
解决办法
4万
查看次数