Missed TensorFlow Dev Summit? Check out the video playlist. Watch recordings

tf.Tensor

TensorFlow 1 version View source on GitHub

A tensor represents a rectangular array of data.

tf.Tensor(
    op, value_index, dtype
)

When writing a TensorFlow program, the main object you manipulate and pass around is the tf.Tensor. A tf.Tensor object represents a rectangular array of arbitrary dimension, filled with data of a specific data type.

A tf.Tensor has the following properties:

  • a data type (float32, int32, or string, for example)
  • a shape

Each element in the Tensor has the same data type, and the data type is always known.

In eager execution, which is the default mode in TensorFlow, results are calculated immediately.

# Compute some values using a Tensor 
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) 
print(e) 
tf.Tensor( 
[[1. 3.] 
 [3. 7.]], shape=(2, 2), dtype=float32) 

Note that during eager execution, you may discover your Tensors are actually of type EagerTensor. This is an internal detail, but it does give you access to a useful function, numpy:

type(e) 
<class '...ops.EagerTensor'> 
print(e.numpy()) 
  [[1. 3.] 
   [3. 7.]] 

TensorFlow can define computations without immediately executing them, most commonly inside tf.functions, as well as in (legacy) Graph mode. In those cases, the shape (that is, the rank of the Tensor and the size of each dimension) might be only partially known.

Most operations produce tensors of fully-known shapes if the shapes of their inputs are also fully known, but in some cases it's only possible to find the shape of a tensor at execution time.

There are specialized tensors; for these, see tf.Variable, tf.constant, tf.placeholder, tf.SparseTensor, and tf.RaggedTensor.

For more on Tensors, see the guide.

Args:

  • op: An Operation. Operation that computes this tensor.
  • value_index: An int. Index of the operation's endpoint that produces this tensor.
  • dtype: A DType. Type of elements stored in this tensor.

Attributes:

  • device: The name of the device on which this tensor will be produced, or None.
  • dtype: The DType of elements in this tensor.
  • graph: The Graph that contains this tensor.
  • name: The string name of this tensor.
  • op: The Operation that produces this tensor as an output.
  • shape: Returns the TensorShape that represents the shape of this tensor.

    The shape is computed using shape inference functions that are registered in the Op for each Operation. See tf.TensorShape for more details of what a shape represents.

    The inferred shape of a tensor is used to provide shape information without having to execute the underlying kernel. 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) # will be TensorShape([2, 3]) 
    (2, 3) 
   
     

d = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]]) print(d.shape) (4, 2)

# Raises a ValueError, because `c` and `d` do not have compatible
# inner dimensions.
>>> e = tf.matmul(c, d)
Traceback (most recent call last):
    ...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix
size-incompatible: In[0]: [2,3], In[1]: [4,2] [Op:MatMul] name: MatMul/

# This works because we have compatible shapes.
>>> f = tf.matmul(c, d, transpose_a=True, transpose_b=True)
>>> print(f.shape)
(3, 4)

In some cases, the inferred shape may have unknown dimensions. If the caller has additional information about the values of these dimensions, Tensor.set_shape() can be used to augment the inferred shape.

  • value_index: The index of this tensor in the outputs of its Operation.

Raises:

  • TypeError: If the op is not an Operation.

Methods

__abs__

View source

__abs__(
    x, name=None
)

Computes the absolute value of a tensor.

Given a tensor of integer or floating-point values, this operation returns a tensor of the same type, where each element contains the absolute value of the corresponding element in the input.

Given a tensor x of complex numbers, this operation returns a tensor of type float32 or float64 that is the absolute value of each element in x. For a complex number \(a + bj\), its absolute value is computed as \(\sqrt{a^2

  • b^2}\). For example:
x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]]) 
tf.abs(x) 
<tf.Tensor: shape=(2, 1), dtype=float64, numpy= 
array([[5.25594901], 
       [6.60492241]])> 

Args:

  • x: A Tensor or SparseTensor of type float16, float32, float64, int32, int64, complex64 or complex128.
  • name: A name for the operation (optional).

Returns:

A Tensor or SparseTensor of the same size, type and sparsity as x, with absolute values. Note, for complex64 or complex128 input, the returned Tensor will be of type float32 or float64, respectively.

If x is a SparseTensor, returns SparseTensor(x.indices, tf.math.abs(x.values, ...), x.dense_shape)

__add__

View source

__add__(
    x, y
)

Dispatches to add for strings and add_v2 for all other types.

__and__

View source

__and__(
    x, y
)

Returns the truth value of x AND y element-wise.

NOTE: LogicalAnd supports broadcasting. More about broadcasting here

Args:

  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__bool__

View source

__bool__()

Dummy method to prevent a tensor from being used as a Python bool.

This overload raises a TypeError when the user inadvertently treats a Tensor as a boolean (most commonly in an if or while statement), in code that was not converted by AutoGraph. For example:

if tf.constant(True):  # Will raise.
  # ...

if tf.constant(5) < tf.constant(7):  # Will raise.
  # ...

Raises:

TypeError.

__div__

View source

__div__(
    x, y
)

Divide two values using Python 2 semantics.

Used for Tensor.div.

Args:

  • x: Tensor numerator of real numeric type.
  • y: Tensor denominator of real numeric type.
  • name: A name for the operation (optional).

Returns:

x / y returns the quotient of x and y.

__eq__

View source

__eq__(
    other
)

Compares two tensors element-wise for equality.

__floordiv__

View source

__floordiv__(
    x, y
)

Divides x / y elementwise, rounding toward the most negative integer.

The same as tf.compat.v1.div(x,y) for integers, but uses tf.floor(tf.compat.v1.div(x,y)) for floating point arguments so that the result is always an integer (though possibly an integer represented as floating point). This op is generated by x // y floor division in Python 3 and in Python 2.7 with from __future__ import division.

x and y must have the same type, and the result will have the same type as well.

Args:

  • x: Tensor numerator of real numeric type.
  • y: Tensor denominator of real numeric type.
  • name: A name for the operation (optional).

Returns:

x / y rounded down.

Raises:

  • TypeError: If the inputs are complex.

__ge__

__ge__(
    x, y, name=None
)

Returns the truth value of (x >= y) element-wise.

NOTE: math.greater_equal supports broadcasting. More about broadcasting here

Example:

x = tf.constant([5, 4, 6, 7])
y = tf.constant([5, 2, 5, 10])
tf.math.greater_equal(x, y) ==> [True, True, True, False]

x = tf.constant([5, 4, 6, 7])
y = tf.constant([5])
tf.math.greater_equal(x, y) ==> [True, False, True, True]

Args:

  • x: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__getitem__

View source

__getitem__(
    tensor, slice_spec, var=None
)

Overload for Tensor.getitem.

This operation extracts the specified region from the tensor. The notation is similar to NumPy with the restriction that currently only support basic indexing. That means that using a non-scalar tensor as input is not currently allowed.

Some useful examples:

# Strip leading and trailing 2 elements
foo = tf.constant([1,2,3,4,5,6])
print(foo[2:-2].eval())  # => [3,4]

# Skip every other row and reverse the order of the columns
foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
print(foo[::2,::-1].eval())  # => [[3,2,1], [9,8,7]]

# Use scalar tensors as indices on both dimensions
print(foo[tf.constant(0), tf.constant(2)].eval())  # => 3

# Insert another dimension
foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
print(foo[tf.newaxis, :, :].eval()) # => [[[1,2,3], [4,5,6], [7,8,9]]]
print(foo[:, tf.newaxis, :].eval()) # => [[[1,2,3]], [[4,5,6]], [[7,8,9]]]
print(foo[:, :, tf.newaxis].eval()) # => [[[1],[2],[3]], [[4],[5],[6]],
[[7],[8],[9]]]

# Ellipses (3 equivalent operations)
foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
print(foo[tf.newaxis, :, :].eval())  # => [[[1,2,3], [4,5,6], [7,8,9]]]
print(foo[tf.newaxis, ...].eval())  # => [[[1,2,3], [4,5,6], [7,8,9]]]
print(foo[tf.newaxis].eval())  # => [[[1,2,3], [4,5,6], [7,8,9]]]

# Masks
foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
print(foo[foo > 2].eval())  # => [3, 4, 5, 6, 7, 8, 9]

Notes:

  • tf.newaxis is None as in NumPy.
  • An implicit ellipsis is placed at the end of the slice_spec
  • NumPy advanced indexing is currently not supported.

Args:

  • tensor: An ops.Tensor object.
  • slice_spec: The arguments to Tensor.getitem.
  • var: In the case of variable slice assignment, the Variable object to slice (i.e. tensor is the read-only view of this variable).

Returns:

The appropriate slice of "tensor", based on "slice_spec".

Raises:

  • ValueError: If a slice range is negative size.
  • TypeError: If the slice indices aren't int, slice, ellipsis, tf.newaxis or scalar int32/int64 tensors.

__gt__

__gt__(
    x, y, name=None
)

Returns the truth value of (x > y) element-wise.

NOTE: math.greater supports broadcasting. More about broadcasting here

Example:

x = tf.constant([5, 4, 6])
y = tf.constant([5, 2, 5])
tf.math.greater(x, y) ==> [False, True, True]

x = tf.constant([5, 4, 6])
y = tf.constant([5])
tf.math.greater(x, y) ==> [False, False, True]

Args:

  • x: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__invert__

__invert__(
    x, name=None
)

Returns the truth value of NOT x element-wise.

Example:

tf.math.logical_not(tf.constant([True, False])) 
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([False,  True])> 

Args:

  • x: A Tensor of type bool. A Tensor of type bool.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__iter__

View source

__iter__()

__le__

__le__(
    x, y, name=None
)

Returns the truth value of (x <= y) element-wise.

NOTE: math.less_equal supports broadcasting. More about broadcasting here

Example:

x = tf.constant([5, 4, 6])
y = tf.constant([5])
tf.math.less_equal(x, y) ==> [True, True, False]

x = tf.constant([5, 4, 6])
y = tf.constant([5, 6, 6])
tf.math.less_equal(x, y) ==> [True, True, True]

Args:

  • x: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__len__

View source

__len__()

__lt__

__lt__(
    x, y, name=None
)

Returns the truth value of (x < y) element-wise.

NOTE: math.less supports broadcasting. More about broadcasting here

Example:

x = tf.constant([5, 4, 6])
y = tf.constant([5])
tf.math.less(x, y) ==> [False, True, False]

x = tf.constant([5, 4, 6])
y = tf.constant([5, 6, 7])
tf.math.less(x, y) ==> [False, True, True]

Args:

  • x: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__matmul__

View source

__matmul__(
    x, y
)

Multiplies matrix a by matrix b, producing a * b.

The inputs must, following any transpositions, be tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication dimensions, and any further outer dimensions specify matching batch size.

Both matrices must be of the same type. The supported types are: float16, float32, float64, int32, complex64, complex128.

Either matrix can be transposed or adjointed (conjugated and transposed) on the fly by setting one of the corresponding flag to True. These are False by default.

If one or both of the matrices contain a lot of zeros, a more efficient multiplication algorithm can be used by setting the corresponding a_is_sparse or b_is_sparse flag to True. These are False by default. This optimization is only available for plain matrices (rank-2 tensors) with datatypes bfloat16 or float32.

A simple 2-D tensor matrix multiplication:

a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) 
a  # 2-D tensor 
<tf.Tensor: shape=(2, 3), dtype=int32, numpy= 
array([[1, 2, 3], 
       [4, 5, 6]], dtype=int32)> 
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) 
b  # 2-D tensor 
<tf.Tensor: shape=(3, 2), dtype=int32, numpy= 
array([[ 7,  8], 
       [ 9, 10], 
       [11, 12]], dtype=int32)> 
c = tf.matmul(a, b) 
c  # `a` * `b` 
<tf.Tensor: shape=(2, 2), dtype=int32, numpy= 
array([[ 58,  64], 
       [139, 154]], dtype=int32)> 

A batch matrix multiplication with batch shape [2]:

a = tf.constant(np.arange(1, 13, dtype=np.int32), shape=[2, 2, 3]) 
a  # 3-D tensor 
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy= 
array([[[ 1,  2,  3], 
        [ 4,  5,  6]], 
       [[ 7,  8,  9], 
        [10, 11, 12]]], dtype=int32)> 
b = tf.constant(np.arange(13, 25, dtype=np.int32), shape=[2, 3, 2]) 
b  # 3-D tensor 
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy= 
array([[[13, 14], 
        [15, 16], 
        [17, 18]], 
       [[19, 20], 
        [21, 22], 
        [23, 24]]], dtype=int32)> 
c = tf.matmul(a, b) 
c  # `a` * `b` 
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy= 
array([[[ 94, 100], 
        [229, 244]], 
       [[508, 532], 
        [697, 730]]], dtype=int32)> 

Since python >= 3.5 the @ operator is supported (see PEP 465). In TensorFlow, it simply calls the tf.matmul() function, so the following lines are equivalent:

d = a @ b @ [[10], [11]] 
d = tf.matmul(tf.matmul(a, b), [[10], [11]]) 

Args:

  • a: tf.Tensor of type float16, float32, float64, int32, complex64, complex128 and rank > 1.
  • b: tf.Tensor with same type and rank as a.
  • transpose_a: If True, a is transposed before multiplication.
  • transpose_b: If True, b is transposed before multiplication.
  • adjoint_a: If True, a is conjugated and transposed before multiplication.
  • adjoint_b: If True, b is conjugated and transposed before multiplication.
  • a_is_sparse: If True, a is treated as a sparse matrix. Notice, this does not support tf.sparse.SparseTensor, it just makes optimizations that assume most values in a are zero. See tf.sparse.sparse_dense_matmul for some support for tf.SparseTensor multiplication.
  • b_is_sparse: If True, b is treated as a sparse matrix. Notice, this does not support tf.sparse.SparseTensor, it just makes optimizations that assume most values in a are zero. See tf.sparse.sparse_dense_matmul for some support for tf.SparseTensor multiplication.
  • name: Name for the operation (optional).

Returns:

A tf.Tensor of the same type as a and b where each inner-most matrix is the product of the corresponding matrices in a and b, e.g. if all transpose or adjoint attributes are False:

output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j]), for all indices i, j.

  • Note: This is matrix product, not element-wise product.

Raises:

  • ValueError: If transpose_a and adjoint_a, or transpose_b and adjoint_b are both set to True.

__mod__

View source

__mod__(
    x, y
)

Returns element-wise remainder of division. When x < 0 xor y < 0 is

true, this follows Python semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.

NOTE: math.floormod supports broadcasting. More about broadcasting here

Args:

  • x: A Tensor. Must be one of the following types: int32, int64, bfloat16, half, float32, float64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

A Tensor. Has the same type as x.

__mul__

View source

__mul__(
    x, y
)

Dispatches cwise mul for "DenseDense" and "DenseSparse".

__ne__

View source

__ne__(
    other
)

Compares two tensors element-wise for equality.

__neg__

__neg__(
    x, name=None
)

Computes numerical negative value element-wise.

I.e., \(y = -x\).

Args:

  • x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).

Returns:

A Tensor. Has the same type as x.

If x is a SparseTensor, returns SparseTensor(x.indices, tf.math.negative(x.values, ...), x.dense_shape)

__nonzero__

View source

__nonzero__()

Dummy method to prevent a tensor from being used as a Python bool.

This is the Python 2.x counterpart to __bool__() above.

Raises:

TypeError.

__or__

View source

__or__(
    x, y
)

Returns the truth value of x OR y element-wise.

NOTE: math.logical_or supports broadcasting. More about broadcasting here

Args:

  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__pow__

View source

__pow__(
    x, y
)

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes \(x^y\) for corresponding elements in x and y. For example:

x = tf.constant([[2, 2], [3, 3]])
y = tf.constant([[8, 16], [2, 3]])
tf.pow(x, y)  # [[256, 65536], [9, 27]]

Args:

  • x: A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128.
  • y: A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128.
  • name: A name for the operation (optional).

Returns:

A Tensor.

__radd__

View source

__radd__(
    y, x
)

Dispatches to add for strings and add_v2 for all other types.

__rand__

View source

__rand__(
    y, x
)

Returns the truth value of x AND y element-wise.

NOTE: LogicalAnd supports broadcasting. More about broadcasting here

Args:

  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).

Returns:

A Tensor of type bool.

__rdiv__

View source

__rdiv__(
    y, x
)

Divide two values using Python 2 semantics.

Used for Tensor.div.

Args:

  • x: Tensor numerator of real numeric type.
  • y: Tensor denominator of real numeric type.
  • name: A name for the operation (optional).

Returns:

x / y returns the quotient of x and y.

__rfloordiv__

View source

__rfloordiv__(
    y, x
)

Divides x / y elementwise, rounding toward the most negative integer.

The same as tf.compat.v1.div(x,y) for integers, but uses tf.floor(tf.compat.v1.div(x,y)) for floating point arguments so that the result is always an integer (though possibly an integer represented as floating point). This op is generated by x // y floor division in Python 3 and in Python 2.7 with from __future__ import division.

x and y must have the same type, and the result will have the same type as well.

Args:

  • x: Tensor numerator of real numeric type.
  • y: Tensor denominator of real numeric type.
  • name: A name for the operation (optional).

Returns:

x / y rounded down.

Raises:

  • TypeError: If the inputs are complex.

__rmatmul__

View source

__rmatmul__(
    y, x
)

Multiplies matrix a by matrix b, producing a * b.

The inputs must, following any transpositions, be tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication dimensions, and any further outer dimensions specify matching batch size.

Both matrices must be of the same type. The supported types are: float16, float32, float64, int32, complex64, complex128.

Either matrix can be transposed or adjointed (conjugated and transposed) on the fly by setting one of the corresponding flag to True. These are False by default.

If one or both of the matrices contain a lot of zeros, a more efficient multiplication algorithm can be used by setting the corresponding a_is_sparse or b_is_sparse flag to True. These are False by default. This optimization is only available for plain matrices (rank-2 tensors) with datatypes bfloat16 or float32.

A simple 2-D tensor matrix multiplication:

a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) 
a  # 2-D tensor 
<tf.Tensor: shape=(2, 3), dtype=int32, numpy= 
array([[1, 2, 3], 
       [4, 5, 6]], dtype=int32)> 
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) 
b  # 2-D tensor 
<tf.Tensor: shape=(3, 2), dtype=int32, numpy= 
array([[ 7,  8], 
       [ 9, 10], 
       [11, 12]], dtype=int32)> 
c = tf.matmul(a, b) 
c  # `a` * `b` 
<tf.Tensor: shape=(2, 2), dtype=int32, numpy= 
array([[ 58,  64], 
       [139, 154]], dtype=int32)> 

A batch matrix multiplication with batch shape [2]:

a = tf.constant(np.arange(1, 13, dtype=np.int32), shape=[2, 2, 3]) 
a  # 3-D tensor 
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy= 
array([[[ 1,  2,  3], 
        [ 4,  5,  6]], 
       [[ 7,  8,  9], 
        [10, 11, 12]]], dtype=int32)> 
b = tf.constant(np.arange(13, 25, dtype=np.int32), shape=[2, 3, 2]) 
b  # 3-D tensor 
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy= 
array([[[13, 14], 
        [15, 16], 
        [17, 18]], 
       [[19, 20], 
        [21, 22], 
        [23, 24]]], dtype=int32)> 
c = tf.matmul(a, b) 
c  # `a` * `b` 
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy= 
array([[[ 94, 100], 
        [229, 244]], 
       [[508, 532], 
        [697, 730]]], dtype=int32)> 

Since python >= 3.5 the @ operator is supported (see PEP 465). In TensorFlow, it simply calls the tf.matmul() function, so the following lines are equivalent:

d = a @ b @ [[10], [11]] 
d = tf.matmul(tf.matmul(a, b), [[10], [11]]) 

Args:

  • a: tf.Tensor of type float16, float32, float64, int32, complex64, complex128 and rank > 1.
  • b: tf.Tensor with same type and rank as a.
  • transpose_a: If True, a is transposed before multiplication.
  • transpose_b: If True, b is transposed before multiplication.
  • adjoint_a: If True, a is conjugated and transposed before multiplication.
  • adjoint_b: If True, b is conjugated and transposed before multiplication.
  • a_is_sparse: If True, a is treated as a sparse matrix. Notice, this does not support tf.sparse.SparseTensor, it just makes optimizations that assume most values in a are zero. See tf.sparse.sparse_dense_matmul for some support for tf.SparseTensor multiplication.
  • b_is_sparse: If True, b is treated as a sparse matrix. Notice, this does not support tf.sparse.SparseTensor, it just makes optimizations that assume most values in a are zero. See tf.sparse.sparse_dense_matmul for some support for tf.SparseTensor multiplication.
  • name: Name for the operation (optional).

Returns:

A tf.Tensor of the same type as a