sigmoid RuntimeWarning:exp中遇到溢出 [英] sigmoid RuntimeWarning: overflow encountered in exp

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

问题描述

我正在尝试在Python中创建S形函数,但是出现以下错误:

I'm trying to create a sigmoid function in Python, however, I get the following error:

RuntimeWarning: overflow encountered in exp

这是我的代码:

def sigmoid(self, value):

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

我搜索了以前的答案,但它们没有解决我的问题. 问题在于计算a的值. 我也尝试使用:

I searched for previous answers, but they didn't solve my problem. The problem is on calculating the value of 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))

我遇到以下错误:

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

我认为,如果发生溢出,我可以返回0,如果发生下溢,我可以返回1

I thought that if I have an overflow I could return 0, and if I have an underflow I could return 1

推荐答案

警告不是错误.您可以忽略它.

A warning is not an error. You could just ignore it.

也就是说,当exp(-value)的结果超过value的浮点数据类型格式可表示的最大数目时,就会发生这种情况.

That said, it happens when the result of exp(-value) exceeds the maximum number representable by value's floating point data type format.

您可以通过检查value是否太小来防止溢出:

You can prevent the overflow by checking if value is too small:

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)

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

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