Self-attention with Functional Time Representation Learning

Da Xu, Chuanwei Ruan, Evren Körpeoglu, Sushant Kumar, Kannan AchanNeurIPS 2019

Introduction: The Problem with Discrete Time

Standard self-attention mechanisms, which power most modern sequence models like Transformers, rely on positional encoding to understand the order of events. While this approach is highly effective for natural language—where words follow a strictly discrete sequence—it falls short in "continuous-time" scenarios such as online shopping, user behavior tracking, or sensor data analysis.

In these real-world sequences, the actual time elapsed between events carries vital contextual information. For example, a user viewing two items one minute apart suggests a very different intent than viewing them one week apart. Traditional self-attention treats these two cases identically because it only processes the sequence index (e.g., Event 1, then Event 2). This paper bridges that gap by introducing a "functional" time representation that seamlessly integrates continuous time intervals into the self-attention framework.

Methodology: Functional Time Embeddings

To solve the limitations of discrete positional encodings, the authors propose a functional feature map that embeds a continuous time span into a high-dimensional vector space. To ensure these embeddings are mathematically sound and preserve relative temporal distances, the approach draws on two classic results from functional analysis:

  1. Bochner Time Embedding: Based on Bochner’s Theorem, this method relates translation-invariant kernels to the Fourier transform of a probability measure. Practically, it represents time using a series of sine and cosine functions. The model learns the optimal frequencies for these functions during training using the "reparameterization trick" and inverse CDF transformations.
  2. Mercer Time Embedding: Based on Mercer’s Theorem, this approach represents the time kernel as an expansion of eigenfunctions. In this architecture, it takes the form of a truncated Fourier basis where the coefficients are learned directly as model parameters.
Visualization of Bochner and Mercer time embeddings

Once generated, these continuous time vectors are concatenated or added to the standard event embeddings. This allows the self-attention mechanism to calculate attention weights that are highly sensitive to the actual temporal distance between events, rather than just their sequential order.

Key Insights and Advantages

  • Time-Event Interaction: The most powerful aspect of this approach is that it allows the model to learn how time interacts with specific events. For instance, the model can learn that a "purchase" event has a longer-lasting influence on future recommendations than a mere "view" event.
  • Theoretical Grounding: Unlike previous heuristic-driven "time-to-vector" methods, this approach is firmly grounded in kernel theory. This ensures that the relative positions of two time representations in the embedding space accurately reflect their actual temporal difference.
  • Parallelization: Because the method avoids recurrent structures (like LSTMs or RNNs), it maintains the massive parallelization advantages of self-attention, making it significantly faster to train on long sequences.

Experimental Results

The models were evaluated on three diverse real-world datasets: Stack Overflow (predicting the next badge a user will receive), MovieLens-1m (predicting the next movie a user will rate), and Walmart.com (predicting the next item a user will view based on session data).

The proposed functional time embeddings consistently outperformed baseline models, including standard LSTMs, time-aware RNNs (TimeJoint), and standard self-attention with positional encoding (PosEnc).

Performance comparison across Stack Overflow, MovieLens-1m, and Walmart.com datasets
  • Stack Overflow: The Mercer-based model achieved the highest accuracy (46.83%), surpassing the best RNN-based baselines.
  • Walmart.com: The Mercer embedding showed significant gains, particularly in Hit@10 (14.94% vs. 12.49% for standard self-attention), proving that capturing precise time intervals is crucial for e-commerce recommendations.
  • Qualitative Findings: Visualizations of the attention weights revealed that the model successfully learned "decay" patterns. Attention naturally decreases as the time since the last interaction increases, but the rate of that decay dynamically changes based on the user's specific actions.

Limitations and Future Work

While the results establish a new state-of-the-art for continuous-time event prediction, the authors note a few areas for further exploration:

  • Hyperparameter Sensitivity: The performance of the Mercer embedding depends heavily on the dimension of the Fourier basis ($k$), which requires careful tuning to avoid overfitting.
  • Distributional Complexity: The Bochner-based "Normal" embedding sometimes struggled with high variance. This suggests that simple Gaussian distributions might not be rich enough to capture all temporal patterns, leading the authors to recommend non-parametric or flow-based CDF transformations instead.
  • Generalization: While this paper focuses on sequence prediction, the authors suggest that these functional time representations could be adapted to other domains, such as temporal graph representation learning or reinforcement learning.

Summary updated

BibTeX
@inproceedings{xu2019self,
  author = {Da Xu and Chuanwei Ruan and Evren Körpeoglu and Sushant Kumar and Kannan Achan},
  title = {{Self-attention with Functional Time Representation Learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2019},
}
Figure