I need to define the following function. Is it possible to do in Theano?
To clarify I'm asking about defining a theano symbolic variable that can take the above form. I understand that I can define 2 separate variables and use either of them based on the value of R. My questions here is it possible to define a single variable that takes the above form. The reason is that I need to take gradients of this variable as well as use it in other variables and it would drastically simplify my solution if I can define this withing a single symbolic variable.
Proposed solution with lambda doesn't work. This doesn't generate a symbolic variable that can later be used with Theano:
r = T.dscalar('r')
dd = lambda r: r + 1 if r > 0 else r - 1
Without knowing specifics about Theano, I remember that one way to turn an
if-else statement into a linear equation is to make your
if check into a variable itself, setting it as
1. Then, you can do something like:
sign = (R_t > 0) ## this is the part I don't know how exactly to do (topEquation * sign) + (bottomEquation * (sign ^ 1))
This has the nice property that if
bottomEquation will drop out, being multiplied by
1 ^ 1 or just
topEquation drops out if
One note, though maybe Theano can help with this - it will still evaluate both equations, so this could present an efficiency concern (for every single input, it's running both equations, and then ignoring one of them).