Tensor Operations: Selection & Filtering 🎯
The Treasure Hunt Analogy
Imagine you have a giant toy box filled with colorful marbles. Some are red, some blue, some big, some small. Now imagine you want to:
- Find all the red marbles (comparison)
- Pick only the marbles that are bigger than your thumb (conditional selection)
- Arrange them from smallest to biggest (sorting)
That’s exactly what tensor selection and filtering does in PyTorch! Your tensor is the toy box, and you’re the smart kid who knows exactly how to find what you need.
1. Tensor Comparison Operations 🔍
What Are Comparisons?
Comparisons are like asking yes/no questions about your data.
“Is this marble red?” → Yes or No “Is this number bigger than 5?” → True or False
In PyTorch, when you compare tensors, you get back a boolean tensor (a tensor of True and False values).
The Six Magic Questions
import torch
a = torch.tensor([1, 5, 3, 8, 2])
b = torch.tensor([2, 5, 1, 6, 2])
# Greater than: "Is a bigger than b?"
print(a > b) # [False, False, True, True, False]
# Less than: "Is a smaller than b?"
print(a < b) # [True, False, False, False, False]
# Equal: "Are they the same?"
print(a == b) # [False, True, False, False, True]
# Not equal: "Are they different?"
print(a != b) # [True, False, True, True, False]
# Greater or equal
print(a >= b) # [False, True, True, True, True]
# Less or equal
print(a <= b) # [True, True, False, False, True]
Compare with a Single Number
You can also compare your whole tensor against one number:
scores = torch.tensor([85, 42, 91, 67, 73])
# Who passed? (score >= 60)
passed = scores >= 60
print(passed) # [True, False, True, True, True]
Think of it like a teacher checking: “Did each student pass?”
PyTorch Function Versions
x = torch.tensor([3, 7, 2])
torch.gt(x, 5) # Greater than 5
torch.lt(x, 5) # Less than 5
torch.eq(x, 3) # Equal to 3
torch.ne(x, 3) # Not equal to 3
2. Conditional Selection 🎪
The Magic Filter
Now here’s where it gets exciting! Once you have those True/False answers, you can use them as a magic filter to grab only the items you want.
numbers = torch.tensor([10, 25, 5, 30, 15])
# Create the mask (our filter)
mask = numbers > 12
# Use the mask to select
big_numbers = numbers[mask]
print(big_numbers) # tensor([25, 30, 15])
It’s like having magic glasses that only show you what you’re looking for!
torch.where() - The Smart Picker
torch.where() is like a magical sorting hat. It looks at each element and decides what to do based on a condition.
x = torch.tensor([1, 4, 2, 8, 3])
# If > 3, keep it. Otherwise, make it 0.
result = torch.where(x > 3, x, 0)
print(result) # tensor([0, 4, 0, 8, 0])
Real Example - Fixing Negative Scores:
scores = torch.tensor([85, -10, 92, -5, 78])
# Replace negatives with 0
fixed = torch.where(scores < 0, 0, scores)
print(fixed) # tensor([85, 0, 92, 0, 78])
torch.masked_select() - Extract Matching Items
data = torch.tensor([[1, 2], [3, 4], [5, 6]])
mask = data > 2
# Get all values where mask is True
selected = torch.masked_select(data, mask)
print(selected) # tensor([3, 4, 5, 6])
Note: This flattens the result into a 1D tensor!
Combining Multiple Conditions
Use & (and), | (or), ~ (not):
ages = torch.tensor([12, 25, 8, 45, 17])
# Teenagers (13-19)
teens = (ages >= 13) & (ages <= 19)
print(ages[teens]) # tensor([17])
# Kids OR seniors
special = (ages < 13) | (ages > 40)
print(ages[special]) # tensor([12, 8, 45])
⚠️ Important: Use parentheses around each condition!
3. Tensor Sorting and Selection 🏆
Basic Sorting
Sorting is like lining up kids by height - shortest to tallest (or the other way around).
messy = torch.tensor([3, 1, 4, 1, 5, 9, 2, 6])
# Sort ascending (small to big)
sorted_tensor = torch.sort(messy)
print(sorted_tensor.values) # [1, 1, 2, 3, 4, 5, 6, 9]
print(sorted_tensor.indices) # [1, 3, 6, 0, 2, 4, 7, 5]
The indices tell you where each number came from originally - super useful!
Descending Sort (Big to Small)
scores = torch.tensor([72, 95, 88, 64])
# Top scorers first
result = torch.sort(scores, descending=True)
print(result.values) # tensor([95, 88, 72, 64])
argsort() - Just the Positions
Sometimes you only need to know the order, not the actual sorted values:
prices = torch.tensor([25.0, 10.0, 30.0, 15.0])
# Get indices that would sort the tensor
order = torch.argsort(prices)
print(order) # tensor([1, 3, 0, 2])
# Meaning: cheapest is index 1, then 3, then 0, then 2
topk() - Find the Winners! 🥇
Want just the top 3 or bottom 5? Use topk():
scores = torch.tensor([82, 95, 67, 91, 78, 88])
# Top 3 scores
top3 = torch.topk(scores, k=3)
print(top3.values) # tensor([95, 91, 88])
print(top3.indices) # tensor([1, 3, 5])
Get the Bottom k:
# Bottom 2 scores
bottom2 = torch.topk(scores, k=2, largest=False)
print(bottom2.values) # tensor([67, 78])
kthvalue() - Find a Specific Rank
What’s the 3rd smallest value?
data = torch.tensor([50, 20, 80, 10, 60])
# Find the 3rd smallest
third = torch.kthvalue(data, k=3)
print(third.values) # tensor(50)
print(third.indices) # tensor(0)
Sorting 2D Tensors
matrix = torch.tensor([
[3, 1, 2],
[6, 4, 5]
])
# Sort each row
row_sorted = torch.sort(matrix, dim=1)
print(row_sorted.values)
# [[1, 2, 3],
# [4, 5, 6]]
# Sort each column
col_sorted = torch.sort(matrix, dim=0)
print(col_sorted.values)
# [[3, 1, 2],
# [6, 4, 5]]
Quick Reference Flow
graph LR A[Your Tensor] --> B{What do you need?} B -->|Compare values| C[Comparison Ops] B -->|Filter data| D[Conditional Selection] B -->|Order data| E[Sorting] C --> C1[>, <, ==, !=] C --> C2[Returns True/False tensor] D --> D1[Boolean Indexing] D --> D2[torch.where] D --> D3[torch.masked_select] E --> E1[torch.sort] E --> E2[torch.topk] E --> E3[torch.argsort]
The Complete Picture 🎨
Think of selection and filtering as your data detective toolkit:
| Task | Tool | What It Does |
|---|---|---|
| Ask questions | >, <, == |
Get True/False answers |
| Filter with answers | tensor[mask] |
Keep only True items |
| Smart replacement | torch.where() |
Choose between two options |
| Line up nicely | torch.sort() |
Arrange in order |
| Find the best | torch.topk() |
Get top k winners |
| Just the order | torch.argsort() |
Get sorting positions |
You Did It! 🎉
You now know how to:
- ✅ Compare tensors and get boolean results
- ✅ Select elements based on conditions
- ✅ Sort and find top values in your data
These operations are the building blocks of data preprocessing in deep learning. Every time a neural network needs to pick the best predictions or filter out bad data, it uses exactly these techniques!
Keep practicing, and soon these operations will feel as natural as picking your favorite toys from a toy box. 🧸