andr111 - 1 year ago 63

Python Question

I need to define the following function. Is it possible to do in Theano?

**UPDATE:**

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.

**UPDATE 2:**

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