Python 运行时警告:在较大时遇到无效值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37651803/
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
RuntimeWarning: invalid value encountered in greater
提问by Cheshie
I tried to implement soft-maxwith the following code (out_vec
is a numpy
vector of floats):
我尝试使用以下代码实现soft-max(out_vec
是numpy
浮点数向量):
numerator = np.exp(out_vec)
denominator = np.sum(np.exp(out_vec))
out_vec = numerator/denominator
However, I got an overflow error because of np.exp(out_vec)
. Therefore, I checked (manually) what the upper limit of np.exp()
is, and found that np.exp(709)
is a number, but np.exp(710)
is considered to be np.inf
. Thus, to try to avoid the overflow error, I modified my code as follows:
但是,由于np.exp(out_vec)
. 因此,我(手动)检查了上限是多少,np.exp()
发现np.exp(709)
是一个数字,但np.exp(710)
被认为是np.inf
。因此,为了尽量避免溢出错误,我修改了我的代码如下:
out_vec[out_vec > 709] = 709 #prevent np.exp overflow
numerator = np.exp(out_vec)
denominator = np.sum(np.exp(out_vec))
out_vec = numerator/denominator
Now, I get a different error:
现在,我得到一个不同的错误:
RuntimeWarning: invalid value encountered in greater out_vec[out_vec > 709] = 709
What's wrong with the line I added? I looked up this specific error and all I found is people's advice on how to ignore the error. Simply ignoring the error won't help me, because every time my code encounters this error it does not give the usual results.
我添加的行有什么问题?我查找了这个特定的错误,我发现的只是人们关于如何忽略错误的建议。简单地忽略该错误对我没有帮助,因为每次我的代码遇到此错误时,它都不会给出通常的结果。
回答by kvorobiev
Your problem is caused by the NaN
or Inf
elements in your out_vec
array. You could use the following code to avoid this problem:
您的问题是由数组中的NaN
orInf
元素引起的out_vec
。您可以使用以下代码来避免此问题:
if np.isnan(np.sum(out_vec)):
out_vec = out_vec[~numpy.isnan(out_vec)] # just remove nan elements from vector
out_vec[out_vec > 709] = 709
...
or you could use the following code to leave the NaN
values in your array:
或者您可以使用以下代码将NaN
值保留在数组中:
out_vec[ np.array([e > 709 if ~np.isnan(e) else False for e in out_vec], dtype=bool) ] = 709
回答by juerg
In my case the warning did not show up when calling this before the comparison (I had NaN values getting compared)
在我的情况下,在比较之前调用它时没有出现警告(我比较了 NaN 值)
np.warnings.filterwarnings('ignore')
回答by Ramin Barati
IMO the better way would be to use a more numerically stable implementation of sum of exponentials.
IMO 更好的方法是使用指数和的数值更稳定的实现。
from scipy.misc import logsumexp
out_vec = np.exp(out_vec - logsumexp(out_vec))
回答by Maryam Bahrami
If this happens because of your NaN value, then this might help:
如果发生这种情况是因为您的 NaN 值,那么这可能会有所帮助:
out_vec[~np.isnan(out_vec)] = out_vec[~np.isnan(out_vec)] > 709
This does the greater operation for none NaN values and the rest remains the same. If you need the rest to be False, then do this too:
这对无 NaN 值执行更大的操作,其余保持不变。如果你需要其余的都是假的,那么也这样做:
out_vec[np.isnan(out_vec)] = False