Search code examples
pythontheano

Is there an identity function?


Suppose I have a Theano symbol x and I run the following code.

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

Of course x.name is then "y". Is there an identity function that lets me do something like the following?

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

The expected behavior is that y is now seen to be a function of x, and they are both properly named. Of course the Theano compiler would simply merge the symbols because y is just x.

The reason I am asking this is because I have a situation like the following, where filter and feature are Theano symbols and nonlinear is either True or False.

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

The problem is that in the case of nonlinear being False, my activation symbol gets the name "response".

I can fix this by working around the problem:

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"

But an identity function would be much more elegant:

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

Solution

  • The copy(name=None) function on tensors is what you want.

    The first example becomes this:

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

    The second example becomes this:

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