numpy中的exp溢出了吗

numpy中的exp溢出了吗

本文介绍了python中scipy/numpy中的exp溢出了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下错误是什么?

Warning: overflow encountered in exp

在scipy/numpy中使用Python通常是什么意思?我正在以对数形式计算比率,即log(a)+ log(b),然后使用exp和logumexp的总和来取结果的指数,如下所示:

in scipy/numpy using Python generally mean? I'm computing a ratio in log form, i.e. log(a) + log(b) and then taking the exponent of the result, using exp, and using a sum with logsumexp, as follows:

c = log(a) + log(b)
c = c - logsumexp(c)

数组b中的某些值被有意地设置为0.它们的日志为-Inf.

some values in the array b are intentionally set to 0. Their log will be -Inf.

此警告可能是什么原因?谢谢.

What could be the cause of this warning? thanks.

推荐答案

在您的情况下,这意味着b在数组中的某个地方非常小,并且您得到一个数字( a/bexp(log(a) - log(b)))对于您用于存储输出的数组的任何dtype(float32,float64等)都太大.

In your case, it means that b is very small somewhere in your array, and you're getting a number (a/b or exp(log(a) - log(b))) that is too large for whatever dtype (float32, float64, etc) the array you're using to store the output is.

Numpy可以配置为

Numpy can be configured to

  1. 忽略这些错误,
  2. 打印错误,但不发出警告以停止执行(默认)
  3. 记录错误,
  4. 提出警告
  5. 引发错误
  6. 调用用户定义的函数

请参见 numpy.seterr 以控制它的方式处理浮点数组中的下溢/上溢等.

See numpy.seterr to control how it handles having under/overflows, etc in floating point arrays.

这篇关于python中scipy/numpy中的exp溢出了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:09