What is the equivalent of [a <0] = 0 in Theano?

What is the NumPy equivalent a[a < 0] = 0

in Theano (tensor variable)? I want all my matrix elements to be less than a number equal to zero.

+3


source share


2 answers


This work:

import theano
a=theano.tensor.matrix()
idxs=(a<0).nonzero()
new_a=theano.tensor.set_subtensor(a[idxs], 0)

      



Don't forget that Theano is a symbolic language. Thus, the variable a does not change in the user's graph. This is a new variable new_a that contains the new value and still has the old value.

Theano will optimize this to work locally if possible.

+5


source


This also works and can add an upper bound on the border as well

import theano
import theano.tensor as T
a = T.matrix()
b = a.clip(0.0)

      

or if you want to get the top border you can try:



b = T.clip(a, 0.0, 1.0)

      

where 1.0 is the place to set the upper border.

check docs here

+1


source







All Articles