如何解决运行时错误:操作在 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."
弄清楚为什么你的代码返回空张量,这将解决问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。