简体   繁体   English

为什么 `np.sum([-np.Inf, +np.Inf])` 警告“在 reduce 中遇到无效值”

[英]Why does `np.sum([-np.Inf, +np.Inf])` warn about "invalid value encountered in reduce"

python -c "import numpy as np; print(np.sum([-np.Inf, +np.Inf]))" 

gives

numpy\core\fromnumeric.py:86: RuntimeWarning: invalid value encountered in reduce
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
nan

I wonder why that is:我不知道这是为什么:

  1. There is no warning in里面没有警告

    python -c "import numpy as np; print(np.sum([-np.Inf, -np.Inf]))"

    nor in也不在

    python -c "import numpy as np; print(np.sum([+np.Inf, +np.Inf]))"

    so it can't be the Inf s.所以它不可能是Inf s。

  2. There is no warning in里面没有警告

    python -c "import numpy as np; print(np.sum([np.nan, np.nan]))"

    so it can't be the NaN result.所以它不可能是NaN结果。

What is it, then, and how can I avoid it?那么,它是什么,我该如何避免呢? I actually like getting NaN as a result, I just want to avoid the warning.我实际上喜欢得到NaN结果,我只是想避免警告。

The warning is fine, because Inf - Inf is mathematically undefined.警告很好,因为Inf - Inf在数学上未定义。 What result would you expect?你期望什么结果?

If you want to avoid the warning, use a filter as follows:如果您想避免警告,请使用如下过滤器:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=RuntimeWarning)
    res = np.sum([-np.Inf, np.Inf])

It turns out the answer by @CarlosHorn is pretty close, although hidden deep inside the IEEE standard 754 (I checked the 2008 version).事实证明,@CarlosHorn 的答案非常接近,尽管隐藏在 IEEE 标准 754 的深处(我检查了 2008 版本)。

Section 7.2 (Default exception handling > Invalid operation) writes第 7.2 节(默认异常处理 > 无效操作)写道

The invalid operation exception is signaled if and only if there is no usefully definable result .当且仅当没有有用的可定义结果时,才会发出无效操作异常信号。

I wouldn't know what a "usefully definable result" might be, considering that some people may not find Inf useful;考虑到有些人可能觉得Inf没有用,我不知道什么是“有用的可定义结果”; I, by contrast, find even NaN pretty useful.相比之下,我发现甚至NaN也很有用。 Anyay, the section gives a comprehensive list of examples, which includes (in d) the "magnitude subtraction of infinities", explaining why and form of Inf - Inf should be considered invalid.不管怎样,本节给出了一个完整的示例列表,其中包括(在 d 中)“无穷大的减法”,解释了 Inf 的原因和形式Inf - Inf应被视为无效。 It does also include (in a) "any [...] operation on a signaling NaN", but does not include operations on a quiet NaN.它还包括(在 a 中)“对信号 NaN 的任何 [...] 操作”,但不包括对安静 NaN 的操作。 This important distinction explains why NaN + NaN usually does not signal, as np.nan is quiet.这个重要的区别解释了为什么NaN + NaN通常不发出信号,因为np.nan是安静的。

For completeness, section 6.1 explains why Inf + Inf should not be considered invalid.为了完整起见,第 6.1 节解释了为什么Inf + Inf不应被视为无效。

Two things left to says:还有两件事要说:

  • It is unclear (yet irrelevant) to me why np.inf - np.inf does not raise an exception.我不清楚(但无关紧要)为什么np.inf - np.inf不会引发异常。
  • with np.errstate(invalid="ignore"): ... is probably the cleanest way to suppress the warning. with np.errstate(invalid="ignore"): ...可能是抑制警告的最干净的方法。

More resources:更多资源:

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

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