Tensors and eager execution

TensorFlow · Essentials · 8 min

Every value flowing through a TensorFlow program is a tensor, so this is the vocabulary for everything else.

A `tf.Tensor` is an n-dimensional array with a shape and a dtype, much like a NumPy array. Create one with tf.constant(value) or from factories like tf.zeros, tf.ones, tf.range and tf.random.normal.

Since TensorFlow 2, code runs eagerly: each operation executes immediately and returns a concrete result, so you can print tensors and debug with normal Python tools. (Graphs still exist, but you opt into them with tf.function.)

Things that differ from NumPy:

  • Tensors are immutable. You cannot assign to t[0, 0]; you create a new tensor instead. Mutable state lives in tf.Variable.
  • Python ints become int32 and Python floats float32 by default; NumPy arrays keep their dtype (often int64 or float64).
  • t.numpy() returns a NumPy array, and most NumPy functions accept tensors directly.

Inspect a tensor with t.shape (a TensorShape, which may contain None for unknown dimensions in graphs), t.dtype, tf.rank(t) and tf.size(t).

Operations are placed on a GPU automatically when one is available, so the same code runs on CPU and GPU without changes.

Example

import tensorflow as tf

t = tf.constant([[1, 2, 3], [4, 5, 6]])
print(t.shape, t.dtype)            # (2, 3) <dtype: 'int32'>
print(t.numpy())                   # [[1 2 3]
                                   #  [4 5 6]]
f = tf.constant([1.0, 2.0])
print(f.dtype)                     # <dtype: 'float32'>

print(t[1, 2].numpy())             # 6
print(tf.rank(t).numpy(), tf.size(t).numpy())   # 2 6
print(tf.zeros([2, 2]).shape)      # (2, 2)

# t[0, 0] = 9  -> TypeError: tensors are immutable; use tf.Variable for state

Remember

  • Tensors are typed, immutable n-d arrays
  • TF 2 runs eagerly; .numpy() gets the values
  • Python ints default to int32 and floats to float32

Watch out for

  • Trying to assign into a tensor; build a new one or use a tf.Variable.

Check yourself

  1. What is the dtype of tf.constant([1, 2, 3])?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: www.tensorflow.org/guide/tensor