Loading W Code...
⢠RNN: Simple, vanishing gradients
⢠LSTM: 3 gates, best for long sequences
⢠GRU: 2 gates, faster training
Concept Level: Beginner
# WHY SEQUENCE MODELS
import numpy as np
import matplotlib.pyplot as plt
print("="*60)
print("SEQUENCE DATA EXAMPLES")
print("="*60)
# Example 1: Text where order matters
print("\n1. TEXT ORDER MATTERS:")
sentences = [
("The movie was not good", "NEGATIVE"),
("The movie was good, not bad", "POSITIVE"),
]
for sent, label in sentences:
print(f" '{sent}' ā {label}")
# Example 2: Time series
print("\n2. TIME SERIES:")
np.random.seed(42)
days = 30
stock_prices = 100 + np.cumsum(np.random.randn(days) * 2)
plt.figure(figsize=(12, 4))
plt.plot(stock_prices, 'b-', linewidth=2)
plt.xlabel('Day')
plt.ylabel('Stock Price (ā¹)')
plt.title('Stock Price Over Time - Order Matters!')
plt.grid(True, alpha=0.3)
plt.show()
print(" Stock prices are sequential - today depends on yesterday!")
# Example 3: Why MLP fails for sequences
print("\n3. WHY STANDARD NETWORKS FAIL:")
print(" MLP sees: [100, 105, 102, 108]")
print(" MLP also sees: [108, 102, 105, 100] (same shuffle)")
print(" ā Both look identical to MLP!")
print(" ā But trend is completely different!")
# Simulating RNN behavior
print("\n" + "="*60)
print("RNN CONCEPT")
print("="*60)
sequence = "HELLO"
print(f"\nProcessing sequence: '{sequence}'")
print("\nRNN processes one character at a time with memory:")
hidden = 0 # Initial memory
for i, char in enumerate(sequence):
# Simplified: memory is just sum of ASCII codes so far
hidden = hidden * 0.5 + ord(char) # Combine old memory with new input
print(f" Step {i+1}: Input='{char}' ā Hidden State = {hidden:.1f}")
print("\nš” Hidden state carries information from ALL previous characters!")