Tensor Operations
Run the Tensor Operations Fullscreen
Edit the MicroSim with the p5.js editor
About This MicroSim
This visualization helps you understand how tensor shapes transform under common operations. Tensors are the fundamental data structures in deep learning, and manipulating their shapes is a crucial skill.
Operations Covered
| Operation | Description | Example |
|---|---|---|
| Reshape | Changes shape, preserves elements | (2,3,4) → (6,4) |
| Transpose | Permutes axes | (2,3,4) → (2,4,3) |
| Flatten | Collapses to 1D | (2,3,4) → (24,) |
| Squeeze | Removes size-1 dims | (1,3,1) → (3,) |
| Unsqueeze | Adds size-1 dim | (3,4) → (1,3,4) |
Key Principle
Total elements must be preserved in reshape operations:
\(\prod_i \text{input\_dim}_i = \prod_j \text{output\_dim}_j\)
For a (2,3,4) tensor: \(2 \times 3 \times 4 = 24\) elements
Interactive Features
- Dimension Sliders: Adjust input tensor dimensions
- Operation Selector: Choose which transformation to apply
- Apply Button: Animate the transformation
- Shape Annotations: See input and output shapes with element counts
Lesson Plan
Learning Objectives
Students will be able to:
- Predict output tensor shapes for common operations
- Verify that element counts are preserved in reshapes
- Choose the appropriate operation for a given shape transformation
- Debug shape mismatch errors in neural network code
Suggested Activities
- Reshape Chain: Starting from (2,3,4), reshape to (6,4), then (24,), then back to (2,3,4)
- Transpose Exploration: What happens when you transpose twice?
- Squeeze/Unsqueeze: Create a tensor (1,5,1,3), squeeze it, then unsqueeze back
- Invalid Reshape: Try to reshape (2,3,4) to (5,5). Why does it fail?
Discussion Questions
- Why might you need to flatten a tensor before a fully-connected layer?
- When would you use unsqueeze vs reshape to add a dimension?
- How does transpose relate to matrix transpose from linear algebra?
- What does "contiguous" mean for tensors after transpose?
Common Patterns in Deep Learning
1 2 3 4 5 6 7 8 | |
References
- PyTorch Documentation: Tensor Views and Reshaping
- NumPy Documentation: Array Manipulation Routines