# Got RuntimeError: Boolean value of Tensor with more than one value is ambiguous during training

**URL:** <https://discuss.pytorch.org/t/got-runtimeerror-boolean-value-of-tensor-with-more-than-one-value-is-ambiguous-during-training/133762>\
**Category:** vision\
**Created:** [October 8, 2021, 2:35pm UTC](https://discuss.pytorch.org/t/got-runtimeerror-boolean-value-of-tensor-with-more-than-one-value-is-ambiguous-during-training/133762 "2021-10-08T14:35:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/james_lee/32/29487_2.png) [@James\_Lee](https://discuss.pytorch.org/u/James_Lee)\
**Post date:** [October 8, 2021, 2:35pm UTC](https://discuss.pytorch.org/t/got-runtimeerror-boolean-value-of-tensor-with-more-than-one-value-is-ambiguous-during-training/133762/1 "2021-10-08T14:35:39Z")

</div>

Hello. I’m trying to set ignore\_index for labels during training with CE loss, my code seemed as follows,

```
    for i, data in enumerate(train_loader):
        labels[labels > 0.9] = 1
        labels[labels < 0.1] = 0

        # bug occurred here
        labels[0.1<=labels<=0.9] = -1

        loss = nn.CrossEntropyLoss(out, labels, ignore_index=-1)

```

I got RuntimeError: Boolean value of Tensor with more than one value is ambiguous  
during training.

What’s wrong here? Thanks.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 9, 2021, 12:59am UTC](https://discuss.pytorch.org/t/got-runtimeerror-boolean-value-of-tensor-with-more-than-one-value-is-ambiguous-during-training/133762/2 "2021-10-09T00:59:20Z")

</div>

You have to combine the two conditions via `&`:

```python
labels[(0.1<=labels) & (labels<=0.9)]

```

---

<div class="post-metadata">

**Author:** ![James\_Lee](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/james_lee/32/29487_2.png) [@James\_Lee](https://discuss.pytorch.org/u/James_Lee)\
**Post date:** [October 9, 2021, 6:42am UTC](https://discuss.pytorch.org/t/got-runtimeerror-boolean-value-of-tensor-with-more-than-one-value-is-ambiguous-during-training/133762/3 "2021-10-09T06:42:01Z")

</div>

@ptrblck Thanks for your prompt reply. After fix this problem, when I ran CUDA\_LAUNCH\_BLOCKING=1 python train.py I got the following error,

/opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn\_SpatialClassNLLCriterion\_updateOutput\_kernel: block: [0,0,0], thread: [342,0,0] Assertion `t >= 0 && t < n_classes` failed.  
/opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn\_SpatialClassNLLCriterion\_updateOutput\_kernel: block: [0,0,0], thread: [343,0,0] Assertion `t >= 0 && t < n_classes` failed.  
/opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn\_SpatialClassNLLCriterion\_updateOutput\_kernel: block: [0,0,0], thread: [344,0,0] Assertion `t >= 0 && t < n_classes` failed.  
/opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn\_SpatialClassNLLCriterion\_updateOutput\_kernel: block: [0,0,0], thread: [345,0,0] Assertion `t >= 0 && t < n_classes` failed.  
/opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn\_SpatialClassNLLCriterion\_updateOutput\_kernel: block: [0,0,0], thread: [346,0,0] Assertion `t >= 0 && t < n_classes` failed.  
/opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn\_SpatialClassNLLCriterion\_updateOutput\_kernel: block: [0,0,0], thread: [347,0,0] Assertion `t >= 0 && t < n_classes` failed.  
Traceback (most recent call last):  
File “train.py”, line 238, in   
main()  
File “train.py”, line 126, in main  
train(net, optimizer)  
File “train.py”, line 197, in train  
loss1 = criterion\_CE(out, torch.squeeze(labels).long())  
File “/home/public/software/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 722, in \_call\_impl  
result = self.forward(\*input, \*\*kwargs)  
File “/home/public/software/anaconda3/lib/python3.8/site-packages/torch/nn/modules/loss.py”, line 947, in forward  
return F.cross\_entropy(input, target, weight=self.weight,  
File “/home/public/software/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py”, line 2422, in cross\_entropy  
return nll\_loss(log\_softmax(input, 1), target, weight, None, ignore\_index, None, reduction)  
File “/home/public/software/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py”, line 2220, in nll\_loss  
ret = torch.\_C.\_nn.nll\_loss2d(input, target, weight, \_Reduction.get\_enum(reduction), ignore\_index)  
RuntimeError: cuda runtime error (710) : device-side assert triggered at /opt/conda/conda-bld/pytorch\_1595629395347/work/aten/src/THCUNN/generic/SpatialClassNLLCriterion.cu:134

It seemed that I should use nn.BCE rather than nn.CrossEntropyLoss here? I found only CrossEntropyLoss support ignore\_index for classification while nn.BCE didn’t?

Here’s my loss,

```
        criterion_CE = nn.CrossEntropyLoss(ignore_index=-1).cuda()
        loss = criterion_CE(out, torch.squeeze(labels).long())
```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 9, 2021, 10:00am UTC](https://discuss.pytorch.org/t/got-runtimeerror-boolean-value-of-tensor-with-more-than-one-value-is-ambiguous-during-training/133762/4 "2021-10-09T10:00:17Z")

</div>

Answered in your [cross-post](https://discuss.pytorch.org/t/assertion-t-0-t-n-classes-failed-error/133794/2).
