TensorFlow Cheatsheet

Tensors

Use this TensorFlow reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

What is a Tensor

A tensor is an immutable, multi-dimensional array with a uniform dtype. TensorFlow operations produce and consume tensors. tf.Tensor objects are not NumPy arrays but interoperate with them freely.

import tensorflow as tf

# Scalar (rank-0)
s = tf.constant(3.14)

# Vector (rank-1)
v = tf.constant([1, 2, 3])

# Matrix (rank-2)
m = tf.constant([[1, 2], [3, 4]])

# 3-D tensor
t = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print(t.shape)   # (2, 2, 2)
print(t.dtype)   # tf.int32
print(t.ndim)    # 3

Creating Tensors

From Python / NumPy

import numpy as np

tf.constant(42)                        # scalar int32
tf.constant(3.14)                      # scalar float32
tf.constant([1.0, 2.0, 3.0])          # 1-D float32
tf.constant([[1, 2], [3, 4]], dtype=tf.float64)

a = np.array([1, 2, 3])
t = tf.constant(a)                     # copies data
t = tf.convert_to_tensor(a)           # same effect

Filled / Shaped Tensors

FunctionDescription
tf.zeros(shape)All zeros
tf.ones(shape)All ones
tf.fill(shape, value)All value
tf.zeros_like(tensor)Zeros matching shape/dtype
tf.ones_like(tensor)Ones matching shape/dtype
tf.eye(num_rows)Identity matrix
tf.linspace(start, stop, num)Evenly spaced values
tf.range(start, limit, delta)Like Python range
tf.zeros([2, 3])                       # shape (2, 3) of 0.0
tf.ones([3], dtype=tf.int32)
tf.fill([2, 2], 7)
tf.eye(4)                              # 4x4 identity
tf.linspace(0.0, 1.0, 5)              # [0. 0.25 0.5 0.75 1.]
tf.range(10)                           # [0..9]
tf.range(2, 10, 2)                     # [2, 4, 6, 8]

Random Tensors

tf.random.set_seed(42)                 # global seed

tf.random.normal([3, 3])              # N(0,1)
tf.random.normal([3, 3], mean=5.0, stddev=2.0)
tf.random.uniform([2, 4])             # U[0,1)
tf.random.uniform([2, 4], minval=0, maxval=10, dtype=tf.int32)
tf.random.truncated_normal([2, 3])    # |x| < 2σ
tf.random.shuffle(tf.range(10))       # shuffle rank-1

Tensor Properties

t = tf.constant([[1.0, 2.0], [3.0, 4.0]])

t.shape           # TensorShape([2, 2])
t.shape[0]        # 2  (static)
t.dtype           # tf.float32
t.ndim            # 2
t.device          # '/job:localhost/replica:0/task:0/device:CPU:0'

tf.size(t)        # scalar: total elements (4)
tf.rank(t)        # scalar: 2

# Dynamic shape
tf.shape(t)       # Tensor([2, 2], dtype=int32)  — prefer for dynamic graphs

Indexing and Slicing

t = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

t[0]              # first row  → [1, 2, 3]
t[-1]             # last row   → [7, 8, 9]
t[0, 1]           # element    → 2
t[0:2]            # rows 0-1
t[:, 1]           # column 1   → [2, 5, 8]
t[::2, ::2]       # stride

Boolean / Fancy Indexing

mask = tf.constant([True, False, True])
tf.boolean_mask(t, mask, axis=0)      # rows 0 and 2

indices = tf.constant([2, 0])
tf.gather(t, indices)                  # rows 2 then 0

# nd gather
params = tf.constant([[10, 20], [30, 40]])
tf.gather_nd(params, [[0, 1], [1, 0]])  # → [20, 30]

Reshaping and Squeezing

t = tf.range(12, dtype=tf.float32)

tf.reshape(t, [3, 4])
tf.reshape(t, [3, -1])                 # infer last dim
tf.reshape(t, [-1])                    # flatten

# Expand / squeeze
tf.expand_dims(t, axis=0)             # (12,) → (1, 12)
tf.squeeze(tf.zeros([1, 3, 1]))       # (1,3,1) → (3,)
tf.squeeze(tf.zeros([1, 3, 1]), axis=0)   # (3, 1)

Type Casting

t = tf.constant([1, 2, 3])            # int32
tf.cast(t, tf.float32)                # → float32
tf.cast(t, tf.float16)
tf.cast(tf.constant(3.9), tf.int32)   # → 3  (truncates)

Copying and Converting

# Tensor → NumPy
arr = t.numpy()                        # requires eager mode

# NumPy → Tensor
t = tf.constant(arr)

# EagerTensor to Python scalar
val = t[0, 0].numpy()

# Deep copy
tf.identity(t)                         # new tensor, same values

Variables vs Constants

# Constants — immutable
c = tf.constant([1.0, 2.0])

# Variables — mutable, tracked by GradientTape and Keras
v = tf.Variable([1.0, 2.0])
v.assign([3.0, 4.0])
v.assign_add([1.0, 0.0])
v.assign_sub([0.0, 1.0])

# tf.Variable properties
v.trainable        # True by default
v.shape
v.dtype

Gotcha: tf.Variable cannot change shape after creation. Use validate_shape=False carefully, and prefer assign over = for in-place updates inside tf.function.

Sparse and Ragged Tensors

SparseTensor

# Store only non-zero values
st = tf.SparseTensor(
    indices=[[0, 1], [1, 2]],
    values=[5.0, 7.0],
    dense_shape=[3, 4]
)
tf.sparse.to_dense(st)

RaggedTensor

# Variable-length rows
rt = tf.ragged.constant([[1, 2, 3], [4], [5, 6]])
rt.shape           # (3, None)
rt[0]              # [1, 2, 3]
rt.flat_values     # all values as 1-D

tf.ragged.stack([tf.constant([1, 2]), tf.constant([3, 4, 5])])

Memory and Device Placement

# List available devices
tf.config.list_physical_devices()
tf.config.list_physical_devices('GPU')

# Force a device
with tf.device('/GPU:0'):
    t = tf.constant([1.0, 2.0])

# Move to another device
t2 = tf.identity(t)   # remains on same device; explicit copy:
t_gpu = t + tf.zeros_like(t)  # ops route automatically