Time2Vec: Learning a Vector Representation of Time

Seyed Mehran Kazemi, Rishab Goel, Sepehr Eghbali, Janahan Ramanan, Jaspreet Sahota, Sanjay Thakur, Stella Wu, Cathal Smyth, Pascal Poupart, Marcus BrubakerarXiv 2019

TL;DR

This paper introduces Time2Vec, a novel, model-agnostic vector representation for time that can be easily integrated into existing machine learning architectures. By learning a representation that captures both periodic and non-periodic temporal patterns, Time2Vec consistently improves performance on various time-series tasks, outperforming traditional methods of incorporating time information.

What Problem Does This Paper Address?

In many real-world applications, time is a crucial feature for making predictions. Whether it's forecasting sales based on daily data, predicting a patient's next medical event, or recommending music based on listening history, understanding temporal dependencies is key. However, effectively encoding time into machine learning models has been a challenge. Traditional approaches often treat time as a simple numerical input, which recurrent neural networks (RNNs) struggle to interpret effectively. While some methods involve hand-crafting time-based features or designing specialized architectures, these can be complex and require domain expertise. This paper aims to provide a general-purpose, learnable time representation that can be seamlessly incorporated into various models to enhance their performance without extensive feature engineering.

Methods

The core contribution of this paper is the Time2Vec representation. For a given scalar time value $\tau$, Time2Vec generates a vector of size $k+1$. This vector consists of a linear component ($\omega_0\tau + \phi_0$) and $k$ periodic components, each defined by a sine function with learnable frequency ($\omega_i$) and phase-shift ($\phi_i$):

$t2v(\tau)[i] = \begin{cases} \omega_0\tau + \phi_0 & \text{if } i = 0 \\ \sin(\omega_i\tau + \phi_i) & \text{if } 1 \le i \le k \end{cases}$

The linear component is designed to capture non-periodic patterns and aid in extrapolation, while the sine functions are intended to model periodic behaviors (like daily or weekly cycles). The authors chose sine functions due to their ability to capture periodicity and their favorable extrapolation properties compared to other basis functions. Time2Vec is model-agnostic, meaning it can be fed as an input to various architectures, such as LSTMs, to augment their existing features.

Datasets

The authors evaluated Time2Vec on a variety of datasets:

  • Synthesized data: A toy dataset created to illustrate periodic patterns (weekly cycles) and test time rescaling invariance.
  • Event-MNIST: A sequential version of MNIST where images are represented by the temporal sequence of pixel activations. This dataset was designed to isolate the effect of time representation.
  • N_TIDIGITS18: Audio spike data from a spoken digit dataset, used for sequence classification.
  • Stack Overflow (SOF): Sequences of badges obtained by users, with timestamps, for predicting the next badge.
  • Last.FM: User listening histories with song event timestamps, for predicting the next song.
  • CiteULike: User posting histories of papers with timestamps, for predicting the next paper.

Key Insights and Ideas

  • Time as a Learned Representation: Instead of treating time as a raw numerical feature or relying on hand-crafted features, Time2Vec learns a rich representation that captures its inherent characteristics.
  • Dual Nature of Time: The linear component effectively models the continuous progression of time and aids in extrapolation, while the periodic components (sine waves) are crucial for capturing cyclical patterns.
  • Model Agnosticism is Key: The power of Time2Vec lies in its ability to be plugged into any existing architecture, offering a universal upgrade for time-aware tasks.
  • Periodicity Matters: The experiments strongly suggest that capturing periodic temporal patterns is vital for many real-world time-series problems.
  • Learning Frequencies is Better: The paper demonstrates that learning the frequencies and phase-shifts of the sine functions within Time2Vec leads to better performance than fixing them to predefined values.

Key Results

  • Consistent Performance Boost: Across all tested datasets and architectures (including standard LSTMs and specialized time-handling models like TLSTM1 and TLSTM3), replacing raw time features with Time2Vec consistently improved performance or, at worst, yielded comparable results.
  • Effective for Long Sequences: Time2Vec showed particular effectiveness on datasets with long sequences and time horizons, such as N_TIDIGITS18 and SOF.
  • Capturing Periodic Patterns: On the synthesized dataset, Time2Vec successfully learned to oscillate every 7 days, aligning multiples of 7 with the positive peaks of the learned signal, demonstrating its ability to capture specific periodic behaviors.
  • Time Rescaling Invariance: The paper proves that Time2Vec is invariant to time rescaling, meaning it behaves consistently regardless of the units used to measure time (e.g., days, hours).

Limitations and Future Work

  • Theoretical Analysis: The authors acknowledge that a deeper theoretical analysis of the hypothesis that combining sine functions with powerful models and using many sine functions reduces the distance to the goal is needed.
  • Optimizer Development: They suggest that developing better optimizers for Time2Vec could be a future research direction.
  • Non-Periodic Activation Functions: While periodic activations (sine, mod, triangle) outperformed non-periodic ones (Sigmoid, Tanh, ReLU) in their experiments, the paper notes that non-periodic activations can saturate or explode, making them less suitable for time-series data.
  • Overfitting with Many Gates: In the case of TLSTM3, using a large number of sine functions (64) with its multiple time gates led to overfitting. This suggests that the optimal number of sine functions might be architecture-dependent.

Summary updated

BibTeX
@misc{kazemi2019time,
  author = {Seyed Mehran Kazemi and Rishab Goel and Sepehr Eghbali and Janahan Ramanan and Jaspreet Sahota and Sanjay Thakur and Stella Wu and Cathal Smyth and Pascal Poupart and Marcus Brubaker},
  title = {{Time2Vec: Learning a Vector Representation of Time}},
  year = {2019},
  eprint = {1907.05321},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1907.05321},
}
Figure