有身份功能吗?

时间:2016-01-07 18:47:40

标签: python theano

假设我有一个Theano符号x.name = "x" y = x y.name = "y" ,我运行以下代码。

x.name

当然"y"x.name = "x" y = T.identity(x) y.name = "y" 是否有身份功能可以让我执行以下操作?

y

预期的行为是x现在被视为y的函数,并且它们都被正确命名。当然,Theano编译器只会合并符号,因为x只是filter

我问这个的原因是因为我遇到如下情况:featurenonlinear是Theano符号而TrueFalse或{{ 1}}。

activation = T.dot(feature, filter)
activation.name = "activation"
response = T.nnet.sigmoid(activation) if nonlinear else activation
response.name = "response"

问题是,在nonlinearFalse的情况下,我的activation符号的名称为"response"

我可以通过解决问题来解决这个问题:

activation = T.dot(feature, filter)
activation.name = "activation"
if nonlinear:
    response = T.nnet.sigmoid(activation)
    response.name = "response"
else:
    response = activation
    response.name = "activation&response"

但是身份功能会更加优雅:

activation = T.dot(feature, filter)
activation.name = "activation"
response = T.nnet.sigmoid(activation) if nonlinear else T.identity(activation)
response.name = "response"

1 个答案:

答案 0 :(得分:3)

张量上的copy(name=None) function就是你想要的。

第一个例子变成了这个:

x.name = "x"
y = x.copy("y")

第二个例子变成了这个:

activation = T.dot(feature, filter)
activation.name = "activation"
response = T.nnet.sigmoid(activation) if nonlinear else activation.copy()
response.name = "response"