Search code examples
pythontorchf-stringtorchvision

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?


Solution

  • 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.