# Bool value of Tensor with more than one value is ambiguous

**URL:** <https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049>\
**Category:** Uncategorized\
**Created:** [June 15, 2019, 6:29pm UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049 "2019-06-15T18:29:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ahmed](https://discuss.pytorch.org/u/ahmed)\
**Post date:** [June 15, 2019, 6:29pm UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/1 "2019-06-15T18:29:35Z")

</div>

I am trying to train my network for object detection in images using labels and bounding boxes. I have some images with no objects. For labels, the images are classified as ‘NONE’ and the box values are 0.

I want to write a script within my training class to calculate the loss based on only the labels if the box values are 0. if the sum of the boxes is not 0 then i want it to calculate the loss using both the box values and labels. This is the script that i have written:

```auto
num_epochs = 10
start_time = time.time()    
for epoch in range(num_epochs):
    model = net1.train()
    for batch_idx, (images, target) in enumerate(trainloader):
        images = images.to(device)
        labels = target['label'].to(device)
        bbox = target['box'].to(device)
        norm = target['norm_box'].to(device)
        outputs_labels, outputs_bbox = model(images)
        
        if sum(norm) == 0:
            print(norm)
            loss_label = criterion_label(outputs_labels, labels)
            oss = loss_label
        else:
            print(norm)
            loss_label = criterion_label(outputs_labels, labels)
            loss_bbox = criterion_bbox(outputs_bbox, norm)
            loss = loss_label + loss_bbox
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

```

But i keeping getting the following error:

```auto
RuntimeError: bool value of Tensor with more than one value is ambiguous

```

I believe it has something to do with my batch size, which is 4, where the box input for the network has 4 tensors for 4 sets of bounding box values.

For example, this is what a single batch looks like:

```auto
tensor([[0.66, -0.15, -0.93, -0.79],
        [-1.00, -0.14, -0.94, -0.84],
        [0.00, 0.00, 0.00, 0.00],
        [0.86, -0.13, -0.92, -0.75]], device='cuda:0')

```

Any advice would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![timesler](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/timesler/32/12734_2.png) [@timesler](https://discuss.pytorch.org/u/timesler)\
**Post date:** [June 17, 2019, 12:47am UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/2 "2019-06-17T00:47:44Z")

</div>

The error indicates that `sum(norm)` returns a tensor with multiple values. If you intend to sum all values in `norm`, use `torch.sum(norm)` or `norm.sum()`. Using the built-in `sum` function will only sum over a single dimension.

---

<div class="post-metadata">

**Author:** ![ahmed](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ahmed](https://discuss.pytorch.org/u/ahmed)\
**Post date:** [June 18, 2019, 12:05pm UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/3 "2019-06-18T12:05:26Z")

</div>

Thank you very much for your reply. I will let you know if I have any issues.

---

<div class="post-metadata">

**Author:** ![pinocchio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pinocchio/32/22093_2.png) [@pinocchio](https://discuss.pytorch.org/u/pinocchio)\
**Post date:** [February 13, 2020, 8:16pm UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/4 "2020-02-13T20:16:01Z")

</div>

what about this:

```auto
list(mdl.parameters()) == list(mdl0.parameters())

```

why do I get the same error?

```auto
RuntimeError: bool value of Tensor with more than one value is ambiguous

```

---

<div class="post-metadata">

**Author:** ![mathematics](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mathematics/32/40082_2.png) [@mathematics](https://discuss.pytorch.org/u/mathematics)\
**Post date:** [April 18, 2020, 7:29am UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/5 "2020-04-18T07:29:54Z")

</div>

Hi , @timesler

What are the possible reason’s and conditions that i can get this type of error?

`RuntimeError: bool value of Tensor with more than one value is ambiguous`

Please explain in simple code 🙂 . Thank you for answer

---

<div class="post-metadata">

**Author:** ![sharkdeng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sharkdeng/32/23021_2.png) [@sharkdeng](https://discuss.pytorch.org/u/sharkdeng)\
**Post date:** [April 28, 2020, 12:58am UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/6 "2020-04-28T00:58:39Z")

</div>

In my case, change nn.MSELoss(y\_true, y\_pred) to nn.MSELoss()(y\_true, y\_pred) works. Check whether there is a typo.

---

<div class="post-metadata">

**Author:** ![Medkne](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/medkne/32/50255_2.png) [@Medkne](https://discuss.pytorch.org/u/Medkne)\
**Post date:** [June 4, 2022, 11:02pm UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/7 "2022-06-04T23:02:31Z")

</div>

@sharkdeng Thank you very much !!!

---

<div class="post-metadata">

**Author:** ![Van-Vi\_Vo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/van-vi_vo/32/57101_2.png) [@Van-Vi\_Vo](https://discuss.pytorch.org/u/Van-Vi_Vo)\
**Post date:** [February 7, 2023, 6:14pm UTC](https://discuss.pytorch.org/t/bool-value-of-tensor-with-more-than-one-value-is-ambiguous/48049/8 "2023-02-07T18:14:02Z")

</div>

Thank you very muchhhh \<3
