Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a tensor in TensorFlow?
A tensor is a multi-dimensional array used to store data in TensorFlow. It can have any number of dimensions, like scalars (0D), vectors (1D), matrices (2D), or higher.
Click to reveal answer
beginner
How do you get the first element of a 1D tensor named t?
You use t[0] to get the first element, just like accessing the first item in a list.
Click to reveal answer
beginner
What does the slice t[1:4] do on a 1D tensor?
It extracts elements starting from index 1 up to, but not including, index 4. So it gets elements at positions 1, 2, and 3.
Click to reveal answer
intermediate
How do you select the second row and third column element from a 2D tensor t?
Use t[1, 2]. The first index is the row (starting at 0), the second is the column.
Click to reveal answer
intermediate
What does t[:, 1] mean for a 2D tensor t?
It selects all rows (the :) but only the second column (index 1). This extracts a 1D tensor of that column.
Click to reveal answer
What does t[2:5] return for a 1D tensor t?
AElements at indices 3, 4, and 5
BElements at indices 2, 3, and 4
CElements at indices 2, 3, 4, and 5
DElements at indices 0 to 5
✗ Incorrect
Slicing includes the start index (2) but excludes the end index (5), so it returns elements at 2, 3, and 4.
How do you select the last element of a 1D tensor t?
At[-1]
Bt[0]
Ct[len(t)]
Dt[1]
✗ Incorrect
Using -1 as the index selects the last element in the tensor.
What does t[0, :] select in a 2D tensor t?
ALast row, all columns
BAll rows, first column
CFirst element only
DFirst row, all columns
✗ Incorrect
The first index 0 selects the first row; the colon : selects all columns.
If t is a 3D tensor, what does t[:, 1, :] select?
AAll elements in the second slice of the second dimension
BOnly the first element
CAll elements in the first slice of the first dimension
DOnly the last element
✗ Incorrect
The colon : selects all in the first dimension, 1 selects the second index in the second dimension, and colon selects all in the third dimension.
What is the shape of the tensor after slicing t[:, 2:5] if t is shape (4, 6)?
A(4, 6)
B(3, 6)
C(4, 3)
D(3, 4)
✗ Incorrect
Slicing columns from index 2 to 5 (3 columns) keeps all 4 rows, so shape is (4, 3).
Explain how to use indexing and slicing to extract a sub-tensor from a 2D tensor in TensorFlow.
Think about how you pick rows and columns like selecting cells in a spreadsheet.
You got /4 concepts.
Describe how negative indices work when indexing tensors in TensorFlow.
Imagine counting backwards from the last item.
You got /4 concepts.
Practice
(1/5)
1. What does indexing a tensor in TensorFlow do?
easy
A. Selects a single element by its position
B. Changes the shape of the tensor
C. Adds new elements to the tensor
D. Deletes elements from the tensor
Solution
Step 1: Understand indexing
Indexing means picking one element from a tensor by its position, like choosing one item from a list.
Step 2: Compare with other options
Changing shape, adding, or deleting elements are different operations, not indexing.
Final Answer:
Selects a single element by its position -> Option A
Quick Check:
Indexing = single element pick [OK]
Hint: Indexing picks one element, slicing picks many [OK]
Common Mistakes:
Thinking indexing changes tensor shape
Confusing indexing with adding elements
Assuming indexing deletes elements
2. Which of the following is the correct syntax to slice a 1D tensor t from index 2 to 5 (exclusive) in TensorFlow?
easy
A. t[2:5]
B. t.slice(2, 5)
C. t[2, 5]
D. t.slice(2:5)
Solution
Step 1: Recall slicing syntax
TensorFlow uses Python-style slicing: t[start:stop] to get elements from start up to but not including stop.
Step 2: Check each option
t[2:5] uses correct Python slice syntax. t.slice(2, 5) and D use incorrect method calls or syntax. t[2, 5] uses comma which is invalid for 1D slicing.
Final Answer:
t[2:5] -> Option A
Quick Check:
Slice syntax = t[start:stop] [OK]
Hint: Use square brackets with colon for slicing [OK]
Common Mistakes:
Using commas instead of colons in slices
Trying to call slice as a method incorrectly
Confusing slice stop index as inclusive
3. Given the tensor t = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), what is the output of t[1:, :2].numpy()?
medium
A. [[5 6]
[8 9]]
B. [[1 2]
[4 5]]
C. [[4 5 6]
[7 8 9]]
D. [[4 5]
[7 8]]
Solution
Step 1: Understand slicing t[1:, :2]
1: means rows from index 1 to end (rows 1 and 2). :2 means columns from start to index 2 (columns 0 and 1).
Step 2: Extract the sliced elements
Rows 1 and 2 are [[4,5,6], [7,8,9]]. Taking first two columns gives [[4,5], [7,8]].
Final Answer:
[[4 5]
[7 8]] -> Option D
Quick Check:
Rows 1+ and cols 0-1 = [[4 5],[7 8]] [OK]
Hint: Remember slice stop is exclusive, so :2 means columns 0 and 1 [OK]
Common Mistakes:
Including column index 2 mistakenly
Starting rows from 0 instead of 1
Confusing rows and columns order
4. What is wrong with this TensorFlow slicing code?
t = tf.constant([10, 20, 30, 40, 50])
slice = t[1:6]
medium
A. Index 6 is out of range, causing an error
B. Slicing with stop index beyond length is allowed, no error
C. Syntax error due to missing colon
D. TensorFlow does not support slicing
Solution
Step 1: Check slicing behavior with stop index
In Python and TensorFlow, slicing stop index can be beyond tensor length without error; it stops at the end.
Step 2: Analyze given code
Tensor t has length 5, slicing 1:6 extracts elements from index 1 to end safely.
Final Answer:
Slicing with stop index beyond length is allowed, no error -> Option B
Quick Check:
Slice stop > length is safe [OK]
Hint: Slice stop can exceed length without error [OK]
Common Mistakes:
Expecting IndexError for slice stop beyond length
Confusing slicing with indexing single element
Thinking slicing syntax is invalid
5. You have a 3D tensor t = tf.constant([[[1,2],[3,4]], [[5,6],[7,8]], [[9,10],[11,12]]]). How do you extract the second element from each 2D matrix (i.e., elements 2, 4, 6, 8, 10, 12) using indexing and slicing?
hard
A. t[1, :, :]
B. t[:, 1, :]
C. t[:, :, 1]
D. t[:, 1]
Solution
Step 1: Understand tensor shape and indexing
The tensor shape is (3, 2, 2): 3 matrices, each 2x2. We want the second element in the last dimension (index 1).
Step 2: Apply slicing to get second element in last dimension
Using t[:, :, 1] selects all matrices (:), all rows (:), and the second element (index 1) in the last dimension.
Final Answer:
t[:, :, 1] -> Option C
Quick Check:
Last dim index 1 selects second elements [OK]
Hint: Use colon for all dims except last, index last dim 1 [OK]