Traceback (most recent call last): File "C:\Users\Lenovo\Desktop\DZY\DZY\CNN_mnist_yuanshi.py", line 177, in <module> train_loop(train_dataloader, model, loss_fn, optimizer, t, schedular) File "C:\Users\Lenovo\Desktop\DZY\DZY\CNN_mnist_yuanshi.py", line 114, in train_loop loss = loss_fn(outputs, y) File "D:\Program Files (x86)\py38\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl return forward_call(*args, **kwargs) File "D:\Program Files (x86)\py38\lib\site-packages\torch\nn\modules\loss.py", line 1174, in forward return F.cross_entropy(input, target, weight=self.weight, File "D:\Program Files (x86)\py38\lib\site-packages\torch\nn\functional.py", line 3029, in cross_entropy return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) TypeError: cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not tuple Process finished with exit code 1
时间: 2023-07-21 11:04:54 浏览: 190
TypeError: unsupported format string passed to NoneType.__format__
5星 · 资源好评率100%
这个错误是由于在调用`F.cross_entropy`函数时,传入的`target`参数是一个元组而不是一个张量。`F.cross_entropy`函数的`target`参数应该是一个包含目标类别的张量,而不是一个元组。
请检查你的代码,确保在调用`F.cross_entropy`函数时,传入的`target`参数是一个张量。如果`target`是一个元组,你可以使用`torch.Tensor`或`torch.tensor`将其转换为张量。
例如,如果`target`是一个长度为`n`的元组,你可以使用以下代码将其转换为张量:
```python
target = torch.tensor(target)
```
然后,将转换后的`target`张量传递给`F.cross_entropy`函数。这样应该可以解决这个错误。
阅读全文