🕐 TemporalEmbedding
🕐 TemporalEmbedding
🟡 Intermediate
✅ Stable
⏱️ Time Series
🎯 Overview
The TemporalEmbedding layer embeds temporal/calendar features (month, day, weekday, hour, minute) into a shared embedding space. It supports both:
- Fixed Embeddings: Pre-defined sinusoidal patterns (no parameters)
- Learned Embeddings: Trainable embeddings optimized for your task
Perfect for capturing: - Seasonal Patterns: Monthly, weekly, daily cycles - Hourly Effects: Rush hours, off-peak hours - Calendar Effects: Holidays, weekends, special events - Time-of-Day Variations: Energy demand, traffic patterns
🔍 How It Works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Each temporal component is embedded independently, then summed to create a combined representation.
💡 Why Use This Layer?
| Scenario | Fixed | Learned | Result |
|---|---|---|---|
| Fast Training | ✅ No params | ❌ Slower | Use Fixed |
| Accuracy | ⚠️ Limited | ✅ Optimal | Use Learned |
| Transfer Learning | ✅ Generic | ⚠️ Task-specific | Use Fixed |
| Data Scarcity | ✅ Better | ❌ Overfits | Use Fixed |
📊 Use Cases
- Load Forecasting: Hour and month embeddings for energy demand
- Traffic Prediction: Weekday and hour-of-day patterns
- Retail Sales: Weekend/holiday effects, seasonal trends
- Weather: Seasonal patterns, daily cycles
- Stock Market: Trading hours, day-of-week effects
- Healthcare: Time-of-day symptoms, seasonal diseases
🚀 Quick Start
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
🔧 API Reference
1 2 3 4 5 6 7 | |
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
d_model |
int |
— | Output embedding dimension |
embed_type |
str |
'fixed' | 'fixed' or 'learned' embeddings |
freq |
str |
'h' | Frequency: 'h'(hourly), 'd'(daily), 't'(minutely) |
name |
str \| None |
None | Optional layer name |
Input Shape
(batch_size, time_steps, 5)or(batch_size, time_steps, 4)- Channels: [month(0-12), day(0-31), weekday(0-6), hour(0-23), minute(0-59)]
Output Shape
(batch_size, time_steps, d_model)
💡 Best Practices
- Choose Embed Type: Fixed for speed/generality, Learned for accuracy
- Match Frequency: hourly (h) / daily (d) / minutely (t)
- Proper Ranges: month(1-12), day(1-31), weekday(0-6), hour(0-23)
- Combine with Values: Use with TokenEmbedding for full context
- Layer Norm: Consider LayerNorm after embedding
⚠️ Common Pitfalls
- ❌ Out-of-range indices: month>12, hour>23 causes embedding errors
- ❌ Wrong frequency: Mismatch between data and freq setting
- ❌ Missing minute: If freq='t', must provide 5 channels
- ❌ Unused embeddings: If not using minutes, set freq='h'
📚 References
- Vaswani, A., et al. (2017). "Attention Is All You Need"
- Zhou, H., et al. (2021). "Informer: Beyond Efficient Transformer"
🔗 Related Layers
FixedEmbedding- Individual fixed embeddingsTokenEmbedding- Value embeddingsDataEmbeddingWithoutPosition- Combined embeddings
Last Updated: 2025-11-04 | Keras: 3.0+ | Status: ✅ Production Ready