RuntimeWarning:遇到更大的无效值 [英] RuntimeWarning: invalid value encountered in greater

查看:127
本文介绍了RuntimeWarning:遇到更大的无效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码实现 soft-max (out_vecnumpy浮点数矢量):

I tried to implement soft-max with the following code (out_vec is a numpy vector of floats):

numerator = np.exp(out_vec)
denominator = np.sum(np.exp(out_vec))
out_vec = numerator/denominator

但是,由于np.exp(out_vec),我出现了溢出错误.因此,我(手动)检查了np.exp()的上限是多少,发现np.exp(709)是一个数字,但是np.exp(710)被认为是np.inf.因此,为了避免溢出错误,我对代码进行了如下修改:

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:

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.

推荐答案

您的问题是由out_vec数组中的NaNInf元素引起的.您可以使用以下代码来避免此问题:

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:

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
...

或者您可以使用以下代码将NaN值保留在数组中:

or you could use the following code to leave the NaN values in your array:

out_vec[ np.array([e > 709 if ~np.isnan(e) else False for e in out_vec], dtype=bool) ] = 709

这篇关于RuntimeWarning:遇到更大的无效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆