Remove inf from Theano array

317 views Asked by At

Supposing I have a vector in Theano and some of the elements are inf, how do I remove them? Consider the following example:

import numpy as np
import theano
from theano import tensor
vec = tensor.dvector('x')
fin = vec[(~tensor.isinf(vec)).nonzero()]
f = theano.function([vec], fin)

According to the Theano documentation, this should remove the elements via indexing. However, this is not the case as f([1,2,np.inf]) returns array([ 1., 2., inf]).

How can I do this so that f([1,2,np.inf]) returns array([ 1., 2.]) ?

1

There are 1 answers

0
Alexander McFarlane On BEST ANSWER

I found an awkward workaround

import theano
import theano.tensor as T
import numpy as np

vec = T.vector()
compare = T.isinf(vec)
out = vec[(1-compare).nonzero()]

v = [  1.,   1.,   1.,   1.,  np.inf,   3.,   4.,   5.,   6.,  np.inf]
v = np.asarray(v)

out.eval({var:v})
array([ 1.,  1.,  1.,  1.,  3.,  4.,  5.,  6.])

For your example:

fin = vec[(1-T.isinf(vec)).nonzero()]
f = theano.function([vec], fin)

f([1,2,np.inf])
array([ 1.,  2.])