Python 中 scipy/numpy 中的 exp 溢出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4359959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overflow in exp in scipy/numpy in Python?
提问by
What does the following error:
以下错误是什么意思:
Warning: overflow encountered in exp
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:
在 scipy/numpy 中使用 Python 一般是什么意思?我正在计算对数形式的比率,即 log(a) + log(b),然后取结果的指数,使用 exp,并使用带有 logsumexp 的总和,如下所示:
c = log(a) + log(b)
c = c - logsumexp(c)
some values in the array b are intentionally set to 0. Their log will be -Inf.
数组 b 中的某些值有意设置为 0。它们的日志将为 -Inf。
What could be the cause of this warning? thanks.
出现此警告的原因可能是什么?谢谢。
采纳答案by Joe Kington
In your case, it means that bis verysmall somewhere in your array, and you're getting a number (a/bor 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.
根据你的情况,这意味着b是非常数组中的小地方,而你得到一个数字(a/b或exp(log(a) - log(b)))过大对任何D型(FLOAT32,float64等),你正在使用存储输出数组.
Numpy can be configured to
Numpy 可以配置为
- Ignore these sorts of errors,
- Print the error, but not raise a warning to stop the execution (the default)
- Log the error,
- Raise a warning
- Raise an error
- Call a user-defined function
- 忽略这些错误,
- 打印错误,但不发出警告以停止执行(默认)
- 记录错误,
- 发出警告
- 引发错误
- 调用用户定义的函数
See numpy.seterrto control how it handles having under/overflows, etc in floating point arrays.
请参阅numpy.seterr以控制它如何处理浮点数组中的下溢/溢出等。
回答by hughdbrown
Isn't exp(log(a) - log(b))the same as exp(log(a/b))which is the same as a/b?
不exp(log(a) - log(b))一样exp(log(a/b))与 相同a/b吗?
>>> from math import exp, log
>>> exp(log(100) - log(10))
10.000000000000002
>>> exp(log(1000) - log(10))
99.999999999999957
2010-12-07: If this is so "some values in the array b are intentionally set to 0", then you are essentially dividing by 0. That sounds like a problem.
2010-12-07:如果是这样“数组 b 中的某些值被有意设置为 0”,那么您实际上是在除以 0。这听起来像是一个问题。
回答by David Cournapeau
When you need to deal with exponential, you quickly go into under/over flow since the function grows so quickly. A typical case is statistics, where summing exponentials of various amplitude is quite common. Since the numbers are very big/smalls, one generally takes the log to stay in a "reasonable" range, the so-called log domain:
当您需要处理指数时,您很快就会进入欠流/过流,因为函数增长得如此之快。一个典型的例子是统计,其中对各种幅度的指数求和是很常见的。由于数字非常大/小,人们通常会将日志保持在“合理”范围内,即所谓的日志域:
exp(-a) + exp(-b) -> log(exp(-a) + exp(-b))
Problems still arise because exp(-a) will still underflows up. For example, exp(-1000) is already below the smallest number you can represent as a double. So for example:
问题仍然存在,因为 exp(-a) 仍然会下溢。例如,exp(-1000) 已经低于您可以表示为双精度的最小数字。例如:
log(exp(-1000) + exp(-1000))
gives -inf (log (0 + 0)), even though you can expect something like -1000 by hand (-1000 + log(2)). The function logsumexp does it better, by extracting the max of the number set, and taking it out of the log:
给出 -inf (log (0 + 0)),即使您可以手动预期 -1000 (-1000 + log(2))。函数 logsumexp 做得更好,通过提取数字集的最大值,并将其从日志中取出:
log(exp(a) + exp(b)) = m + log(exp(a-m) + exp(b-m))
It does not avoid underflow totally (if a and b are vastly different for example), but it avoids most precision issues in the final result
它不能完全避免下溢(例如,如果 a 和 b 有很大不同),但它避免了最终结果中的大多数精度问题
回答by intoo
I think you can use this method to solve this problem:
我觉得你可以用这个方法来解决这个问题:
Normalized
归一化
I overcome the problem in this method. Before using this method, the accuracy my classify is :86%. After using this method, the accuracy of my classify is :96%!!!
It's great!
first:
Min-Max scaling
我用这种方法克服了这个问题。在使用这种方法之前,我分类的准确率是:86%。使用这个方法后,我的分类准确率为:96%!!!这很棒!
第一:
最小-最大缩放
second:
Z-score standardization
第二:
Z-score标准化
These are common methods to implement normalization.
I use the first method. And I alter it. The maximum number is divided by 10.
So the maximum number of the result is 10. Then exp(-10) will be not overflow!
I hope my answer will help you !(^_^)
这些是常见的实现方法normalization。
我使用第一种方法。我改变它。最大数除以 10。所以结果的最大数是 10。那么 exp(-10) 将不是overflow!
希望我的回答对你有帮助!(^_^)
回答by sheikirfanbasha
In my case, it was due to large values in the data. I had to normalize (divide by 255, because my data was related to images) to get the values scaled down.
就我而言,这是由于数据中的大值造成的。我必须归一化(除以 255,因为我的数据与图像相关)以缩小值。


