RuntimeError:操作在f-string语句中没有标识



我正在评估pytorch模型。它以以下方式给出结果

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

然后我试着打印一些值来检查发生了什么

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
)
)

这导致错误

Exception has occurred: RuntimeError: operation does not have identity

更令人困惑的是,print只是在某些时候失败。为什么会失败?

我在代码中遇到了同样的问题。事实证明,当试图访问空张量的属性(例如形状、均值等(时,结果是无身份异常。

要复制的代码:

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

弄清楚为什么你的代码返回空张量,这将解决问题。

最新更新