andr111 andr111 -5 years ago 142
Python Question

Is it possible to define a function that takes a different form based on the sign of the parameter in Theano

I need to define the following function. Is it possible to do in Theano?
enter image description here


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

Answer Source

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 0 or 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 sign is 1 (or True), the bottomEquation will drop out, being multiplied by 1 ^ 1 or just 0. Similarly, topEquation drops out if sign is 0/False.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download