Frage

I'm trying to find find the value of s=max(ele, 0) element-wise on a matrix in theano. I don't have much experience with theano.

So far I have

x = theano.tensor.dmatrix('x')
s = (x + abs(x)) / 2  # poor man's trick
linmax = function([x], s)

This works, but isn't pretty and I thought I should be able to use theano.tensor.maximum for this.

In matlab, to do what I want to do, I would just write linmax=@(x) max (x, zeros (size (x)))

War es hilfreich?

Lösung

This works for me:

import theano.tensor as T
from theano import function

x = T.dmatrix('x')
linmax = function([x], T.maximum(x,0))

Testing:

linmax([[-1,-2],[3,4]])

Outputs:

array([[0.,0.],[3.,4.]])

Andere Tipps

I have seen this implemented as

s = x*(x>0)

several times. Dont know if that's faster than T.maximum()

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top