错误显示如下:
Traceback (most recent call last):
File "D:/CV/pytorch/VGG_Net/main.py", line 64, in <module>
outputs = net(data).to(device)
File "C:\Users\SuSu\.conda\envs\py37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\CV\pytorch\VGG_Net\vgg_original.py", line 59, in forward
x = self.classifier(x)
File "C:\Users\SuSu\.conda\envs\py37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\SuSu\.conda\envs\py37\lib\site-packages\torch\nn\modules\container.py", line 117, in forward
input = module(input)
File "C:\Users\SuSu\.conda\envs\py37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\SuSu\.conda\envs\py37\lib\site-packages\torch\nn\modules\linear.py", line 93, in forward
return F.linear(input, self.weight, self.bias)
File "C:\Users\SuSu\.conda\envs\py37\lib\site-packages\torch\nn\functional.py", line 1690, in linear
ret = torch.addmm(bias, input, weight.t())
RuntimeError: mat1 dim 1 must match mat2 dim 0
Process finished with exit code 1
一般在做图像分类训练模型的时候,遇到RuntimeError: mat1 dim 1 must match mat2 dim 0这个错误,一般都是由于模型定义的num_classes与数据集的分类种类数量不一致造成的。或者由于前向传播中通道数不匹配,需要把out_features和in_features的数值进行统一。