C.W.K.
Stream
Lesson 06 of 07 · published

Indexing, Slicing, Reshaping

~12 min · indexing, slicing, reshape, expand-dims

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

Move data through shapes without losing your sanity

TF supports NumPy-style indexing and slicing identically. Where TF diverges is the dedicated reshape ops — tf.reshape, tf.expand_dims, tf.squeeze, tf.transpose — that you'll use constantly to move tensors through layers.

The batch dimension trick: most TF ops expect a leading batch dim. A single 224×224 RGB image with shape (224, 224, 3) needs to become (1, 224, 224, 3) before feeding into a model. Two equivalent ways: tf.expand_dims(image, axis=0) or image[tf.newaxis, ...].

The -1 trick: tf.reshape(x, [-1]) flattens any tensor to 1D. tf.reshape(x, [batch, -1]) flattens everything except the batch dim — useful right before a Dense layer.

Code

Indexing + slicing·python
import tensorflow as tf

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

print(t[0, 0])      # 1
print(t[1])         # [4 5 6]
print(t[0:2, 1:])   # [[2,3],[5,6]]
print(t[:, -1])     # [3, 6, 9]
print(t[::2, ::2])  # [[1,3],[7,9]]

# Index-based selection
indices = tf.constant([0, 2])
selected = tf.gather(t, indices)   # rows 0 and 2
Reshape / expand_dims / squeeze / transpose·python
x = tf.constant([[1.0, 2.0, 3.0],
                 [4.0, 5.0, 6.0]])     # shape (2, 3)

flat = tf.reshape(x, [6])              # (6,)
auto = tf.reshape(x, [-1])             # (6,) — auto-flatten
auto2 = tf.reshape(x, [3, -1])         # (3, 2)

y = tf.constant([1.0, 2.0, 3.0])       # (3,)
y_batch = y[tf.newaxis, :]             # (1, 3)
y_col = tf.expand_dims(y, axis=1)      # (3, 1)

z = tf.zeros([1, 3, 1])                # (1, 3, 1)
sq = tf.squeeze(z)                     # (3,)

m = tf.ones([2, 3, 4])
mt = tf.transpose(m, perm=[0, 2, 1])   # (2, 4, 3)
mt2 = tf.transpose(m)                  # reverse all → (4, 3, 2)

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.