Bool value of Tensor with more than one value is ambiguous

ahmed: <p>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.</p>
<p>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:</p>
<pre><code class="lang-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()
</code></pre>
<p>But i keeping getting the following error:</p>
<pre><code class="lang-auto">RuntimeError: bool value of Tensor with more than one value is ambiguous
</code></pre>
<p>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.</p>
<p>For example, this is what a single batch looks like:</p>
<pre><code class="lang-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')
</code></pre>
<p>Any advice would be greatly appreciated.</p>

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

ahmed: <p>Thank you very much for your reply. I will let you know if I have any issues.</p>

pinocchio: <p>what about this:</p>
<pre><code class="lang-auto">list(mdl.parameters()) == list(mdl0.parameters())
</code></pre>
<p>why do I get the same error?</p>
<pre><code class="lang-auto">RuntimeError: bool value of Tensor with more than one value is ambiguous
</code></pre>

mathematics: <p>Hi ,  <a class="mention" href="/u/timesler">@timesler</a></p>
<p>What are the possible reason’s and conditions that i can get this type of error?</p>
<p><code>RuntimeError: bool value of Tensor with more than one value is ambiguous</code></p>
<p>Please explain in simple code <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> . Thank you for answer</p>

sharkdeng: <p>In my case, change nn.MSELoss(y_true, y_pred) to nn.MSELoss()(y_true, y_pred) works. Check whether there is a typo.</p>

Medkne: <p><a class="mention" href="/u/sharkdeng">@sharkdeng</a>  Thank you very much !!!</p>

Van-Vi_Vo: <p>Thank you very muchhhh &lt;3</p>