简体   繁体   English

x[x?=x] 是什么意思?

[英]What does x[x!=x] mean?

I don't understand this line :我不明白这一行

lprobs[lprobs != lprobs] = torch.tensor(-math.inf).to(lprobs)

There is no comment, so is it some well-known Python (or PyTorch?) idiom?没有评论,那么它是一些众所周知的Python(或PyTorch?)成语吗? Could someone explain what it means, or show a different way that makes the intent clearer?有人可以解释它的含义,或者展示一种使意图更清晰的不同方式吗?

lprobs is a pytorch Tensor , and it could contain any size float type (I doubt this code is intended to support int or complex types). lprobs是一个pytorch Tensor ,它可以包含任何大小的浮点类型(我怀疑这段代码是为了支持 int 或复杂类型)。 As far as I know, the Tensor classes don't override the __ne__ function.据我所知,张量类不会覆盖__ne__ function。

It's a combination of fancy indexing with a boolean mask , and a "trick" (although intended by design) to check for NaN : x != x holds iff x is NaN (for floats, that is).它是花哨的索引与 boolean 掩码的组合,以及检查NaN“技巧” (尽管是设计意图): x != x成立,如果xNaN (即对于浮点数)。

They could alternatively have written他们也可以写

lprobs[torch.isnan(lprobs)] = torch.tensor(-math.inf).to(lprobs)

or, probably even more idiomatically, used torch.nan_to_num (but beware that the latter also has special behaviour towards infinities).或者,可能更惯用的是,使用torch.nan_to_num (但要注意后者对无穷大也有特殊行为)。

A non-updating variant of the above would be上述的非更新变体将是

torch.where(torch.isnan(lprobs), torch.tensor(-math.inf), lprobs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM