Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theano element wise maximum

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)))

like image 529
Lyndon White Avatar asked Nov 27 '13 06:11

Lyndon White


2 Answers

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.]])
like image 183
szmoore Avatar answered Nov 16 '22 13:11

szmoore


I have seen this implemented as

s = x*(x>0)

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

like image 33
pwohlhart Avatar answered Nov 16 '22 13:11

pwohlhart