# --- # jupyter: # jupytext: # text_representation: # extension: .py # format_name: percent # format_version: '1.3' # jupytext_version: 1.17.1 # kernelspec: # display_name: Python 3 (ipykernel) # language: python # name: python3 # --- # %% [markdown] """ # Module 01: Tensor Foundation - Building Blocks of ML Welcome to Module 01! You're about to build the foundational Tensor class that powers all machine learning operations. ## πŸ”— Prerequisites & Progress **You've Built**: Nothing - this is our foundation! **You'll Build**: A complete Tensor class with arithmetic, matrix operations, and shape manipulation **You'll Enable**: Foundation for activations, layers, and all future neural network components **Connection Map**: ``` NumPy Arrays β†’ Tensor β†’ Activations (Module 02) (raw data) (ML ops) (intelligence) ``` ## Learning Objectives By the end of this module, you will: 1. Implement a complete Tensor class with fundamental operations 2. Understand tensors as the universal data structure in ML 3. Test tensor operations with immediate validation 4. Prepare for gradient computation in Module 05 Let's get started! ## πŸ“¦ Where This Code Lives in the Final Package **Learning Side:** You work in modules/01_tensor/tensor_dev.py **Building Side:** Code exports to tinytorch.core.tensor ```python # Final package structure: from tinytorch.core.tensor import Tensor # This module - foundation for everything # Future modules will import and extend this Tensor ``` **Why this matters:** - **Learning:** Complete tensor system in one focused module for deep understanding - **Production:** Proper organization like PyTorch's torch.Tensor with all core operations together - **Consistency:** All tensor operations and data manipulation in core.tensor - **Integration:** Foundation that every other module will build upon """ # %% nbgrader={"grade": false, "grade_id": "imports", "solution": true} #| default_exp core.tensor import numpy as np # %% [markdown] """ ## 1. Introduction: What is a Tensor? A tensor is a multi-dimensional array that serves as the fundamental data structure in machine learning. Think of it as a universal container that can hold data in different dimensions: ``` Tensor Dimensions: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ 0D: Scalar β”‚ 5.0 (just a number) β”‚ 1D: Vector β”‚ [1, 2, 3] (list of numbers) β”‚ 2D: Matrix β”‚ [[1, 2] (grid of numbers) β”‚ β”‚ [3, 4]] β”‚ 3D: Cube β”‚ [[[... (stack of matrices) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` In machine learning, tensors flow through operations like water through pipes: ``` Neural Network Data Flow: Input Tensor β†’ Layer 1 β†’ Activation β†’ Layer 2 β†’ ... β†’ Output Tensor [batch, [batch, [batch, [batch, [batch, features] hidden] hidden] hidden2] classes] ``` Every neural network, from simple linear regression to modern transformers, processes tensors. Understanding tensors means understanding the foundation of all ML computations. ### Why Tensors Matter in ML Systems In production ML systems, tensors carry more than just data - they carry the computational graph, memory layout information, and execution context: ``` Real ML Pipeline: Raw Data β†’ Preprocessing β†’ Tensor Creation β†’ Model Forward Pass β†’ Loss Computation ↓ ↓ ↓ ↓ ↓ Files NumPy Arrays Tensors GPU Tensors Scalar Loss ``` **Key Insight**: Tensors bridge the gap between mathematical concepts and efficient computation on modern hardware. """ # %% [markdown] """ ## 2. Foundations: Mathematical Background ### Core Operations We'll Implement Our Tensor class will support all fundamental operations that neural networks need: ``` Operation Types: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Element-wise β”‚ Matrix Ops β”‚ Shape Ops β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ + Addition β”‚ @ Matrix Mult β”‚ .reshape() β”‚ β”‚ - Subtraction β”‚ .transpose() β”‚ .sum() β”‚ β”‚ * Multiplicationβ”‚ β”‚ .mean() β”‚ β”‚ / Division β”‚ β”‚ .max() β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Broadcasting: Making Tensors Work Together Broadcasting automatically aligns tensors of different shapes for operations: ``` Broadcasting Examples: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Scalar + Vector: β”‚ β”‚ 5 + [1, 2, 3] β†’ [5, 5, 5] + [1, 2, 3] = [6, 7, 8]β”‚ β”‚ β”‚ β”‚ Matrix + Vector (row-wise): β”‚ β”‚ [[1, 2]] [10] [[1, 2]] [[10, 10]] [[11, 12]] β”‚ β”‚ [[3, 4]] + [10] = [[3, 4]] + [[10, 10]] = [[13, 14]] β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` **Memory Layout**: NumPy uses row-major (C-style) storage where elements are stored row by row in memory for cache efficiency: ``` Memory Layout (2Γ—3 matrix): Matrix: Memory: [[1, 2, 3] [1][2][3][4][5][6] [4, 5, 6]] ↑ Row 1 ↑ Row 2 Cache Behavior: Sequential Access: Fast (uses cache lines efficiently) Row access: [1][2][3] β†’ cache hit, hit, hit Random Access: Slow (cache misses) Column access: [1][4] β†’ cache hit, miss ``` This memory layout affects performance in real ML workloads - algorithms that access data sequentially run faster than those that access randomly. """ # %% [markdown] """ ## 3. Implementation: Building Tensor Foundation Let's build our Tensor class step by step, testing each component as we go. **Key Design Decision**: We'll include gradient-related attributes from the start, but they'll remain dormant until Module 05. This ensures a consistent interface throughout the course while keeping the cognitive load manageable. ### Tensor Class Architecture ``` Tensor Class Structure: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Core Attributes: β”‚ β”‚ β€’ data: np.array (the numbers) β”‚ β”‚ β€’ shape: tuple (dimensions) β”‚ β”‚ β€’ size: int (total elements) β”‚ β”‚ β€’ dtype: type (float32, int64) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Gradient Attributes (dormant): β”‚ β”‚ β€’ requires_grad: bool β”‚ β”‚ β€’ grad: None (until Module 05) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Operations: β”‚ β”‚ β€’ __add__, __sub__, __mul__ β”‚ β”‚ β€’ matmul(), reshape() β”‚ β”‚ β€’ sum(), mean(), max() β”‚ β”‚ β€’ __repr__(), __str__() β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` The beauty of this design: **all methods are defined inside the class from day one**. No monkey-patching, no dynamic attribute addition. Clean, consistent, debugger-friendly. """ # %% [markdown] """ ### Tensor Creation and Initialization Before we implement operations, let's understand how tensors store data and manage their attributes. This initialization is the foundation that everything else builds upon. ``` Tensor Initialization Process: Input Data β†’ Validation β†’ NumPy Array β†’ Tensor Wrapper β†’ Ready for Operations [1,2,3] β†’ types β†’ np.array β†’ shape=(3,) β†’ + - * / @ ... ↓ ↓ ↓ ↓ List/Array Type Check Memory Attributes Set (optional) Allocation Memory Allocation Example: Input: [[1, 2, 3], [4, 5, 6]] ↓ NumPy allocates: [1][2][3][4][5][6] in contiguous memory ↓ Tensor wraps with: shape=(2,3), size=6, dtype=int64 ``` **Key Design Principle**: Our Tensor is a wrapper around NumPy arrays that adds ML-specific functionality. We leverage NumPy's battle-tested memory management and computation kernels while adding the gradient tracking and operation chaining needed for deep learning. **Why This Approach?** - **Performance**: NumPy's C implementations are highly optimized - **Compatibility**: Easy integration with scientific Python ecosystem - **Memory Efficiency**: No unnecessary data copying - **Future-Proof**: Easy transition to GPU tensors in advanced modules """ # %% nbgrader={"grade": false, "grade_id": "tensor-class", "solution": true} class Tensor: """Educational tensor that grows with student knowledge. This class starts simple but includes dormant features for future modules: - requires_grad: Will be used for automatic differentiation (Module 05) - grad: Will store computed gradients (Module 05) - backward(): Will compute gradients (Module 05) For now, focus on: data, shape, and basic operations. """ def __init__(self, data, requires_grad=False): """ Create a new tensor from data. TODO: Initialize tensor attributes APPROACH: 1. Convert data to NumPy array - handles lists, scalars, etc. 2. Store shape and size for quick access 3. Set up gradient tracking (dormant until Module 05) EXAMPLE: >>> tensor = Tensor([1, 2, 3]) >>> print(tensor.data) [1 2 3] >>> print(tensor.shape) (3,) HINT: np.array() handles type conversion automatically """ ### BEGIN SOLUTION # Core tensor data - always present self.data = np.array(data, dtype=np.float32) # Consistent float32 for ML self.shape = self.data.shape self.size = self.data.size self.dtype = self.data.dtype # Gradient features (dormant until Module 05) self.requires_grad = requires_grad self.grad = None ### END SOLUTION def __repr__(self): """String representation of tensor for debugging.""" grad_info = f", requires_grad={self.requires_grad}" if self.requires_grad else "" return f"Tensor(data={self.data}, shape={self.shape}{grad_info})" def __str__(self): """Human-readable string representation.""" return f"Tensor({self.data})" # %% nbgrader={"grade": false, "grade_id": "addition-impl", "solution": true} def __add__(self, other): """ Add two tensors element-wise with broadcasting support. TODO: Implement tensor addition with automatic broadcasting APPROACH: 1. Handle both Tensor and scalar inputs 2. Use NumPy's broadcasting for automatic shape alignment 3. Return new Tensor with result (don't modify self) EXAMPLE: >>> a = Tensor([1, 2, 3]) >>> b = Tensor([4, 5, 6]) >>> result = a + b >>> print(result.data) [5. 7. 9.] BROADCASTING EXAMPLE: >>> matrix = Tensor([[1, 2], [3, 4]]) # Shape: (2, 2) >>> vector = Tensor([10, 20]) # Shape: (2,) >>> result = matrix + vector # Broadcasting: (2,2) + (2,) β†’ (2,2) >>> print(result.data) [[11. 22.] [13. 24.]] HINTS: - Use isinstance() to check if other is a Tensor - NumPy handles broadcasting automatically with + - Always return a new Tensor, don't modify self - Preserve gradient tracking for future modules """ ### BEGIN SOLUTION if isinstance(other, Tensor): # Tensor + Tensor: let NumPy handle broadcasting result_data = self.data + other.data else: # Tensor + scalar: NumPy broadcasts automatically result_data = self.data + other # Create new tensor with result result = Tensor(result_data) # Preserve gradient tracking if either operand requires gradients if hasattr(self, 'requires_grad') and hasattr(other, 'requires_grad'): result.requires_grad = self.requires_grad or (isinstance(other, Tensor) and other.requires_grad) elif hasattr(self, 'requires_grad'): result.requires_grad = self.requires_grad return result ### END SOLUTION # %% nbgrader={"grade": false, "grade_id": "more-arithmetic", "solution": true} def __sub__(self, other): """ Subtract two tensors element-wise. Common use: Centering data (x - mean), computing differences for loss functions. """ if isinstance(other, Tensor): return Tensor(self.data - other.data) else: return Tensor(self.data - other) def __mul__(self, other): """ Multiply two tensors element-wise (NOT matrix multiplication). Common use: Scaling features, applying masks, gating mechanisms in neural networks. Note: This is * operator, not @ (which will be matrix multiplication). """ if isinstance(other, Tensor): return Tensor(self.data * other.data) else: return Tensor(self.data * other) def __truediv__(self, other): """ Divide two tensors element-wise. Common use: Normalization (x / std), converting counts to probabilities. """ if isinstance(other, Tensor): return Tensor(self.data / other.data) else: return Tensor(self.data / other) # %% nbgrader={"grade": false, "grade_id": "matmul-impl", "solution": true} def matmul(self, other): """ Matrix multiplication of two tensors. TODO: Implement matrix multiplication using np.dot with proper validation APPROACH: 1. Validate inputs are Tensors 2. Check dimension compatibility (inner dimensions must match) 3. Use np.dot for optimized computation 4. Return new Tensor with result EXAMPLE: >>> a = Tensor([[1, 2], [3, 4]]) # 2Γ—2 >>> b = Tensor([[5, 6], [7, 8]]) # 2Γ—2 >>> result = a.matmul(b) # 2Γ—2 result >>> # Result: [[1Γ—5+2Γ—7, 1Γ—6+2Γ—8], [3Γ—5+4Γ—7, 3Γ—6+4Γ—8]] = [[19, 22], [43, 50]] SHAPE RULES: - (M, K) @ (K, N) β†’ (M, N) βœ“ Valid - (M, K) @ (J, N) β†’ Error βœ— K β‰  J COMPLEXITY: O(MΓ—NΓ—K) for (MΓ—K) @ (KΓ—N) matrices HINTS: - np.dot handles the optimization for us - Check self.shape[-1] == other.shape[-2] for compatibility - Provide clear error messages for debugging """ ### BEGIN SOLUTION if not isinstance(other, Tensor): raise TypeError(f"Expected Tensor for matrix multiplication, got {type(other)}") # Handle edge cases if self.shape == () or other.shape == (): # Scalar multiplication return Tensor(self.data * other.data) # For matrix multiplication, we need at least 1D tensors if len(self.shape) == 0 or len(other.shape) == 0: return Tensor(self.data * other.data) # Check dimension compatibility for matrix multiplication if len(self.shape) >= 2 and len(other.shape) >= 2: if self.shape[-1] != other.shape[-2]: raise ValueError( f"Cannot perform matrix multiplication: {self.shape} @ {other.shape}. " f"Inner dimensions must match: {self.shape[-1]} β‰  {other.shape[-2]}. " f"πŸ’‘ HINT: For (M,K) @ (K,N) β†’ (M,N), the K dimensions must be equal." ) elif len(self.shape) == 1 and len(other.shape) == 2: # Vector @ Matrix if self.shape[0] != other.shape[0]: raise ValueError( f"Cannot multiply vector {self.shape} with matrix {other.shape}. " f"Vector length {self.shape[0]} must match matrix rows {other.shape[0]}." ) elif len(self.shape) == 2 and len(other.shape) == 1: # Matrix @ Vector if self.shape[1] != other.shape[0]: raise ValueError( f"Cannot multiply matrix {self.shape} with vector {other.shape}. " f"Matrix columns {self.shape[1]} must match vector length {other.shape[0]}." ) # Perform optimized matrix multiplication result_data = np.dot(self.data, other.data) return Tensor(result_data) ### END SOLUTION # %% nbgrader={"grade": false, "grade_id": "shape-ops", "solution": true} def reshape(self, *shape): """ Reshape tensor to new dimensions. TODO: Implement tensor reshaping with validation APPROACH: 1. Handle different calling conventions: reshape(2, 3) vs reshape((2, 3)) 2. Validate total elements remain the same 3. Use NumPy's reshape for the actual operation 4. Return new Tensor (keep immutability) EXAMPLE: >>> tensor = Tensor([1, 2, 3, 4, 5, 6]) # Shape: (6,) >>> reshaped = tensor.reshape(2, 3) # Shape: (2, 3) >>> print(reshaped.data) [[1. 2. 3.] [4. 5. 6.]] COMMON USAGE: >>> # Flatten for MLP input >>> image = Tensor(np.random.rand(3, 32, 32)) # (channels, height, width) >>> flattened = image.reshape(-1) # (3072,) - all pixels in vector >>> >>> # Prepare batch for convolution >>> batch = Tensor(np.random.rand(32, 784)) # (batch, features) >>> images = batch.reshape(32, 1, 28, 28) # (batch, channels, height, width) HINTS: - Handle both reshape(2, 3) and reshape((2, 3)) calling styles - Check np.prod(new_shape) == self.size for validation - Use descriptive error messages for debugging """ ### BEGIN SOLUTION # Handle both reshape(2, 3) and reshape((2, 3)) calling conventions if len(shape) == 1 and isinstance(shape[0], (tuple, list)): new_shape = tuple(shape[0]) else: new_shape = shape # Handle -1 for automatic dimension inference (like NumPy) if -1 in new_shape: if new_shape.count(-1) > 1: raise ValueError("Can only specify one unknown dimension with -1") # Calculate the unknown dimension known_size = 1 unknown_idx = new_shape.index(-1) for i, dim in enumerate(new_shape): if i != unknown_idx: known_size *= dim unknown_dim = self.size // known_size new_shape = list(new_shape) new_shape[unknown_idx] = unknown_dim new_shape = tuple(new_shape) # Validate total elements remain the same if np.prod(new_shape) != self.size: raise ValueError( f"Cannot reshape tensor of size {self.size} to shape {new_shape}. " f"Total elements must match: {self.size} β‰  {np.prod(new_shape)}. " f"πŸ’‘ HINT: Make sure new_shape dimensions multiply to {self.size}" ) # Reshape the data (NumPy handles the memory layout efficiently) reshaped_data = np.reshape(self.data, new_shape) return Tensor(reshaped_data) ### END SOLUTION def transpose(self, dim0=None, dim1=None): """ Transpose tensor dimensions. TODO: Implement tensor transposition APPROACH: 1. Handle default case (transpose last two dimensions) 2. Handle specific dimension swapping 3. Use NumPy's transpose with proper axis specification 4. Return new Tensor EXAMPLE: >>> matrix = Tensor([[1, 2, 3], [4, 5, 6]]) # (2, 3) >>> transposed = matrix.transpose() # (3, 2) >>> print(transposed.data) [[1. 4.] [2. 5.] [3. 6.]] NEURAL NETWORK USAGE: >>> # Weight matrix transpose for backward pass >>> W = Tensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]]) # (3, 2) >>> W_T = W.transpose() # (2, 3) - for gradient computation >>> >>> # Attention mechanism >>> Q = Tensor([[1, 2], [3, 4]]) # queries (2, 2) >>> K = Tensor([[5, 6], [7, 8]]) # keys (2, 2) >>> attention_scores = Q.matmul(K.transpose()) # Q @ K^T HINTS: - Default: transpose last two dimensions (most common case) - Use np.transpose() with axes parameter - Handle 1D tensors gracefully (transpose is identity) """ ### BEGIN SOLUTION if dim0 is None and dim1 is None: # Default: transpose last two dimensions if len(self.shape) < 2: # For 1D tensors, transpose is identity operation return Tensor(self.data.copy()) else: # Transpose last two dimensions (most common in ML) axes = list(range(len(self.shape))) axes[-2], axes[-1] = axes[-1], axes[-2] transposed_data = np.transpose(self.data, axes) else: # Specific dimensions to transpose if dim0 is None or dim1 is None: raise ValueError("Both dim0 and dim1 must be specified for specific dimension transpose") # Validate dimensions exist if dim0 >= len(self.shape) or dim1 >= len(self.shape) or dim0 < 0 or dim1 < 0: raise ValueError( f"Dimension out of range for tensor with shape {self.shape}. " f"Got dim0={dim0}, dim1={dim1}, but tensor has {len(self.shape)} dimensions." ) # Create axes list and swap the specified dimensions axes = list(range(len(self.shape))) axes[dim0], axes[dim1] = axes[dim1], axes[dim0] transposed_data = np.transpose(self.data, axes) return Tensor(transposed_data) ### END SOLUTION # %% nbgrader={"grade": false, "grade_id": "reduction-ops", "solution": true} def sum(self, axis=None, keepdims=False): """ Sum tensor along specified axis. TODO: Implement tensor sum with axis control APPROACH: 1. Use NumPy's sum with axis parameter 2. Handle axis=None (sum all elements) vs specific axis 3. Support keepdims to maintain shape for broadcasting 4. Return new Tensor with result EXAMPLE: >>> tensor = Tensor([[1, 2], [3, 4]]) >>> total = tensor.sum() # Sum all elements: 10 >>> col_sum = tensor.sum(axis=0) # Sum columns: [4, 6] >>> row_sum = tensor.sum(axis=1) # Sum rows: [3, 7] NEURAL NETWORK USAGE: >>> # Batch loss computation >>> batch_losses = Tensor([0.1, 0.3, 0.2, 0.4]) # Individual losses >>> total_loss = batch_losses.sum() # Total: 1.0 >>> avg_loss = batch_losses.mean() # Average: 0.25 >>> >>> # Global average pooling >>> feature_maps = Tensor(np.random.rand(32, 256, 7, 7)) # (batch, channels, h, w) >>> global_features = feature_maps.sum(axis=(2, 3)) # (batch, channels) HINTS: - np.sum handles all the complexity for us - axis=None sums all elements (returns scalar) - axis=0 sums along first dimension, axis=1 along second, etc. - keepdims=True preserves dimensions for broadcasting """ ### BEGIN SOLUTION result = np.sum(self.data, axis=axis, keepdims=keepdims) return Tensor(result) ### END SOLUTION def mean(self, axis=None, keepdims=False): """ Compute mean of tensor along specified axis. Common usage: Batch normalization, loss averaging, global pooling. """ ### BEGIN SOLUTION result = np.mean(self.data, axis=axis, keepdims=keepdims) return Tensor(result) ### END SOLUTION def max(self, axis=None, keepdims=False): """ Find maximum values along specified axis. Common usage: Max pooling, finding best predictions, activation clipping. """ ### BEGIN SOLUTION result = np.max(self.data, axis=axis, keepdims=keepdims) return Tensor(result) ### END SOLUTION # %% nbgrader={"grade": false, "grade_id": "gradient-placeholder", "solution": true} def backward(self): """ Compute gradients (implemented in Module 05: Autograd). TODO: Placeholder implementation for gradient computation STUDENT NOTE: This method exists but does nothing until Module 05: Autograd. Don't worry about it for now - focus on the basic tensor operations. In Module 05, we'll implement: - Gradient computation via chain rule - Automatic differentiation - Backpropagation through operations - Computation graph construction FUTURE IMPLEMENTATION PREVIEW: ```python def backward(self, gradient=None): # Module 05 will implement: # 1. Set gradient for this tensor # 2. Propagate to parent operations # 3. Apply chain rule recursively # 4. Accumulate gradients properly pass ``` CURRENT BEHAVIOR: >>> x = Tensor([1, 2, 3], requires_grad=True) >>> y = x * 2 >>> y.sum().backward() # Calls this method - does nothing >>> print(x.grad) # Still None None """ ### BEGIN SOLUTION # Placeholder - will be implemented in Module 05 # For now, just ensure it doesn't crash when called # This allows students to experiment with gradient syntax # without getting confusing errors about missing methods pass ### END SOLUTION # %% [markdown] """ ### πŸ§ͺ Unit Test: Tensor Creation This test validates our Tensor constructor works correctly with various data types and properly initializes all attributes. **What we're testing**: Basic tensor creation and attribute setting **Why it matters**: Foundation for all other operations - if creation fails, nothing works **Expected**: Tensor wraps data correctly with proper attributes and consistent dtype """ # %% nbgrader={"grade": true, "grade_id": "test-tensor-creation", "locked": true, "points": 10} def test_unit_tensor_creation(): """πŸ§ͺ Test Tensor creation with various data types.""" print("πŸ§ͺ Unit Test: Tensor Creation...") # Test scalar creation scalar = Tensor(5.0) assert scalar.data == 5.0 assert scalar.shape == () assert scalar.size == 1 assert scalar.requires_grad == False assert scalar.grad is None assert scalar.dtype == np.float32 # Test vector creation vector = Tensor([1, 2, 3]) assert np.array_equal(vector.data, np.array([1, 2, 3], dtype=np.float32)) assert vector.shape == (3,) assert vector.size == 3 # Test matrix creation matrix = Tensor([[1, 2], [3, 4]]) assert np.array_equal(matrix.data, np.array([[1, 2], [3, 4]], dtype=np.float32)) assert matrix.shape == (2, 2) assert matrix.size == 4 # Test gradient flag (dormant feature) grad_tensor = Tensor([1, 2], requires_grad=True) assert grad_tensor.requires_grad == True assert grad_tensor.grad is None # Still None until Module 05 print("βœ… Tensor creation works correctly!") test_unit_tensor_creation() # %% [markdown] """ ## Element-wise Arithmetic Operations Element-wise operations are the workhorses of neural network computation. They apply the same operation to corresponding elements in tensors, often with broadcasting to handle different shapes elegantly. ### Why Element-wise Operations Matter In neural networks, element-wise operations appear everywhere: - **Activation functions**: Apply ReLU, sigmoid to every element - **Batch normalization**: Subtract mean, divide by std per element - **Loss computation**: Compare predictions vs. targets element-wise - **Gradient updates**: Add scaled gradients to parameters element-wise ### Element-wise Addition: The Foundation Addition is the simplest and most fundamental operation. Understanding it deeply helps with all others. ``` Element-wise Addition Visual: [1, 2, 3] + [4, 5, 6] = [1+4, 2+5, 3+6] = [5, 7, 9] Matrix Addition: [[1, 2]] [[5, 6]] [[1+5, 2+6]] [[6, 8]] [[3, 4]] + [[7, 8]] = [[3+7, 4+8]] = [[10, 12]] Broadcasting Addition (Matrix + Vector): [[1, 2]] [10] [[1, 2]] [[10, 10]] [[11, 12]] [[3, 4]] + [20] = [[3, 4]] + [[20, 20]] = [[23, 24]] ↑ ↑ ↑ ↑ ↑ (2,2) (2,1) (2,2) broadcast result Broadcasting Rules: 1. Start from rightmost dimension 2. Dimensions must be equal OR one must be 1 OR one must be missing 3. Missing dimensions are assumed to be 1 ``` **Key Insight**: Broadcasting makes tensors of different shapes compatible by automatically expanding dimensions. This is crucial for batch processing where you often add a single bias vector to an entire batch of data. **Memory Efficiency**: Broadcasting doesn't actually create expanded copies in memory - NumPy computes results on-the-fly, saving memory. """ # %% [markdown] """ ### Subtraction, Multiplication, and Division These operations follow the same pattern as addition, working element-wise with broadcasting support. Each serves specific purposes in neural networks: ``` Element-wise Operations in Neural Networks: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Subtraction β”‚ Multiplication β”‚ Division β”‚ Use Cases β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ [6,8] - [1,2] β”‚ [2,3] * [4,5] β”‚ [8,9] / [2,3] β”‚ β€’ Gradient β”‚ β”‚ = [5,6] β”‚ = [8,15] β”‚ = [4.0, 3.0] β”‚ computation β”‚ β”‚ β”‚ β”‚ β”‚ β€’ Normalization β”‚ β”‚ Center data: β”‚ Gate values: β”‚ Scale features: β”‚ β€’ Loss functionsβ”‚ β”‚ x - mean β”‚ x * mask β”‚ x / std β”‚ β€’ Attention β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Broadcasting with Scalars (very common in ML): [1, 2, 3] * 2 = [2, 4, 6] (scale all values) [1, 2, 3] - 1 = [0, 1, 2] (shift all values) [2, 4, 6] / 2 = [1, 2, 3] (normalize all values) Real ML Example - Batch Normalization: batch_data = [[1, 2], [3, 4], [5, 6]] # Shape: (3, 2) mean = [3, 4] # Shape: (2,) std = [2, 2] # Shape: (2,) # Normalize: (x - mean) / std normalized = (batch_data - mean) / std # Broadcasting: (3,2) - (2,) = (3,2), then (3,2) / (2,) = (3,2) ``` **Performance Note**: Element-wise operations are highly optimized in NumPy and run efficiently on modern CPUs with vectorization (SIMD instructions). """ # %% [markdown] """ ### πŸ§ͺ Unit Test: Arithmetic Operations This test validates our arithmetic operations work correctly with both tensor-tensor and tensor-scalar operations, including broadcasting behavior. **What we're testing**: Addition, subtraction, multiplication, division with broadcasting **Why it matters**: Foundation for neural network forward passes, batch processing, normalization **Expected**: Operations work with both tensors and scalars, proper broadcasting alignment """ # %% nbgrader={"grade": true, "grade_id": "test-arithmetic", "locked": true, "points": 15} def test_unit_arithmetic_operations(): """πŸ§ͺ Test arithmetic operations with broadcasting.""" print("πŸ§ͺ Unit Test: Arithmetic Operations...") # Test tensor + tensor a = Tensor([1, 2, 3]) b = Tensor([4, 5, 6]) result = a + b assert np.array_equal(result.data, np.array([5, 7, 9], dtype=np.float32)) # Test tensor + scalar (very common in ML) result = a + 10 assert np.array_equal(result.data, np.array([11, 12, 13], dtype=np.float32)) # Test broadcasting with different shapes (matrix + vector) matrix = Tensor([[1, 2], [3, 4]]) vector = Tensor([10, 20]) result = matrix + vector expected = np.array([[11, 22], [13, 24]], dtype=np.float32) assert np.array_equal(result.data, expected) # Test subtraction (data centering) result = b - a assert np.array_equal(result.data, np.array([3, 3, 3], dtype=np.float32)) # Test multiplication (scaling) result = a * 2 assert np.array_equal(result.data, np.array([2, 4, 6], dtype=np.float32)) # Test division (normalization) result = b / 2 assert np.array_equal(result.data, np.array([2.0, 2.5, 3.0], dtype=np.float32)) # Test chaining operations (common in ML pipelines) normalized = (a - 2) / 2 # Center and scale expected = np.array([-0.5, 0.0, 0.5], dtype=np.float32) assert np.allclose(normalized.data, expected) print("βœ… Arithmetic operations work correctly!") test_unit_arithmetic_operations() # %% [markdown] """ ## Matrix Multiplication: The Heart of Neural Networks Matrix multiplication is fundamentally different from element-wise multiplication. It's the operation that gives neural networks their power to transform and combine information across features. ### Why Matrix Multiplication is Central to ML Every neural network layer essentially performs matrix multiplication: ``` Linear Layer (the building block of neural networks): Input Features Γ— Weight Matrix = Output Features (N, D_in) Γ— (D_in, D_out) = (N, D_out) Real Example - Image Classification: Flattened Image Γ— Hidden Weights = Hidden Features (32, 784) Γ— (784, 256) = (32, 256) ↑ ↑ ↑ 32 images 784β†’256 transform 32 feature vectors ``` ### Matrix Multiplication Visualization ``` Matrix Multiplication Process: A (2Γ—3) B (3Γ—2) C (2Γ—2) β”Œ ┐ β”Œ ┐ β”Œ ┐ β”‚ 1 2 3 β”‚ β”‚ 7 8 β”‚ β”‚ 1Γ—7+2Γ—9+3Γ—1 β”‚ β”Œ ┐ β”‚ β”‚ Γ— β”‚ 9 1 β”‚ = β”‚ β”‚ = β”‚ 28 13β”‚ β”‚ 4 5 6 β”‚ β”‚ 1 2 β”‚ β”‚ 4Γ—7+5Γ—9+6Γ—1 β”‚ β”‚ 79 37β”‚ β”” β”˜ β”” β”˜ β”” β”˜ β”” β”˜ Computation Breakdown: C[0,0] = A[0,:] Β· B[:,0] = [1,2,3] Β· [7,9,1] = 1Γ—7 + 2Γ—9 + 3Γ—1 = 28 C[0,1] = A[0,:] Β· B[:,1] = [1,2,3] Β· [8,1,2] = 1Γ—8 + 2Γ—1 + 3Γ—2 = 13 C[1,0] = A[1,:] Β· B[:,0] = [4,5,6] Β· [7,9,1] = 4Γ—7 + 5Γ—9 + 6Γ—1 = 79 C[1,1] = A[1,:] Β· B[:,1] = [4,5,6] Β· [8,1,2] = 4Γ—8 + 5Γ—1 + 6Γ—2 = 37 Key Rule: Inner dimensions must match! A(m,n) @ B(n,p) = C(m,p) ↑ ↑ these must be equal ``` ### Computational Complexity and Performance ``` Computational Cost: For C = A @ B where A is (MΓ—K), B is (KΓ—N): - Multiplications: M Γ— N Γ— K - Additions: M Γ— N Γ— (K-1) β‰ˆ M Γ— N Γ— K - Total FLOPs: β‰ˆ 2 Γ— M Γ— N Γ— K Example: (1000Γ—1000) @ (1000Γ—1000) - FLOPs: 2 Γ— 1000Β³ = 2 billion operations - On 1 GHz CPU: ~2 seconds if no optimization - With optimized BLAS: ~0.1 seconds (20Γ— speedup!) Memory Access Pattern: A: MΓ—K (row-wise access) βœ“ Good cache locality B: KΓ—N (column-wise) βœ— Poor cache locality C: MΓ—N (row-wise write) βœ“ Good cache locality This is why optimized libraries like OpenBLAS, Intel MKL use: - Blocking algorithms (process in cache-sized chunks) - Vectorization (SIMD instructions) - Parallelization (multiple cores) ``` ### Neural Network Context ``` Multi-layer Neural Network: Input (batch=32, features=784) ↓ W1: (784, 256) Hidden1 (batch=32, features=256) ↓ W2: (256, 128) Hidden2 (batch=32, features=128) ↓ W3: (128, 10) Output (batch=32, classes=10) Each arrow represents a matrix multiplication: - Forward pass: 3 matrix multiplications - Backward pass: 3 more matrix multiplications (with transposes) - Total: 6 matrix mults per forward+backward pass For training batch: 32 Γ— (784Γ—256 + 256Γ—128 + 128Γ—10) FLOPs = 32 Γ— (200,704 + 32,768 + 1,280) = 32 Γ— 234,752 = 7.5M FLOPs per batch ``` This is why GPU acceleration matters - modern GPUs can perform thousands of these operations in parallel! """ # %% [markdown] """ ### πŸ§ͺ Unit Test: Matrix Multiplication This test validates matrix multiplication works correctly with proper shape checking and error handling. **What we're testing**: Matrix multiplication with shape validation and edge cases **Why it matters**: Core operation in neural networks (linear layers, attention mechanisms) **Expected**: Correct results for valid shapes, clear error messages for invalid shapes """ # %% nbgrader={"grade": true, "grade_id": "test-matmul", "locked": true, "points": 15} def test_unit_matrix_multiplication(): """πŸ§ͺ Test matrix multiplication operations.""" print("πŸ§ͺ Unit Test: Matrix Multiplication...") # Test 2Γ—2 matrix multiplication (basic case) a = Tensor([[1, 2], [3, 4]]) # 2Γ—2 b = Tensor([[5, 6], [7, 8]]) # 2Γ—2 result = a.matmul(b) # Expected: [[1Γ—5+2Γ—7, 1Γ—6+2Γ—8], [3Γ—5+4Γ—7, 3Γ—6+4Γ—8]] = [[19, 22], [43, 50]] expected = np.array([[19, 22], [43, 50]], dtype=np.float32) assert np.array_equal(result.data, expected) # Test rectangular matrices (common in neural networks) c = Tensor([[1, 2, 3], [4, 5, 6]]) # 2Γ—3 (like batch_size=2, features=3) d = Tensor([[7, 8], [9, 10], [11, 12]]) # 3Γ—2 (like features=3, outputs=2) result = c.matmul(d) # Expected: [[1Γ—7+2Γ—9+3Γ—11, 1Γ—8+2Γ—10+3Γ—12], [4Γ—7+5Γ—9+6Γ—11, 4Γ—8+5Γ—10+6Γ—12]] expected = np.array([[58, 64], [139, 154]], dtype=np.float32) assert np.array_equal(result.data, expected) # Test matrix-vector multiplication (common in forward pass) matrix = Tensor([[1, 2, 3], [4, 5, 6]]) # 2Γ—3 vector = Tensor([1, 2, 3]) # 3Γ—1 (conceptually) result = matrix.matmul(vector) # Expected: [1Γ—1+2Γ—2+3Γ—3, 4Γ—1+5Γ—2+6Γ—3] = [14, 32] expected = np.array([14, 32], dtype=np.float32) assert np.array_equal(result.data, expected) # Test shape validation - should raise clear error try: incompatible_a = Tensor([[1, 2]]) # 1Γ—2 incompatible_b = Tensor([[1], [2], [3]]) # 3Γ—1 incompatible_a.matmul(incompatible_b) # 1Γ—2 @ 3Γ—1 should fail (2 β‰  3) assert False, "Should have raised ValueError for incompatible shapes" except ValueError as e: assert "Inner dimensions must match" in str(e) assert "2 β‰  3" in str(e) # Should show specific dimensions print("βœ… Matrix multiplication works correctly!") test_unit_matrix_multiplication() # %% [markdown] """ ## Shape Manipulation: Reshape and Transpose Neural networks constantly change tensor shapes to match layer requirements. Understanding these operations is crucial for data flow through networks. ### Why Shape Manipulation Matters Real neural networks require constant shape changes: ``` CNN Data Flow Example: Input Image: (32, 3, 224, 224) # batch, channels, height, width ↓ Convolutional layers Feature Maps: (32, 512, 7, 7) # batch, features, spatial ↓ Global Average Pool Pooled: (32, 512, 1, 1) # batch, features, 1, 1 ↓ Flatten for classifier Flattened: (32, 512) # batch, features ↓ Linear classifier Output: (32, 1000) # batch, classes Each ↓ involves reshape or view operations! ``` ### Reshape: Changing Interpretation of the Same Data ``` Reshaping (changing dimensions without changing data): Original: [1, 2, 3, 4, 5, 6] (shape: (6,)) ↓ reshape(2, 3) Result: [[1, 2, 3], (shape: (2, 3)) [4, 5, 6]] Memory Layout (unchanged): Before: [1][2][3][4][5][6] After: [1][2][3][4][5][6] ← Same memory, different interpretation Key Insight: Reshape is O(1) operation - no data copying! Just changes how we interpret the memory layout. Common ML Reshapes: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Flatten for MLP β”‚ Unflatten for CNN β”‚ Batch Dimension β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ (N,H,W,C) β†’ (N,HΓ—WΓ—C) β”‚ (N,D) β†’ (N,H,W,C) β”‚ (H,W) β†’ (1,H,W) β”‚ β”‚ Images to vectors β”‚ Vectors to images β”‚ Add batch dimension β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Transpose: Swapping Dimensions ``` Transposing (swapping dimensions - data rearrangement): Original: [[1, 2, 3], (shape: (2, 3)) [4, 5, 6]] ↓ transpose() Result: [[1, 4], (shape: (3, 2)) [2, 5], [3, 6]] Memory Layout (rearranged): Before: [1][2][3][4][5][6] After: [1][4][2][5][3][6] ← Data actually moves in memory Key Insight: Transpose involves data movement - more expensive than reshape. Neural Network Usage: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Weight Matrices β”‚ Attention Mechanism β”‚ Gradient Computationβ”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Forward: X @ W β”‚ Q @ K^T attention β”‚ βˆ‚L/βˆ‚W = X^T @ βˆ‚L/βˆ‚Yβ”‚ β”‚ Backward: X @ W^T β”‚ scores β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Performance Implications ``` Operation Performance (for 1000Γ—1000 matrix): β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Operation β”‚ Time β”‚ Memory Access β”‚ Cache Behavior β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ reshape() β”‚ ~0.001 ms β”‚ No data copy β”‚ No cache impact β”‚ β”‚ transpose() β”‚ ~10 ms β”‚ Full data copy β”‚ Poor locality β”‚ β”‚ view() (future) β”‚ ~0.001 ms β”‚ No data copy β”‚ No cache impact β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Why transpose() is slower: - Must rearrange data in memory - Poor cache locality (accessing columns) - Can't be parallelized easily ``` This is why frameworks like PyTorch often use "lazy" transpose operations that defer the actual data movement until necessary. """ # %% [markdown] """ ### πŸ§ͺ Unit Test: Shape Manipulation This test validates reshape and transpose operations work correctly with validation and edge cases. **What we're testing**: Reshape and transpose operations with proper error handling **Why it matters**: Essential for data flow in neural networks, CNN/RNN architectures **Expected**: Correct shape changes, proper error handling for invalid operations """ # %% nbgrader={"grade": true, "grade_id": "test-shape-ops", "locked": true, "points": 15} def test_unit_shape_manipulation(): """πŸ§ͺ Test reshape and transpose operations.""" print("πŸ§ͺ Unit Test: Shape Manipulation...") # Test basic reshape (flatten β†’ matrix) tensor = Tensor([1, 2, 3, 4, 5, 6]) # Shape: (6,) reshaped = tensor.reshape(2, 3) # Shape: (2, 3) assert reshaped.shape == (2, 3) expected = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) assert np.array_equal(reshaped.data, expected) # Test reshape with tuple (alternative calling style) reshaped2 = tensor.reshape((3, 2)) # Shape: (3, 2) assert reshaped2.shape == (3, 2) expected2 = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.float32) assert np.array_equal(reshaped2.data, expected2) # Test reshape with -1 (automatic dimension inference) auto_reshaped = tensor.reshape(2, -1) # Should infer -1 as 3 assert auto_reshaped.shape == (2, 3) # Test reshape validation - should raise error for incompatible sizes try: tensor.reshape(2, 2) # 6 elements can't fit in 2Γ—2=4 assert False, "Should have raised ValueError" except ValueError as e: assert "Total elements must match" in str(e) assert "6 β‰  4" in str(e) # Test matrix transpose (most common case) matrix = Tensor([[1, 2, 3], [4, 5, 6]]) # (2, 3) transposed = matrix.transpose() # (3, 2) assert transposed.shape == (3, 2) expected = np.array([[1, 4], [2, 5], [3, 6]], dtype=np.float32) assert np.array_equal(transposed.data, expected) # Test 1D transpose (should be identity) vector = Tensor([1, 2, 3]) vector_t = vector.transpose() assert np.array_equal(vector.data, vector_t.data) # Test specific dimension transpose tensor_3d = Tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # (2, 2, 2) swapped = tensor_3d.transpose(0, 2) # Swap first and last dimensions assert swapped.shape == (2, 2, 2) # Same shape but data rearranged # Test neural network reshape pattern (flatten for MLP) batch_images = Tensor(np.random.rand(2, 3, 4)) # (batch=2, height=3, width=4) flattened = batch_images.reshape(2, -1) # (batch=2, features=12) assert flattened.shape == (2, 12) print("βœ… Shape manipulation works correctly!") test_unit_shape_manipulation() # %% [markdown] """ ## Reduction Operations: Aggregating Information Reduction operations collapse dimensions by aggregating data, which is essential for computing statistics, losses, and preparing data for different layers. ### Why Reductions are Crucial in ML Reduction operations appear throughout neural networks: ``` Common ML Reduction Patterns: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Loss Computation β”‚ Batch Normalization β”‚ Global Pooling β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Per-sample losses β†’ β”‚ Batch statistics β†’ β”‚ Feature maps β†’ β”‚ β”‚ Single batch loss β”‚ Normalization β”‚ Single features β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ losses.mean() β”‚ batch.mean(axis=0) β”‚ fmaps.mean(axis=(2,3))β”‚ β”‚ (N,) β†’ scalar β”‚ (N,D) β†’ (D,) β”‚ (N,C,H,W) β†’ (N,C) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Real Examples: β€’ Cross-entropy loss: -log(predictions).mean() [average over batch] β€’ Batch norm: (x - x.mean()) / x.std() [normalize each feature] β€’ Global avg pool: features.mean(dim=(2,3)) [spatial β†’ scalar per channel] ``` ### Understanding Axis Operations ``` Visual Axis Understanding: Matrix: [[1, 2, 3], All reductions operate on this data [4, 5, 6]] Shape: (2, 3) axis=0 (↓) β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” axis=1 β”‚ 1 2 3 β”‚ β†’ axis=1 reduces across columns (β†’) (β†’) β”‚ 4 5 6 β”‚ β†’ Result shape: (2,) [one value per row] β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ↓ ↓ ↓ axis=0 reduces down rows (↓) Result shape: (3,) [one value per column] Reduction Results: β”œβ”€ .sum() β†’ 21 (sum all: 1+2+3+4+5+6) β”œβ”€ .sum(axis=0) β†’ [5, 7, 9] (sum columns: [1+4, 2+5, 3+6]) β”œβ”€ .sum(axis=1) β†’ [6, 15] (sum rows: [1+2+3, 4+5+6]) β”œβ”€ .mean() β†’ 3.5 (average all: 21/6) β”œβ”€ .mean(axis=0) β†’ [2.5, 3.5, 4.5] (average columns) └─ .max() β†’ 6 (maximum element) 3D Tensor Example (batch, height, width): data.shape = (2, 3, 4) # 2 samples, 3Γ—4 images β”‚ β”œβ”€ .sum(axis=0) β†’ (3, 4) # Sum across batch dimension β”œβ”€ .sum(axis=1) β†’ (2, 4) # Sum across height dimension β”œβ”€ .sum(axis=2) β†’ (2, 3) # Sum across width dimension └─ .sum(axis=(1,2)) β†’ (2,) # Sum across both spatial dims (global pool) ``` ### Memory and Performance Considerations ``` Reduction Performance: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Operation β”‚ Time Complex β”‚ Memory Access β”‚ Cache Behavior β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ .sum() β”‚ O(N) β”‚ Sequential read β”‚ Excellent β”‚ β”‚ .sum(axis=0) β”‚ O(N) β”‚ Column access β”‚ Poor (strided) β”‚ β”‚ .sum(axis=1) β”‚ O(N) β”‚ Row access β”‚ Excellent β”‚ β”‚ .mean() β”‚ O(N) β”‚ Sequential read β”‚ Excellent β”‚ β”‚ .max() β”‚ O(N) β”‚ Sequential read β”‚ Excellent β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Why axis=0 is slower: - Accesses elements with large strides - Poor cache locality (jumping rows) - Less vectorization-friendly Optimization strategies: - Prefer axis=-1 operations when possible - Use keepdims=True to maintain shape for broadcasting - Consider reshaping before reduction for better cache behavior ``` """ # %% [markdown] """ ### πŸ§ͺ Unit Test: Reduction Operations This test validates reduction operations work correctly with axis control and maintain proper shapes. **What we're testing**: Sum, mean, max operations with axis parameter and keepdims **Why it matters**: Essential for loss computation, batch processing, and pooling operations **Expected**: Correct reduction along specified axes with proper shape handling """ # %% nbgrader={"grade": true, "grade_id": "test-reductions", "locked": true, "points": 10} def test_unit_reduction_operations(): """πŸ§ͺ Test reduction operations.""" print("πŸ§ͺ Unit Test: Reduction Operations...") matrix = Tensor([[1, 2, 3], [4, 5, 6]]) # Shape: (2, 3) # Test sum all elements (common for loss computation) total = matrix.sum() assert total.data == 21.0 # 1+2+3+4+5+6 assert total.shape == () # Scalar result # Test sum along axis 0 (columns) - batch dimension reduction col_sum = matrix.sum(axis=0) expected_col = np.array([5, 7, 9], dtype=np.float32) # [1+4, 2+5, 3+6] assert np.array_equal(col_sum.data, expected_col) assert col_sum.shape == (3,) # Test sum along axis 1 (rows) - feature dimension reduction row_sum = matrix.sum(axis=1) expected_row = np.array([6, 15], dtype=np.float32) # [1+2+3, 4+5+6] assert np.array_equal(row_sum.data, expected_row) assert row_sum.shape == (2,) # Test mean (average loss computation) avg = matrix.mean() assert np.isclose(avg.data, 3.5) # 21/6 assert avg.shape == () # Test mean along axis (batch normalization pattern) col_mean = matrix.mean(axis=0) expected_mean = np.array([2.5, 3.5, 4.5], dtype=np.float32) # [5/2, 7/2, 9/2] assert np.allclose(col_mean.data, expected_mean) # Test max (finding best predictions) maximum = matrix.max() assert maximum.data == 6.0 assert maximum.shape == () # Test max along axis (argmax-like operation) row_max = matrix.max(axis=1) expected_max = np.array([3, 6], dtype=np.float32) # [max(1,2,3), max(4,5,6)] assert np.array_equal(row_max.data, expected_max) # Test keepdims (important for broadcasting) sum_keepdims = matrix.sum(axis=1, keepdims=True) assert sum_keepdims.shape == (2, 1) # Maintains 2D shape expected_keepdims = np.array([[6], [15]], dtype=np.float32) assert np.array_equal(sum_keepdims.data, expected_keepdims) # Test 3D reduction (simulating global average pooling) tensor_3d = Tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # (2, 2, 2) spatial_mean = tensor_3d.mean(axis=(1, 2)) # Average across spatial dimensions assert spatial_mean.shape == (2,) # One value per batch item print("βœ… Reduction operations work correctly!") test_unit_reduction_operations() # %% [markdown] """ ## Gradient Features: Preparing for Module 05 Our Tensor includes dormant gradient features that will spring to life in Module 05. For now, they exist but do nothing - this design choice ensures a consistent interface throughout the course. ### Why Include Gradient Features Now? ``` Gradient System Evolution: Module 01: Tensor with dormant gradients β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Tensor β”‚ β”‚ β€’ data: actual values β”‚ β”‚ β€’ requires_grad: False β”‚ ← Present but unused β”‚ β€’ grad: None β”‚ ← Present but stays None β”‚ β€’ backward(): pass β”‚ ← Present but does nothing β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ↓ Module 05 activates these Module 05: Tensor with active gradients β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Tensor β”‚ β”‚ β€’ data: actual values β”‚ β”‚ β€’ requires_grad: True β”‚ ← Now controls gradient tracking β”‚ β€’ grad: computed gradients β”‚ ← Now accumulates gradients β”‚ β€’ backward(): computes grads β”‚ ← Now implements chain rule β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Design Benefits **Consistency**: Same Tensor class interface throughout all modules - No confusing Variable vs. Tensor distinction (unlike early PyTorch) - Students never need to learn a "new" Tensor class - IDE autocomplete works from day one **Gradual Complexity**: Features activate when students are ready - Module 01-04: Ignore gradient features, focus on operations - Module 05: Gradient features "turn on" magically - No cognitive overload in early modules **Future-Proof**: Easy to extend without breaking changes - Additional features can be added as dormant initially - No monkey-patching or dynamic class modification - Clean evolution path ### Current State (Module 01) ``` Gradient Features - Current Behavior: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Feature β”‚ Current State β”‚ Module 05 State β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ requires_grad β”‚ False β”‚ True (when needed) β”‚ β”‚ grad β”‚ None β”‚ np.array(...) β”‚ β”‚ backward() β”‚ pass (no-op) β”‚ Chain rule impl β”‚ β”‚ Operation chainingβ”‚ Not tracked β”‚ Computation graph β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Student Experience: β€’ Can call .backward() without errors (just does nothing) β€’ Can set requires_grad=True (just gets stored) β€’ Focus on understanding tensor operations first β€’ Gradients remain "mysterious" until Module 05 reveals them ``` This approach matches the pedagogical principle of "progressive disclosure" - reveal complexity only when students are ready to handle it. """ # %% [markdown] """ ## 4. Integration: Bringing It Together Let's test how our Tensor operations work together in realistic scenarios that mirror neural network computations. This integration demonstrates that our individual operations combine correctly for complex ML workflows. ### Neural Network Layer Simulation The fundamental building block of neural networks is the linear transformation: **y = xW + b** ``` Linear Layer Forward Pass: y = xW + b Input Features β†’ Weight Matrix β†’ Matrix Multiply β†’ Add Bias β†’ Output Features (batch, in) (in, out) (batch, out) (batch, out) (batch, out) Step-by-Step Breakdown: 1. Input: X shape (batch_size, input_features) 2. Weight: W shape (input_features, output_features) 3. Matmul: XW shape (batch_size, output_features) 4. Bias: b shape (output_features,) 5. Result: XW + b shape (batch_size, output_features) Example Flow: Input: [[1, 2, 3], Weight: [[0.1, 0.2], Bias: [0.1, 0.2] [4, 5, 6]] [0.3, 0.4], (2, 3) [0.5, 0.6]] (3, 2) Step 1: Matrix Multiply [[1, 2, 3]] @ [[0.1, 0.2]] = [[1Γ—0.1+2Γ—0.3+3Γ—0.5, 1Γ—0.2+2Γ—0.4+3Γ—0.6]] [[4, 5, 6]] [[0.3, 0.4]] [[4Γ—0.1+5Γ—0.3+6Γ—0.5, 4Γ—0.2+5Γ—0.4+6Γ—0.6]] [[0.5, 0.6]] = [[1.6, 2.6], [4.9, 6.8]] Step 2: Add Bias (Broadcasting) [[1.6, 2.6]] + [0.1, 0.2] = [[1.7, 2.8], [4.9, 6.8]] [5.0, 7.0]] This is the foundation of every neural network layer! ``` ### Why This Integration Matters This simulation shows how our basic operations combine to create the computational building blocks of neural networks: - **Matrix Multiplication**: Transforms input features into new feature space - **Broadcasting Addition**: Applies learned biases efficiently across batches - **Shape Handling**: Ensures data flows correctly through layers - **Memory Management**: Creates new tensors without corrupting inputs Every layer in a neural network - from simple MLPs to complex transformers - uses this same pattern. """ # %% nbgrader={"grade": false, "grade_id": "integration-demo", "solution": true} def demonstrate_tensor_integration(): """ Demonstrate Tensor operations working together. This simulates a simple linear transformation: y = xW + b This is the core computation in neural network layers. """ print("πŸ”— Integration Demo: Linear Transformation") print("Simulating: y = xW + b (core neural network operation)") print() # Input data (batch of 2 samples, 3 features each) # This could be: 2 images with 3 pixel values, or 2 sentences with 3 word embeddings x = Tensor([[1, 2, 3], [4, 5, 6]]) # Shape: (2, 3) print("Input x (2 samples, 3 features each):") print(f" {x.data}") print(f" Shape: {x.shape}") print() # Weight matrix (3 input features β†’ 2 output features) # These are the learned parameters that the network will optimize W = Tensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]]) # Shape: (3, 2) print("Weight W (3 inputs β†’ 2 outputs):") print(f" {W.data}") print(f" Shape: {W.shape}") print() # Bias vector (2 output features) # Adds flexibility by shifting the output b = Tensor([0.1, 0.2]) # Shape: (2,) print("Bias b (2 outputs):") print(f" {b.data}") print(f" Shape: {b.shape}") print() # Forward pass: y = xW + b print("Forward pass computation:") print(" Step 1: xW (matrix multiplication)") xW = x.matmul(W) print(f" {x.shape} @ {W.shape} = {xW.shape}") print(f" Result: {xW.data}") print() print(" Step 2: xW + b (broadcasting addition)") y = xW + b print(f" {xW.shape} + {b.shape} = {y.shape} (broadcasting)") print(f" Final result: {y.data}") print() # Verify the computation manually for educational purposes print(" Manual verification of first output:") print(f" Sample 1: [1,2,3] @ [[0.1,0.2],[0.3,0.4],[0.5,0.6]] + [0.1,0.2]") manual_1 = 1*0.1 + 2*0.3 + 3*0.5 # First output feature manual_2 = 1*0.2 + 2*0.4 + 3*0.6 # Second output feature print(f" = [{manual_1}, {manual_2}] + [0.1, 0.2] = [{manual_1+0.1}, {manual_2+0.2}]") print(f" Expected: {y.data[0]}") print() print("βœ… Neural network layer simulation complete!") return y demonstrate_tensor_integration() # %% [markdown] """ ## πŸ§ͺ Module Integration Test Final validation that everything works together correctly before module completion. """ # %% nbgrader={"grade": true, "grade_id": "module-integration", "locked": true, "points": 20} def test_module(): """ Comprehensive test of entire module functionality. This final test runs before module summary to ensure: - All unit tests pass - Functions work together correctly - Module is ready for integration with TinyTorch """ print("πŸ§ͺ RUNNING MODULE INTEGRATION TEST") print("=" * 50) # Run all unit tests print("Running unit tests...") test_unit_tensor_creation() test_unit_arithmetic_operations() test_unit_matrix_multiplication() test_unit_shape_manipulation() test_unit_reduction_operations() print("\nRunning integration scenarios...") # Test realistic neural network computation print("πŸ§ͺ Integration Test: Two-Layer Neural Network...") # Create input data (2 samples, 3 features) x = Tensor([[1, 2, 3], [4, 5, 6]]) # First layer: 3 inputs β†’ 4 hidden units W1 = Tensor([[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8], [0.9, 1.0, 1.1, 1.2]]) b1 = Tensor([0.1, 0.2, 0.3, 0.4]) # Forward pass: hidden = xW1 + b1 hidden = x.matmul(W1) + b1 assert hidden.shape == (2, 4), f"Expected (2, 4), got {hidden.shape}" # Second layer: 4 hidden β†’ 2 outputs W2 = Tensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6], [0.7, 0.8]]) b2 = Tensor([0.1, 0.2]) # Output layer: output = hiddenW2 + b2 output = hidden.matmul(W2) + b2 assert output.shape == (2, 2), f"Expected (2, 2), got {output.shape}" # Verify data flows correctly (no NaN, reasonable values) assert not np.isnan(output.data).any(), "Output contains NaN values" assert np.isfinite(output.data).all(), "Output contains infinite values" print("βœ… Two-layer neural network computation works!") # Test gradient attributes are preserved and functional print("πŸ§ͺ Integration Test: Gradient System Readiness...") grad_tensor = Tensor([1, 2, 3], requires_grad=True) result = grad_tensor + 5 assert grad_tensor.requires_grad == True, "requires_grad not preserved" assert grad_tensor.grad is None, "grad should still be None" # Test backward() doesn't crash (even though it does nothing) grad_tensor.backward() # Should not raise any exception print("βœ… Gradient system ready for Module 05!") # Test complex shape manipulations print("πŸ§ͺ Integration Test: Complex Shape Operations...") data = Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # Reshape to 3D tensor (simulating batch processing) tensor_3d = data.reshape(2, 2, 3) # (batch=2, height=2, width=3) assert tensor_3d.shape == (2, 2, 3) # Global average pooling simulation pooled = tensor_3d.mean(axis=(1, 2)) # Average across spatial dimensions assert pooled.shape == (2,), f"Expected (2,), got {pooled.shape}" # Flatten for MLP flattened = tensor_3d.reshape(2, -1) # (batch, features) assert flattened.shape == (2, 6) # Transpose for different operations transposed = tensor_3d.transpose() # Should transpose last two dims assert transposed.shape == (2, 3, 2) print("βœ… Complex shape operations work!") # Test broadcasting edge cases print("πŸ§ͺ Integration Test: Broadcasting Edge Cases...") # Scalar broadcasting scalar = Tensor(5.0) vector = Tensor([1, 2, 3]) result = scalar + vector # Should broadcast scalar to vector shape expected = np.array([6, 7, 8], dtype=np.float32) assert np.array_equal(result.data, expected) # Matrix + vector broadcasting matrix = Tensor([[1, 2], [3, 4]]) vec = Tensor([10, 20]) result = matrix + vec expected = np.array([[11, 22], [13, 24]], dtype=np.float32) assert np.array_equal(result.data, expected) print("βœ… Broadcasting edge cases work!") print("\n" + "=" * 50) print("πŸŽ‰ ALL TESTS PASSED! Module ready for export.") print("Run: tito module complete 01_tensor") test_module() # %% if __name__ == "__main__": print("πŸš€ Running Tensor Foundation module...") test_module() print("βœ… Module validation complete!") # %% [markdown] """ ## πŸ€” ML Systems Thinking: Tensor Foundations Now that you've built a complete tensor system, let's reflect on the systems implications of your implementation. """ # %% nbgrader={"grade": false, "grade_id": "systems-q1", "solution": true} # %% [markdown] """ ### Question 1: Memory Scaling Analysis You implemented matrix multiplication that creates new tensors for results. **a) Memory Behavior**: When you compute `A.matmul(B)` where A is (1000Γ—1000) and B is (1000Γ—1000): - Before operation: 2,000,000 elements (A: 1M + B: 1M = 2M total) - During operation: _____ elements total in memory (A + B + result = ?) - After operation: _____ elements (if A and B still exist + result) **Memory Calculation Help:** ``` Matrix Memory: 1000 Γ— 1000 = 1,000,000 elements Float32: 4 bytes per element Total per matrix: 1M Γ— 4 = 4 MB ``` **b) Broadcasting Impact**: Your `+` operator uses NumPy broadcasting. When adding a (1000Γ—1000) matrix to a (1000,) vector: - Does NumPy create a temporary (1000Γ—1000) copy of the vector? - Or does it compute element-wise without full expansion? *Think about: temporary arrays, memory copies, and when broadcasting is efficient vs. expensive* """ # %% nbgrader={"grade": false, "grade_id": "systems-q2", "solution": true} # %% [markdown] """ ### Question 2: Shape Validation Trade-offs Your `matmul` method includes shape validation that raises clear error messages. **a) Performance Impact**: In a training loop that runs matmul operations millions of times, what's the trade-off of this validation? - **Pro**: Clear errors help debugging - **Con**: Extra computation on every call **b) Optimization Strategy**: How could you optimize this? ```python # Current approach: if self.shape[-1] != other.shape[-2]: raise ValueError(...) # Check every time # Alternative approaches: 1. Skip validation in "fast mode" 2. Validate only during debugging 3. Let NumPy raise its own error ``` Which approach would you choose and why? *Hint: Consider debug mode vs. production mode, and the cost of shape checking vs. cryptic errors* """ # %% nbgrader={"grade": false, "grade_id": "systems-q3", "solution": true} # %% [markdown] """ ### Question 3: Dormant Features Design You included `requires_grad` and `grad` attributes from the start, even though they're unused until Module 05. **a) Memory Overhead**: Every tensor now carries these extra attributes: ```python # Each tensor stores: self.data = np.array(...) # The actual data self.requires_grad = False # 1 boolean (8 bytes on 64-bit) self.grad = None # 1 pointer (8 bytes) # For 1 million small tensors: extra 16MB overhead ``` Is this significant? Compare to the data size for typical tensors. **b) Alternative Approaches**: What are the pros and cons of this approach vs. adding gradient features later through: - **Inheritance**: `class GradTensor(Tensor)` - **Composition**: `tensor.grad_info = GradInfo()` - **Monkey-patching**: `Tensor.grad = property(...)` *Consider: code complexity, debugging ease, performance, and maintainability* """ # %% nbgrader={"grade": false, "grade_id": "systems-q4", "solution": true} # %% [markdown] """ ### Question 4: Broadcasting vs. Explicit Operations Your implementation relies heavily on NumPy's automatic broadcasting. **a) Hidden Complexity**: A student's code works with batch_size=32 but fails with batch_size=1. The error is: ``` ValueError: operands could not be broadcast together with shapes (1,128) (128,) ``` Given that your implementation handles broadcasting automatically, what's likely happening? Think about when broadcasting rules change behavior. **b) Debugging Challenge**: How would you modify your tensor operations to help students debug broadcasting-related issues? ```python # Possible enhancement: def __add__(self, other): # Add shape debugging information try: result = self.data + other.data except ValueError as e: # Provide helpful broadcasting explanation raise ValueError(f"Broadcasting failed: {self.shape} + {other.shape}. {helpful_message}") ``` *Think about: when broadcasting masks bugs, dimension edge cases, and helpful error messages* """ # %% [markdown] """ ## 🎯 MODULE SUMMARY: Tensor Foundation Congratulations! You've built the foundational Tensor class that powers all machine learning operations! ### Key Accomplishments - **Built a complete Tensor class** with arithmetic operations, matrix multiplication, and shape manipulation - **Implemented broadcasting semantics** that match NumPy for automatic shape alignment - **Created dormant gradient features** that will activate in Module 05 (autograd) - **Added comprehensive ASCII diagrams** showing tensor operations visually - **All methods defined INSIDE the class** (no monkey-patching) for clean, maintainable code - **All tests pass βœ…** (validated by `test_module()`) ### Systems Insights Discovered - **Memory scaling**: Matrix operations create new tensors (3Γ— memory during computation) - **Broadcasting efficiency**: NumPy's automatic shape alignment vs. explicit operations - **Shape validation trade-offs**: Clear errors vs. performance in tight loops - **Architecture decisions**: Dormant features vs. inheritance for clean evolution ### Ready for Next Steps Your Tensor implementation enables all future modules! The dormant gradient features will spring to life in Module 05, and every neural network component will build on this foundation. Export with: `tito module complete 01_tensor` **Next**: Module 02 will add activation functions (ReLU, Sigmoid, GELU) that bring intelligence to neural networks by introducing nonlinearity! """