Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a name to a tensor?

Is there any way to assign a name to a tensor after it has been created?

I'm building up a neural network bit by bit in a loop, like so:

    def build_logit_pipeline(data):
        # X --> *W1 --> +b1 --> relu --> *W2 --> +b2 ... --> softmax etc...
        pipeline = data

        for i in xrange(len(layer_sizes) - 1):
            with tf.name_scope("linear%d" % i):
                pipeline = tf.matmul(pipeline, weights[i])
                pipeline = tf.add(pipeline, biases[i])

            if i != len(layer_sizes) - 2:
                with tf.name_scope("relu%d" % i):
                    pipeline = tf.nn.relu(pipeline)

        return pipeline

I'd like to assign a name to the result of the entire operation, i.e. the last tf.add should be assigned a name. Is there a way to do this on the pipeline variable right before returning, instead of checking the end of loop condition on the last add, which would be less elegant?

like image 962
Claudiu Avatar asked Jul 02 '16 07:07

Claudiu


People also ask

How do you define a tensor in Python?

Tensors in Python Like vectors and matrices, tensors can be represented in Python using the N-dimensional array (ndarray). A tensor can be defined in-line to the constructor of array() as a list of lists. The example below defines a 3x3x3 tensor as a NumPy ndarray.

Can tensor have different data types?

It is not possible to have a Tensor with more than one data type. It is possible, however, to serialize arbitrary data structures as strings and store those in tensors.

How do you access values in a tensor?

We can access the value of a tensor by using indexing and slicing. Indexing is used to access a single value in the tensor. slicing is used to access the sequence of values in a tensor. we can modify a tensor by using the assignment operator.


1 Answers

You cannot modify the name of a Tensor.

However, one easy trick to do what you want is to use tf.identity with the name you want:

res = build_logit_pipeline(data)
res = tf.identity(res, name="your_name")
like image 82
Olivier Moindrot Avatar answered Oct 24 '22 12:10

Olivier Moindrot