See the guide: Building Graphs > Core graph data structures
Represents one of the outputs of an
Tensor is a symbolic handle to one of the outputs of an
Operation. It does not hold the values of that operation's output,
but instead provides a means of computing those values in a
This class has two primary purposes:
Tensorcan be passed as an input to another
Operation. This builds a dataflow connection between operations, which enables TensorFlow to execute an entire
Graphthat represents a large, multi-step computation.
After the graph has been launched in a session, the value of the
Tensorcan be computed by passing it to
t.eval()is a shortcut for calling
In the following example,
e are symbolic
result is a numpy array that stores a concrete
# Build a dataflow graph. c = tf.constant([[1.0, 2.0], [3.0, 4.0]]) d = tf.constant([[1.0, 1.0], [0.0, 1.0]]) e = tf.matmul(c, d) # Construct a `Session` to execute the graph. sess = tf.Session() # Execute the graph and store the value that `e` represents in `result`. result = sess.run(e)
The name of the device on which this tensor will be produced, or None.
DType of elements in this tensor.
Graph that contains this tensor.
The string name of this tensor.
Operation that produces this tensor as an output.
TensorShape that represents the shape of this tensor.
The shape is computed using shape inference functions that are
registered in the Op for each
for more details of what a shape represents.
The inferred shape of a tensor is used to provide shape information without having to launch the graph in a session. This can be used for debugging, and providing early error messages. For example:
c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) print(c.shape) ==> TensorShape([Dimension(2), Dimension(3)]) d = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]]) print(d.shape) ==> TensorShape([Dimension(4), Dimension(2)]) # Raises a ValueError, because `c` and `d` do not have compatible # inner dimensions. e = tf.matmul(c, d) f = tf.matmul(c, d, transpose_a=True, transpose_b=True) print(f.shape) ==> TensorShape([Dimension(3), Dimension(4)])
In some cases, the inferred shape may have unknown dimensions. If
the caller has additional information about the values of these
Tensor.set_shape() can be used to augment the
TensorShape representing the shape of this tensor.
The index of this tensor in the outputs of its
__init__(op, value_index, dtype)
Creates a new
Operationthat computes this tensor.
int. Index of the operation's endpoint that produces this tensor.
DType. Type of elements stored in this tensor.
TypeError: If the op is not an
Returns a list of
Operations that consume this tensor.
A list of
Evaluates this tensor in a
Calling this method will execute all preceding operations that produce the inputs needed for the operation that produces this tensor.
N.B. Before invoking
Tensor.eval(), its graph must have been
launched in a session, and either a default session must be
session must be specified explicitly.
feed_dict: A dictionary that maps
Tensorobjects to feed values. See
tf.Session.runfor a description of the valid feed values.
session: (Optional.) The
Sessionto be used to evaluate this tensor. If none, the default session will be used.
A numpy array corresponding to the value of this tensor.
Alias of Tensor.shape.
Updates the shape of this tensor.
This method can be called multiple times, and will merge the given
shape with the current shape of this tensor. It can be used to
provide additional information about the shape of this tensor that
cannot be inferred from the graph alone. For example, this can be used
to provide additional information about the shapes of images:
_, image_data = tf.TFRecordReader(...).read(...) image = tf.image.decode_png(image_data, channels=3) # The height and width dimensions of `image` are data dependent, and # cannot be computed without executing the op. print(image.shape) ==> TensorShape([Dimension(None), Dimension(None), Dimension(3)]) # We know that each image in this dataset is 28 x 28 pixels. image.set_shape([28, 28, 3]) print(image.shape) ==> TensorShape([Dimension(28), Dimension(28), Dimension(3)])
TensorShaperepresenting the shape of this tensor.
shapeis not compatible with the current shape of this tensor.