pytorch Assertion `t 」= 0 && t 「 n_classes failed 解决方案

这种错误主要是在设置的 n_class 时有误。例如在CNN最后的线性层的输出num设置有误。这样在做loss计算的时候,由于需要将 label 转换成 one-hot 向量,但是one-hot向量的维度是根据你的 pred 的维度转换成相应的维度,同时将 label 值对应位置的 0 置 1,但如果在线性层的输出的 num_class 的设置时错了,例如设置成小于实际的 label 的class 的值,label 对应的index 位置不存在在 one-hot 向量中,所有就会出现 t < n_classes failed 的情况,同理, t>=0 表示设置的 num_class 应该是要大于0的。

[train epoch 0] loss: 1.626, acc: 0.125: 0%|▏ | 1/368 [00:00<05:16, 1.16it/s]../aten/src/ATen/native/cuda/Loss.cu:250: nll_loss_forward_reduce_cuda_kernel_2d: block: [0,0,0], thread: [1,0,0] Assertion `t >= 0 && t < n_classes` failed. ../aten/src/ATen/native/cuda/Loss.cu:250: nll_loss_forward_reduce_cuda_kernel_2d: block: [0,0,0], thread: [6,0,0] Assertion `t >= 0 && t < n_classes` failed. [train epoch 0] loss: 1.626, acc: 0.125: 0%|▏ | 1/368 [00:00<06:02, 1.01it/s] Traceback (most recent call last): File "/root/bayes-tmp/VITransformer/train.py", line 139, in <module> main(opt) File "/root/bayes-tmp/VITransformer/train.py", line 92, in main train_loss, train_acc = train_one_epoch(model=model, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/bayes-tmp/VITransformer/utils.py", line 135, in train_one_epoch loss = loss_function(pred, labels.to(device)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.12/site-packages/torch/nn/modules/loss.py", line 1293, in forward return F.cross_entropy( ^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.12/site-packages/torch/nn/functional.py", line 3479, in cross_entropy return torch._C._nn.cross_entropy_loss( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: CUDA error: device-side assert triggered Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
最新发布
04-24
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值