【发布时间】: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