Commit Graph

8 Commits

Author SHA1 Message Date
Vijay Janapa Reddi
688e5826ec feat: Add Milestone 04 (CNN Revolution 1998) + Clean spatial imports
Milestone 04 - CNN Revolution:
 Complete 5-Act narrative structure (Challenge → Reflection)
 SimpleCNN architecture: Conv2d → ReLU → MaxPool → Linear
 Trains on 8x8 digits dataset (1,437 train, 360 test)
 Achieves 84.2% accuracy with only 810 parameters
 Demonstrates spatial operations preserve structure
 Beautiful visual output with progress tracking

Key Features:
- Conv2d (1→8 channels, 3×3 kernel) detects local patterns
- MaxPool2d (2×2) provides translation invariance
- 100× fewer parameters than equivalent MLP
- Training completes in ~105 seconds (50 epochs)
- Sample predictions table shows 9/10 correct

Module 09 Spatial Improvements:
- Removed ugly try/except import pattern
- Clean imports: 'from tinytorch.core.tensor import Tensor'
- Matches PyTorch style (simple and professional)
- No fallback logic needed

All 4 milestones now follow consistent 5-Act structure!
2025-09-30 17:04:41 -04:00
Vijay Janapa Reddi
5ae68dd4b4 Fix gradient propagation: enable autograd and patch activations/losses
CRITICAL FIX: Gradients now flow through entire training stack!

Changes:
1. Enable autograd in __init__.py - patches Tensor operations on import
2. Extend enable_autograd() to patch Sigmoid and BCE forward methods
3. Fix gradient accumulation to handle broadcasting (bias gradients)
4. Fix optimizer.step() - param.grad is numpy array, not Tensor.data
5. Add debug_gradients.py for systematic gradient flow testing

Architecture:
- Clean patching pattern - all gradient tracking in enable_autograd()
- Activations/losses remain simple (Module 02/04)
- Autograd (Module 05) upgrades them with gradient tracking
- Pedagogically sound: separation of concerns

Results:
 All 6 debug tests pass
 Perceptron learns: 50% → 93% accuracy
 Loss decreases: 0.79 → 0.36
 Weights update correctly through SGD
2025-09-30 13:51:30 -04:00
Vijay Janapa Reddi
ba6bd79a67 Reset package and export modules 01-07 only (skip broken spatial module) 2025-09-30 13:41:00 -04:00
Vijay Janapa Reddi
ae3f2246d9 Fix imports: Replace dev-style imports with proper package imports in modules 06-07 2025-09-30 13:40:38 -04:00
Vijay Janapa Reddi
8be87d0add Fix nbdev export system across all 20 modules
PROBLEM:
- nbdev requires #| export directive on EACH cell to export when using # %% markers
- Cell markers inside class definitions split classes across multiple cells
- Only partial classes were being exported to tinytorch package
- Missing matmul, arithmetic operations, and activation classes in exports

SOLUTION:
1. Removed # %% cell markers INSIDE class definitions (kept classes as single units)
2. Added #| export to imports cell at top of each module
3. Added #| export before each exportable class definition in all 20 modules
4. Added __call__ method to Sigmoid for functional usage
5. Fixed numpy import (moved to module level from __init__)

MODULES FIXED:
- 01_tensor: Tensor class with all operations (matmul, arithmetic, shape ops)
- 02_activations: Sigmoid, ReLU, Tanh, GELU, Softmax classes
- 03_layers: Linear, Dropout classes
- 04_losses: MSELoss, CrossEntropyLoss, BinaryCrossEntropyLoss classes
- 05_autograd: Function, AddBackward, MulBackward, MatmulBackward, SumBackward
- 06_optimizers: Optimizer, SGD, Adam, AdamW classes
- 07_training: CosineSchedule, Trainer classes
- 08_dataloader: Dataset, TensorDataset, DataLoader classes
- 09_spatial: Conv2d, MaxPool2d, AvgPool2d, SimpleCNN classes
- 10-20: All exportable classes in remaining modules

TESTING:
- Test functions use 'if __name__ == "__main__"' guards
- Tests run in notebooks but NOT on import
- Rosenblatt Perceptron milestone working perfectly

RESULT:
 All 20 modules export correctly
 Perceptron (1957) milestone functional
 Clean separation: development (modules/source) vs package (tinytorch)
2025-09-30 11:21:04 -04:00
Vijay Janapa Reddi
ed7b680ad0 feat: implement professional selective export pattern across all modules
BREAKING CHANGE: Refactor from whole-module exports to selective function/class exports

**What Changed:**
- Separate development utilities from production exports
- Each function/class gets individual #| export directive
- Clean Prerequisites & Setup sections in all modules
- Development helpers (import_previous_module) not exported

**Module Export Summary:**
- 01_tensor: Tensor class only
- 02_activations: Sigmoid, ReLU, Tanh, GELU, Softmax only
- 03_layers: Linear, Dropout only
- 04_losses: MSELoss, CrossEntropyLoss, BinaryCrossEntropyLoss, log_softmax only
- 05_autograd: Function class only
- 06_optimizers: SGD, Adam, AdamW only

**Benefits:**
 Clean public API (matches PyTorch/TensorFlow patterns)
 No development utilities in final package
 Professional software education standards
 Clear separation of concerns
 Educational clarity for students

This matches industry standards for educational ML frameworks.
2025-09-30 09:48:47 -04:00
Vijay Janapa Reddi
6ccfa2b352 feat: standardize integration testing with import helpers
- Add import_previous_module() helper function to all core modules (01-07)
- Standardize cross-module imports for integration testing
- Add clear Prerequisites & Setup sections explaining module dependencies
- Update integration tests to use standardized import pattern
- Maintain clean separation between development and production code

This provides a consistent, educational approach to module integration
while keeping the codebase maintainable and student-friendly.
2025-09-30 09:42:58 -04:00
Vijay Janapa Reddi
e1a9541c4b Clean up module imports: convert tinytorch.core to sys.path style
- Remove circular imports where modules imported from themselves
- Convert tinytorch.core imports to sys.path relative imports
- Only import dependencies that are actually used in each module
- Preserve documentation imports in markdown cells
- Use consistent relative path pattern across all modules
- Remove hardcoded absolute paths in favor of relative imports

Affected modules: 02_activations, 03_layers, 04_losses, 06_optimizers,
07_training, 09_spatial, 12_attention, 17_quantization
2025-09-30 08:58:58 -04:00