Issue
I want to apply a function to a numpy
array, which goes through infinity to arrive at the correct values:
def relu(x):
odds = x / (1-x)
lnex = np.log(np.exp(odds) + 1)
return lnex / (lnex + 1)
x = np.linspace(0,1,10)
np.where(x==1,1,relu(x))
correctly computes
array([0.40938389, 0.43104202, 0.45833921, 0.49343414, 0.53940413,
0.60030842, 0.68019731, 0.77923729, 0.88889303, 1. ])
but also issues warnings:
3478817693.py:2: RuntimeWarning: divide by zero encountered in divide
odds = x / (1-x)
3478817693.py:4: RuntimeWarning: invalid value encountered in divide
return lnex / (lnex + 1)
How do I avoid the warnings?
Please note that performance is of critical importance here, so I would rather avoid creating intermediate arrays.
Solution
Another possible solution, based on np.divide
, to avoid division by zero. This solution is inspired by @hpaulj's comment.
def relu(x):
odds = np.divide(x, 1-x, out=np.zeros_like(x), where=x!=1)
lnex = np.log(np.exp(odds) + 1)
return lnex / (lnex + 1)
x = np.linspace(0,1,10)
np.where(x==1,1,relu(x))
Output:
array([0.40938389, 0.43104202, 0.45833921, 0.49343414, 0.53940413,
0.60030842, 0.68019731, 0.77923729, 0.88889303, 1. ])
Answered By - PaulS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.