Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError: Operation does not have identity in f-string statement

I am evaluating a pytorch model. It gives results in following manner

results = model(batch)
# results is a list of dictionaries with 'boxes', 'labels' and 'scores' keys and torch tensor values

Then I try to print some of the values to check what is happening

print(
    (
        f"{results[0]['boxes'].shape[0]}\n" # Returns how many boxes there is
        f"{results[0]['scores'].mean()}" # Mean credibility score of the boxes
    )
)

This results in error

Exception has occurred: RuntimeError: operation does not have identity

To make things more confusing, print only fails some of the time. Why does this fail?

like image 952
vahvero Avatar asked Nov 23 '25 21:11

vahvero


1 Answers

I had the same problem in my code. It turns out when trying to access attributes of empty tensors (e.g. shape, mean, etc.) the outcome is the no identity exception.

Code to reproduce:

import torch

a = torch.arange(12)
mask = a > 100
b = a[mask]  # tensor([], dtype=torch.int64) -- empty tensor
b.min()  # yields "RuntimeError: operation does not have an identity."

Figure out why your code returns empty tensors and this will solve the problem.

like image 192
Shir Avatar answered Nov 26 '25 12:11

Shir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!