简体   繁体   English

sigmoid RuntimeWarning: exp 中遇到溢出

[英]sigmoid RuntimeWarning: overflow encountered in exp

I'm trying to create a sigmoid function in Python, however, I get the following error:我正在尝试在 Python 中创建一个 sigmoid 函数,但是,我收到以下错误:

RuntimeWarning: overflow encountered in exp

Here my code:这是我的代码:

def sigmoid(self, value):

    a = np.exp(-value)
    return 1.0/ (1.0 + a)

I searched for previous answers, but they didn't solve my problem.我搜索了以前的答案,但他们没有解决我的问题。 The problem is on calculating the value of a.问题在于计算 a 的值。 I also tried using:我也尝试使用:

a = np.float128(np.exp(-value))

but I got the same error, and using:但我遇到了同样的错误,并使用:

a = np.float256(np.exp(-value))

I got the following error:我收到以下错误:

AttributeError: 'module' object has no attribute 'float256'

I thought that if I have an overflow I could return 0, and if I have an underflow I could return 1我想如果我有溢出我可以返回 0,如果我有下溢我可以返回 1

A warning is not an error.警告不是错误。 You could just ignore it.可以忽略它。

That said, it happens when the result of exp(-value) exceeds the maximum number representable by value 's floating point data type format.也就是说,当exp(-value)的结果超过value的浮点数据类型格式可表示的最大数量时,就会发生这种情况。

You can prevent the overflow by checking if value is too small:您可以通过检查value是否太小来防止溢出:

def sigmoid(value):
    if -value > np.log(np.finfo(type(value)).max):
        return 0.0    
    a = np.exp(-value)
    return 1.0/ (1.0 + a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM