Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does TensorFlow name tensors?

Tags:

tensorflow

I wonder if this is the correct understanding:

All tensors are derived from some operation, and operations are either given a name in the constructor, or given the default name for a particular kind of operation. If the name is not unique, TensorFlow automatically handles this by appending "_1", "_2", etc. An operation with n tensor outputs name these tensors "op_name:0", "op_name:1", ..., "op_name:n-1".

One problem seems to arise: if x is a tf.Variable, then x.name gives "variable_name:0". This is confusing: to what does "variable_name" refer?

like image 846
Shengjia Zhao Avatar asked Mar 22 '16 09:03

Shengjia Zhao


People also ask

What does TensorFlow have to do with tensors?

TensorFlow, as the name indicates, is a framework to define and run computations involving tensors. A tensor is a generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.

What are three parameters that define tensors in TensorFlow?

Each tensor object is defined with tensor attributes like a unique label (name), a dimension (shape) and TensorFlow data types (dtype). You can define a tensor with decimal values or with a string by changing the type of data.

How do you define a tensor in Python?

A tensor can be called a generalized matrix. It could be a 0-D matrix (a single number), 1-D matrix (a vector), 2-D matrix or any higher dimensional structure. A tensor is identified by three parameters viz., rank, shape and size. The number of dimensions of the tensor is said to be its rank.


1 Answers

Your observations on Tensor naming are absolutely correct: the name of a Tensor is the concatenation of

  1. the name of the operation that produced it,
  2. a colon (:), and
  3. the index of that tensor in the outputs of the operation that produced it.

Therefore the tensor named "foo:2" is the output of the op named "foo" at position 2 (with indices starting from zero).

The naming of tf.Variable objects is slightly strange. Every tf.Variable contains a mutable tensor object that holds the state of the variable (and a few other tensors). A "Variable" op (which has the name "variable_name" in your example) "produces" this mutable tensor each time it is run as its 0th output, so the name of the mutable tensor is "variable_name:0".

Since a tf.Variable is mostly indistinguishable from a tf.Tensor—in that it can be used in the same places—we took the decision to make variable names resemble tensor names, so the Variable.name property returns the name of the mutable tensor. (This contrasts with tf.QueueBase and tf.ReaderBase objects, which are not usable directly as tensors (instead you have to call methods on them to create ops that operate on their state), so these do not have a tensor-like name.)

like image 107
mrry Avatar answered Oct 04 '22 13:10

mrry