Help protect the Great Barrier Reef with TensorFlow on Kaggle Join Challenge


A tf.Tensor represents a multidimensional array of elements.

All elements are of a single known data type.

When writing a TensorFlow program, the main object that is manipulated and passed around is the tf.Tensor.

A tf.Tensor has the following properties:

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

TensorFlow supports eager execution and graph execution. In eager execution, operations are evaluated immediately. In graph execution, a computational graph is constructed for later evaluation.

TensorFlow defaults to eager execution. In the example below, the matrix multiplication 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)
[[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:

<class '...ops.EagerTensor'>
  [[1. 3.]
   [3. 7.]]

In TensorFlow, tf.functions are a common way to define graph execution.

A Tensor's shape (that is, the rank of the Tensor and the size of each dimension) may not always be fully known. In tf.function definitions, the shape may only be 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.

A number of specialized tensors are available: see tf.Variable, tf.constant, tf.placeholder, tf.sparse.SparseTensor, and tf.RaggedTensor.

a = np.array([1, 2, 3])
b = tf.constant(a)
a[0] = 4
print(b)  # tf.Tensor([4 2 3], shape=(3,), dtype=int64)

For more on Tensors, see the guide.

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.

TypeError If the op is not an Operation.

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 a tf.TensorShape that represents the shape of this tensor.

t = tf.constant([1,2,3,4,5])

tf.Tensor.shape is equivalent to tf.Tensor.get_shape().

In a