TensorFlow Tensor Operations: Your Magic Toolbox 🧰
Imagine you have a giant box of LEGO blocks. Each block has a number on it. TensorFlow lets you play with millions of these blocks at once — stacking them, adding them together, or finding the biggest one. Let’s learn all the cool tricks!
🎯 The Big Picture
Tensors are like organized boxes of numbers. Think of:
- A single LEGO block = a number (scalar)
- A row of blocks = a list (1D tensor)
- A flat LEGO plate = a table (2D tensor)
- A 3D LEGO castle = a cube of numbers (3D tensor)
Today, we’ll learn 7 super powers to work with these LEGO towers!
1. 🔍 Tensor Indexing and Slicing
What Is It?
Picking out specific blocks from your LEGO tower.
The Story
Imagine you have a bookshelf with 5 shelves, and each shelf has 4 books. Sometimes you want:
- Just the 3rd book on the 2nd shelf
- All books on shelf 1
- The first 2 books from every shelf
That’s indexing (picking one thing) and slicing (picking a range)!
How It Works
import tensorflow as tf
# A bookshelf: 3 shelves, 4 books each
shelf = tf.constant([
[10, 20, 30, 40],
[50, 60, 70, 80],
[90, 100, 110, 120]
])
# Get book at shelf 1, position 2
book = shelf[1, 2] # Result: 70
# Get ALL books on shelf 0
row = shelf[0] # [10, 20, 30, 40]
# Get first 2 books from shelf 2
slice_books = shelf[2, :2] # [90, 100]
🎨 Visual
graph TD A["Full Shelf<br/>[3 x 4]"] --> B["shelf[1, 2]<br/>= 70"] A --> C["shelf[0]<br/>= [10,20,30,40]"] A --> D["shelf[2, :2]<br/>= [90, 100]"]
Key Rules
| Syntax | Meaning |
|---|---|
t[i] |
Get row i |
t[i, j] |
Get item at row i, column j |
t[:, j] |
Get column j from all rows |
t[a:b] |
Get rows from a to b-1 |
2. 🔄 Tensor Shape Manipulation
What Is It?
Reshaping your LEGO tower without losing any blocks.
The Story
You have 12 LEGO blocks in a line. You can arrange them as:
- 1 row of 12
- 2 rows of 6
- 3 rows of 4
- 4 rows of 3
- 6 rows of 2
- A 3D cube (2 × 2 × 3)
The blocks stay the same — only the shape changes!
How It Works
# Start with 12 numbers in a line
line = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12])
# Reshape to 3 rows, 4 columns
table = tf.reshape(line, [3, 4])
# [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
# Reshape to 2x2x3 cube
cube = tf.reshape(line, [2, 2, 3])
# Flatten back to a line
flat = tf.reshape(cube, [-1]) # -1 means "figure it out"
🎨 Visual
graph TD A["[1,2,3,4,5,6,7,8,9,10,11,12]<br/>Shape: #40;12,#41;"] A -->|"reshape [3,4]"| B["[[1,2,3,4],<br/>[5,6,7,8],<br/>[9,10,11,12]]<br/>Shape: #40;3,4#41;"] A -->|"reshape [2,2,3]"| C["3D Cube<br/>Shape: #40;2,2,3#41;"] B -->|"reshape [-1]"| D["Back to line<br/>Shape: #40;12,#41;"]
Useful Shape Functions
| Function | What It Does |
|---|---|
tf.reshape(t, shape) |
Change shape |
tf.squeeze(t) |
Remove dimensions of size 1 |
tf.expand_dims(t, axis) |
Add a dimension |
t.shape |
Check current shape |
3. 🎭 Type Casting
What Is It?
Converting blocks from one material to another (plastic to wood, wood to metal).
The Story
Numbers come in different types:
- Integers (whole numbers): 1, 2, 3, -5
- Floats (decimals): 1.5, 3.14, -2.7
Sometimes you need to convert between them — like exchanging coins for bills!
How It Works
# Integer tensor
ages = tf.constant([25, 30, 35])
print(ages.dtype) # int32
# Convert to float
ages_float = tf.cast(ages, tf.float32)
# Result: [25.0, 30.0, 35.0]
# Float to integer (cuts off decimals!)
prices = tf.constant([19.99, 5.50, 12.75])
prices_int = tf.cast(prices, tf.int32)
# Result: [19, 5, 12] ← decimals lost!
# Check type before operations
a = tf.constant([1, 2, 3]) # int32
b = tf.constant([1.0, 2.0, 3.0]) # float32
# a + b would ERROR! Must cast first.
result = tf.cast(a, tf.float32) + b
Common Types
| Type | Description | Example |
|---|---|---|
tf.int32 |
Whole numbers | 1, -5, 100 |
tf.float32 |
Decimals (32-bit) | 3.14 |
tf.float64 |
Decimals (64-bit) | More precise |
tf.bool |
True/False | True |
tf.string |
Text | “hello” |
4. ➕ Tensor Arithmetic
What Is It?
Math with LEGO blocks — adding, subtracting, multiplying, dividing whole towers at once!
The Story
Imagine you have two identical LEGO plates with numbers:
- Plate A: [10, 20, 30]
- Plate B: [1, 2, 3]
Adding them means: add block 1 from A to block 1 from B, and so on. Result: [11, 22, 33]
It’s like having robot helpers that do the same math on every block simultaneously!
How It Works
a = tf.constant([10, 20, 30])
b = tf.constant([1, 2, 3])
# Addition
add = a + b # [11, 22, 33]
# Subtraction
sub = a - b # [9, 18, 27]
# Multiplication (element-wise)
mul = a * b # [10, 40, 90]
# Division
div = a / b # [10.0, 10.0, 10.0]
# Power
power = a ** 2 # [100, 400, 900]
# With a single number (scalar)
double = a * 2 # [20, 40, 60]
🎨 Visual
graph LR A["[10, 20, 30]"] B["[1, 2, 3]"] A -->|"+"| C["[11, 22, 33]"] A -->|"*"| D["[10, 40, 90]"]
Arithmetic Operations Table
| Operation | Symbol | TensorFlow Function |
|---|---|---|
| Add | + |
tf.add(a, b) |
| Subtract | - |
tf.subtract(a, b) |
| Multiply | * |
tf.multiply(a, b) |
| Divide | / |
tf.divide(a, b) |
| Power | ** |
tf.pow(a, b) |
| Modulo | % |
tf.math.mod(a, b) |
5. 📊 Tensor Reduction Operations
What Is It?
Summarizing your LEGO tower into fewer numbers — like finding the total, average, or biggest block.
The Story
You have test scores for 3 students across 4 subjects:
Math Science English Art
Alice 90 85 88 92
Bob 78 82 75 80
Carol 95 90 92 88
Reductions help you answer:
- What’s each student’s average? (reduce across subjects)
- What’s the highest score in each subject? (reduce across students)
- What’s the total of all scores? (reduce everything)
How It Works
scores = tf.constant([
[90, 85, 88, 92],
[78, 82, 75, 80],
[95, 90, 92, 88]
])
# Sum of ALL scores
total = tf.reduce_sum(scores) # 1035
# Average per student (axis=1 = across columns)
avg_per_student = tf.reduce_mean(scores, axis=1)
# [88.75, 78.75, 91.25]
# Max in each subject (axis=0 = across rows)
max_per_subject = tf.reduce_max(scores, axis=0)
# [95, 90, 92, 92]
# Find minimum
min_score = tf.reduce_min(scores) # 75
🎨 Visual
graph TD A["3x4 Score Table"] A -->|"reduce_sum#40;#41;"| B["1035<br/>#40;total#41;"] A -->|"reduce_mean#40;axis=1#41;"| C["[88.75, 78.75, 91.25]<br/>#40;student averages#41;"] A -->|"reduce_max#40;axis=0#41;"| D["[95, 90, 92, 92]<br/>#40;subject max#41;"]
Reduction Functions
| Function | What It Returns |
|---|---|
tf.reduce_sum() |
Sum |
tf.reduce_mean() |
Average |
tf.reduce_max() |
Maximum |
tf.reduce_min() |
Minimum |
tf.reduce_prod() |
Product |
6. 🔢 Matrix Multiplication
What Is It?
A special way to combine two tables that’s the heart of neural networks!
The Story
Matrix multiplication is NOT just multiplying matching blocks. It’s more like a voting system:
Imagine:
- Matrix A = 3 students’ skills in different areas
- Matrix B = how much each skill matters for different jobs
Matrix multiplication tells you: how well each student fits each job!
The Rule
For A (shape m×n) × B (shape n×p) → Result (shape m×p)
Key: The inner numbers must match! (n = n)
How It Works
# 2 students, 3 skills each
students = tf.constant([
[1, 2, 3], # Student A
[4, 5, 6] # Student B
]) # Shape: (2, 3)
# 3 skills, 2 jobs
job_weights = tf.constant([
[1, 0], # Skill 1
[0, 1], # Skill 2
[1, 1] # Skill 3
]) # Shape: (3, 2)
# Matrix multiplication
fit_scores = tf.matmul(students, job_weights)
# Result shape: (2, 2)
# [[4, 5], ← Student A fits
# [10, 11]] ← Student B fits
Calculation Breakdown
Student A, Job 1: (1×1) + (2×0) + (3×1) = 4
Student A, Job 2: (1×0) + (2×1) + (3×1) = 5
Student B, Job 1: (4×1) + (5×0) + (6×1) = 10
Student B, Job 2: (4×0) + (5×1) + (6×1) = 11
🎨 Visual
graph LR A["#40;2, 3#41;"] -->|"× #40;3, 2#41;"| B["#40;2, 2#41;"] C["Inner dims must match<br/>3 = 3 ✓"]
Ways to Multiply
| Method | Usage |
|---|---|
tf.matmul(a, b) |
Standard matrix multiply |
a @ b |
Shorthand (Python 3.5+) |
tf.tensordot(a, b, axes) |
Advanced contraction |
7. 📡 Tensor Broadcasting
What Is It?
TensorFlow’s magic to make mismatched shapes work together!
The Story
You want to add:
- A single number (1 block)
- To a whole row (5 blocks)
Normally, shapes must match. But broadcasting stretches the smaller tensor to fit!
It’s like having a magic copier that duplicates your single block 5 times automatically.
How It Works
# Adding a scalar to a vector
vector = tf.constant([1, 2, 3, 4, 5])
scalar = tf.constant(10)
result = vector + scalar
# [11, 12, 13, 14, 15]
# The 10 was "broadcast" to [10, 10, 10, 10, 10]
# Adding a column to each column of a matrix
matrix = tf.constant([
[1, 2, 3],
[4, 5, 6]
]) # Shape: (2, 3)
column = tf.constant([[10], [20]]) # Shape: (2, 1)
result = matrix + column
# [[11, 12, 13],
# [24, 25, 26]]
Broadcasting Rules
- Compare shapes from right to left
- Dimensions must be equal OR one of them is 1
- Size-1 dimensions get stretched
🎨 Visual
graph TD A["#40;2, 3#41; + #40;2, 1#41;"] A --> B["#40;2, 1#41; stretches to #40;2, 3#41;"] B --> C["Result: #40;2, 3#41;"]
Broadcasting Examples
| Shape A | Shape B | Result | Works? |
|---|---|---|---|
| (3,) | (1,) | (3,) | ✅ |
| (2, 3) | (3,) | (2, 3) | ✅ |
| (2, 3) | (2, 1) | (2, 3) | ✅ |
| (2, 3) | (3, 2) | Error! | ❌ |
🎁 Summary: Your 7 Super Powers
| Power | What It Does | Key Function |
|---|---|---|
| 🔍 Indexing/Slicing | Pick specific elements | t[i, j], t[a:b] |
| 🔄 Shape Manipulation | Rearrange dimensions | tf.reshape() |
| 🎭 Type Casting | Convert data types | tf.cast() |
| ➕ Arithmetic | Math on all elements | +, -, *, / |
| 📊 Reduction | Summarize to fewer values | tf.reduce_sum() |
| 🔢 Matrix Multiply | Combine matrices | tf.matmul() |
| 📡 Broadcasting | Auto-stretch shapes | Automatic! |
🚀 You Did It!
You now have 7 powerful tools in your TensorFlow toolbox. These operations are the building blocks for:
- Neural networks
- Image processing
- Natural language processing
- And so much more!
Remember: Every AI model is just these simple operations, stacked together cleverly. You’ve got this! 💪