[英]Numpy doesn't throw FloatingPointError for dot product
I am taking the dot-product of two numpy arrays (both float32
).我正在计算两个 numpy 数组(都是float32
)的点积。 I am deliberately setting numpy to raise a FloatingPointError
in case of overflow.我故意设置 numpy 以在溢出的情况下引发FloatingPointError
。 However, dot
does not behave as expected: instead of raising a FloatingPointError
as it does when using ordinary multiplication, dot
returns inf
.但是, dot
行为并不如预期: dot
返回inf
,而不是像使用普通乘法时那样引发FloatingPointError
。
Is this the intended behavior?这是预期的行为吗? Is there a way to force dot
to raise an exception as well?有没有办法强制dot
也引发异常?
Minimal working example:最小工作示例:
import numpy as np
np.seterr(over="raise")
x = np.array([2e+38], dtype=np.float32)
y = np.array([10], dtype=np.float32)
x * y
>>> FloatingPointError: overflow encountered in multiply
np.dot(x,y)
>>> inf
Accepting @hpaulj's comment as the answer – matmul
does throw a FloatingPointError
as required, while dot
doesn't (no idea why).接受@hpaulj 的评论作为答案matmul
确实会根据需要抛出FloatingPointError
,而dot
不会(不知道为什么)。 For my purpose matmul
gives the same result as dot
.为了我的目的matmul
给出与dot
相同的结果。
I am taking the dot-product of two numpy arrays (both float32
).我正在使用两个numpy数组(均为float32
)的点积。 I am deliberately setting numpy to raise a FloatingPointError
in case of overflow.我故意将numpy设置为在溢出的情况下引发FloatingPointError
。 However, dot
does not behave as expected: instead of raising a FloatingPointError
as it does when using ordinary multiplication, dot
returns inf
.但是, dot
行为不符合预期: dot
返回inf
,而不是像使用普通乘法那样引发FloatingPointError
。
Is this the intended behavior?这是预期的行为吗? Is there a way to force dot
to raise an exception as well?有没有一种方法可以强制dot
引发异常?
Minimal working example:最小的工作示例:
import numpy as np
np.seterr(over="raise")
x = np.array([2e+38], dtype=np.float32)
y = np.array([10], dtype=np.float32)
x * y
>>> FloatingPointError: overflow encountered in multiply
np.dot(x,y)
>>> inf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.