我在使用 CIFAR-100 数据集在 CPU 上尝试训练模型时,遇到了以下错误:
Traceback (most recent call last): File "recog.py", line 68, in <module> loss = criterion(outputs, labels) File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in _call_impl return forward_call(*input, **kwargs) File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py", line 1152, in forward label_smoothing=self.label_smoothing) File "/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py", line 2846, in cross_entropy return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)IndexError: Target 32 is out of bounds.
我从这里截取了一段代码并稍作修改。代码如下:
另外,目标数字每次都不同。我使用的是 Python 3.9 和最新的 Pytorch。当我尝试使用 CIFAR-10 数据集做同样的事情时,运行得很完美。我现在卡住了。请帮助我。
回答:
您的模型只预测了10个类别。CIFAR100有100个类别。
将
self.fc3 = nn.Linear(84, 10)
改为
self.fc3 = nn.Linear(84, 100)