Error(s) in loading state_dict for LeNet: size mismatch for conv1.weight: copying a param with shape torch.Size([10, 1, 5, 5]) from checkpoint, the shape in current model is torch.Size([6, 1, 5, 5]). size mismatch for conv1.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([6]). size mismatch for conv2.weight: copying a param with shape torch.Size([20, 10, 5, 5]) from checkpoint, the shape in current model is torch.Size([16, 6, 5, 5]). size mismatch for conv2.bias: copying a param with shape torch.Size([20]) from checkpoint, the shape in current model is torch.Size([16]). size mismatch for fc1.weight: copying a param with shape torch.Size([50, 320]) from checkpoint, the shape in current model is torch.Size([120, 256]). size mismatch for fc1.bias: copying a param with shape torch.Size([50]) from checkpoint, the shape in current model is torch.Size([120]). size mismatch for fc2.weight: copying a param with shape torch.Size([10, 50]) from checkpoint, the shape in current model is torch.Size([84, 120]). size mismatch for fc2.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([84]).
时间: 2024-02-02 10:15:29 浏览: 220
pytorch 状态字典:state_dict使用详解
5星 · 资源好评率100%
这个错误信息提示我们在加载预训练模型的权重时,发现模型中的部分参数形状与当前创建的模型不匹配,具体来说,就是:
- `conv1.weight`的形状应该为`(6, 1, 5, 5)`,但是预训练模型中的形状为`(10, 1, 5, 5)`;
- `conv1.bias`的形状应该为`(6,)`,但是预训练模型中的形状为`(10,)`;
- `conv2.weight`的形状应该为`(16, 6, 5, 5)`,但是预训练模型中的形状为`(20, 10, 5, 5)`;
- `conv2.bias`的形状应该为`(16,)`,但是预训练模型中的形状为`(20,)`;
- `fc1.weight`的形状应该为`(120, 256)`,但是预训练模型中的形状为`(50, 320)`;
- `fc1.bias`的形状应该为`(120,)`,但是预训练模型中的形状为`(50,)`;
- `fc2.weight`的形状应该为`(84, 120)`,但是预训练模型中的形状为`(10, 50)`;
- `fc2.bias`的形状应该为`(84,)`,但是预训练模型中的形状为`(10,)`。
这个问题通常是由于预训练模型的结构与当前创建的模型结构不一致导致的。解决方法是在当前模型中手动修改这些参数的形状,使其与预训练模型的结构一致。
阅读全文