有恒等函数吗?

Is there an identity function?

假设我有一个 Theano 符号 x 并且我 运行 下面的代码。

x.name = "x"
y = x
y.name = "y"

当然x.name就是"y"是否有一个身份函数可以让我做如下的事情?

x.name = "x"
y = T.identity(x)
y.name = "y"

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

我问这个的原因是因为我有如下情况,其中 filterfeature 是 Theano 符号, nonlinearTrueFalse.

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"

张量上的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"