I've got a 2d numpy array on which I want to use my function sigmoid(x) which is:
def sigmoid(x):
return 1 / (1 + np.exp(-x))
My problem is that I have inputs that are too big like 3000 and I get this warning:
RuntimeWarning: overflow encountered in exp
return 1 / (1 + np.exp(-x/8.))
I tried to just assign values to inputs over a certain number like 700 -> 1 and -700 -> 0, however, this is very slow because I obv have to loop over the whole array that way.
I have also looked into np.logandexp(x1, x2)
but I can't get it to work...
Edit: The datatype is float64 btw
You can use SciPy's expit()
function which is quite well behaved:
In [114]: from scipy.special import expit
# sample input array
In [115]: x = np.arange(50000, dtype=np.float64)
In [116]: sigm = expit(x)
# sanity check for no `np.inf`
In [117]: expit(70000.0)
Out[117]: 1.0