thanks a lot, Fred.
I define a theano function as below :
x = T.fmatrix()
y = T.fmatrix()
z = x / y
f = theano.function([x,y],z)
and give
a = array([[ 0., 1., 2.],
[ 3., -4., 5.]], dtype=float32)
*b = array([[ 0., 1., 0.], [ 1., 0., 1.]],
dtype=float32)*
f (a,b) = array([[ nan, 1., inf],
[ -0., -inf, 5.]], dtype=float32)
as can we see from denominator b , there are 0 in b.
could we modify "z= x/y " to process all the cases??
在 2016年7月30日星期六 UTC+9上午2:40:37,nouiz写道:
>
> Just to be sure, oou want to replace inf/nan with a numerical value in
> Theano? This can be done like this:
>
> import numpy as np
>
> a = np.array([0,0,1,1,2], dtype='float')
> b = np.array([0,1,0,1,3], dtype='float')
> with np.errstate(divide='ignore', invalid='ignore'):
> c = np.true_divide(a,b)
>
> import theano.tensor as T
> import theano
> c=theano.tensor.as_tensor_variable(c)
> inf_mask = T.isinf(c)
> nan_mask = T.isnan(c)
> inf_idx = inf_mask.nonzero()
> nan_idx = nan_mask.nonzero()
>
> c_without_inf = theano.tensor.set_subtensor(c[inf_idx], -999)
> c_without_inf_nan = theano.tensor.set_subtensor(c_without_inf[nan_idx], 0)
> c_without_inf_nan.eval()
> array([ 0.00000000e+00, 0.00000000e+00, -9.99000000e+02,
> 1.00000000e+00, 6.66666667e-01])
>
> I don't know if it make a difference between inf and -inf.
>
> Fred
>
>
>
> On Thu, Jul 28, 2016 at 11:52 PM Varglur <[email protected]
> <javascript:>> wrote:
>
>> dear all,
>>
>> similar to problem in numpy here
>> <http://stackoverflow.com/questions/26248654/numpy-return-0-with-divide-by-zero>
>>
>> if element wise divide in theano , are there corresponding function in
>> theano as this answer in numpy ?
>>
>> numpy answer:
>>
>> import numpy as np
>>
>> a = np.array([0,0,1,1,2], dtype='float')
>> b = np.array([0,1,0,1,3], dtype='float')
>> with np.errstate(divide='ignore', invalid='ignore'):
>> c = np.true_divide(a,b)
>> c[c == np.inf] = 0
>> c = np.nan_to_num(c)
>> print('c: {0}'.format(c))
>>
>> Output:
>>
>> c: [ 0. 0. 0. 1. 0.66666667]
>>
>>
>>
>> how to process inf and nan problem when 0/0 or 1/0 problem in theano??
>>
>> thanks
>>
>>
>>
>> --
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "theano-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
--
---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.