# 🔥 Module: Activations

## 📊 Module Info
- **Difficulty**: ⭐⭐ Intermediate
- **Time Estimate**: 3-4 hours
- **Prerequisites**: Tensor module
- **Next Steps**: Layers module

Welcome to the **Activations** module! This is where you'll implement the mathematical functions that give neural networks their power to learn complex patterns. Without activation functions, neural networks would just be linear transformations—with them, you unlock the ability to learn any function.

## 🎯 Learning Objectives

By the end of this module, you will be able to:

- **Understand the critical role** of activation functions in enabling neural networks to learn non-linear patterns
- **Implement three core activation functions**: ReLU, Sigmoid, and Tanh with proper numerical stability
- **Apply mathematical reasoning** to understand function properties, ranges, and appropriate use cases
- **Debug and test** activation implementations using both automated tests and visual analysis
- **Connect theory to practice** by understanding when and why to use each activation function

## 🧠 Build → Use → Analyze

This module follows TinyTorch's **Build → Use → Analyze** framework:

1. **Build**: Implement ReLU, Sigmoid, and Tanh activation functions with numerical stability
2. **Use**: Apply these functions in testing scenarios and visualize their mathematical behavior
3. **Analyze**: Compare function properties, performance characteristics, and appropriate use cases through quantitative analysis

## 📚 What You'll Build

### Core Activation Functions
```python
# ReLU: Simple but powerful
relu = ReLU()
output = relu(Tensor([-2, -1, 0, 1, 2]))  # [0, 0, 0, 1, 2]

# Sigmoid: Probabilistic outputs
sigmoid = Sigmoid()
output = sigmoid(Tensor([0, 1, -1]))      # [0.5, 0.73, 0.27]

# Tanh: Zero-centered activation
tanh = Tanh()
output = tanh(Tensor([0, 1, -1]))         # [0, 0.76, -0.76]
```

### ReLU (Rectified Linear Unit)
- **Formula**: `f(x) = max(0, x)`
- **Properties**: Simple, sparse, unbounded, most commonly used
- **Implementation**: Element-wise maximum with zero
- **Use Cases**: Hidden layers in most modern architectures

### Sigmoid Activation
- **Formula**: `f(x) = 1 / (1 + e^(-x))`
- **Properties**: Bounded to (0,1), smooth, probabilistic interpretation
- **Implementation**: Numerically stable version preventing overflow
- **Use Cases**: Binary classification, attention mechanisms, gates

### Tanh (Hyperbolic Tangent)
- **Formula**: `f(x) = tanh(x)`
- **Properties**: Bounded to (-1,1), zero-centered, symmetric
- **Implementation**: Direct NumPy implementation with shape preservation
- **Use Cases**: Hidden layers, RNNs, when zero-centered outputs are beneficial

## 🚀 Getting Started

### Prerequisites
Ensure you have completed the tensor module and understand basic tensor operations:

   ```bash
# Activate TinyTorch environment
   source bin/activate-tinytorch.sh

# Verify tensor module is working
tito test --module tensor
   ```

### Development Workflow
1. **Open the development file**: `modules/source/03_activations/activations_dev.py`
2. **Implement functions progressively**: Start with ReLU, then Sigmoid (numerical stability), then Tanh
3. **Test each implementation**: Use inline tests for immediate feedback
4. **Visualize function behavior**: Leverage plotting sections for mathematical understanding
5. **Export and verify**: `tito export --module activations && tito test --module activations`

## 🧪 Testing Your Implementation

### Comprehensive Test Suite
Run the full test suite to verify mathematical correctness:

   ```bash
# TinyTorch CLI (recommended)
   tito test --module activations

# Direct pytest execution
python -m pytest tests/ -k activations -v
```

### Test Coverage Areas
- ✅ **Mathematical Correctness**: Verify function outputs match expected mathematical formulas
- ✅ **Numerical Stability**: Test with extreme values and edge cases
- ✅ **Shape Preservation**: Ensure input and output tensors have identical shapes
- ✅ **Range Validation**: Confirm outputs fall within expected ranges
- ✅ **Integration Testing**: Verify compatibility with tensor operations

### Inline Testing & Visualization
The module includes comprehensive educational feedback:
```python
# Example inline test output
🔬 Unit Test: ReLU activation...
✅ ReLU handles negative inputs correctly
✅ ReLU preserves positive inputs
✅ ReLU output range is [0, ∞)
📈 Progress: ReLU ✓

# Visual feedback with plotting
📊 Plotting ReLU behavior across range [-5, 5]...
📈 Function visualization shows expected behavior
   ```

### Manual Testing Examples
```python
from tinytorch.core.tensor import Tensor
from activations_dev import ReLU, Sigmoid, Tanh

# Test with various inputs
x = Tensor([[-2.0, -1.0, 0.0, 1.0, 2.0]])

relu = ReLU()
sigmoid = Sigmoid()
tanh = Tanh()

print("Input:", x.data)
print("ReLU:", relu(x).data)      # [0, 0, 0, 1, 2]
print("Sigmoid:", sigmoid(x).data) # [0.12, 0.27, 0.5, 0.73, 0.88]
print("Tanh:", tanh(x).data)      # [-0.96, -0.76, 0, 0.76, 0.96]
```

## 🎯 Key Concepts

### Real-World Applications
- **Computer Vision**: ReLU activations enable CNNs to learn hierarchical features (like those in ResNet, VGG)
- **Natural Language Processing**: Sigmoid/Tanh functions power LSTM and GRU gates for memory control
- **Recommendation Systems**: Sigmoid activations provide probability outputs for binary predictions
- **Generative Models**: Different activations shape the output distributions in GANs and VAEs

### Mathematical Properties Comparison
| Function | Input Range | Output Range | Zero Point | Key Property |
|----------|-------------|--------------|------------|--------------|
| ReLU     | (-∞, ∞)     | [0, ∞)       | f(0) = 0   | Sparse, unbounded |
| Sigmoid  | (-∞, ∞)     | (0, 1)       | f(0) = 0.5 | Probabilistic |
| Tanh     | (-∞, ∞)     | (-1, 1)      | f(0) = 0   | Zero-centered |

### Numerical Stability Considerations
- **ReLU**: No stability issues (simple max operation)
- **Sigmoid**: Requires careful implementation to prevent `exp()` overflow
- **Tanh**: Generally stable, but NumPy implementation handles edge cases

### Performance and Gradient Properties
- **ReLU**: Fastest computation, sparse gradients, can cause "dying ReLU" problem
- **Sigmoid**: Moderate computation, smooth gradients, susceptible to vanishing gradients
- **Tanh**: Moderate computation, stronger gradients than sigmoid, zero-centered helps optimization

## 🎉 Ready to Build?

The activations module is where neural networks truly come alive! You're about to implement the mathematical functions that transform simple linear operations into powerful pattern recognition systems.

Every major breakthrough in deep learning—from image recognition to language models—relies on the functions you're about to build. Take your time, understand the mathematics, and enjoy creating the foundation of intelligent systems!

```{grid} 3
:gutter: 3
:margin: 2

{grid-item-card} 🚀 Launch Builder
:link: https://mybinder.org/v2/gh/VJProductions/TinyTorch/main?filepath=modules/source/03_activations/activations_dev.py
:class-title: text-center
:class-body: text-center

Interactive development environment

{grid-item-card} 📓 Open in Colab  
:link: https://colab.research.google.com/github/VJProductions/TinyTorch/blob/main/modules/source/03_activations/activations_dev.ipynb
:class-title: text-center
:class-body: text-center

Google Colab notebook

{grid-item-card} 👀 View Source
:link: https://github.com/VJProductions/TinyTorch/blob/main/modules/source/03_activations/activations_dev.py  
:class-title: text-center
:class-body: text-center

Browse the code on GitHub
```
