[英]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?
在Theano有可能做吗?
UPDATE: 更新:
To clarify I'm asking about defining a theano symbolic variable that can take the above form. 为了澄清,我想定义一个可以采用上述形式的theano符号变量。 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.
我知道我可以定义2个单独的变量,并根据R的值使用它们中的任何一个。我的问题是可以定义采用上述形式的单个变量。 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: 更新2:
Proposed solution with lambda doesn't work. 用lambda提出的解决方案不起作用。 This doesn't generate a symbolic variable that can later be used with Theano:
这不会生成可稍后与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 0
or 1
. 在不了解有关Theano的细节的情况下,我记得将
if-else
语句转换为线性方程式的一种方法是将if
检入变量本身,将其设置为0
或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
. 它具有很好的属性,如果
sign
为1
(或True
),则bottomEquation
将bottomEquation
,乘以1 ^ 1
或仅乘以0
。 Similarly, topEquation
drops out if sign
is 0
/ False
. 同样,如果
sign
为0
/ False
,则topEquation
退出。
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). 需要注意的是,尽管Theano可以帮助解决这一问题-它仍然会评估两个方程,因此这可能会带来效率问题(对于每个输入,它都会运行两个方程,然后忽略其中的一个)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.