user9155773
user9155773

Reputation:

sigmoid RuntimeWarning: overflow encountered in exp

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

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

Upvotes: 7

Views: 11348

Answers (1)

MB-F
MB-F

Reputation: 23637

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.

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)

Upvotes: 8

Related Questions