I have implemented the stable version of the sigmoid and yet I am getting overflow errors.
import numpy as np
f = lambda preds: (np.where(preds<0.,np.exp(preds)/(1+np.exp(preds)),1. / (1. + np.exp(-preds))))
arr = np.array([9999999999999999999999999999.,-99999999999999999999999999999999999.])
f(arr)
output:
<ipython-input-3-2b9a4fbf3d68>:3: RuntimeWarning: overflow encountered in exp
f = lambda preds: (np.where(preds<0.,np.exp(preds)/(1+np.exp(preds)),1. / (1. + np.exp(-preds))))
<ipython-input-3-2b9a4fbf3d68>:3: RuntimeWarning: invalid value encountered in true_divide
f = lambda preds: (np.where(preds<0.,np.exp(preds)/(1+np.exp(preds)),1. / (1. + np.exp(-preds))))
array([1., 0.])
What am I misunderstanding here? Is it that NumPy evaluates both the branches like tensorflow.cond in tensorflow 1 used to?
No such warnings here:
arr = np.array([9999999999999999999999999999.,-99999999999999999999999999999999999.])
for preds in arr:
if preds < 0.:
np.exp(preds)/(1+np.exp(preds))
else:
1. / (1. + np.exp(-preds))