Cosine-Gated LSTM for Sequential Learning

PROPOSED MODEL
COMPLETED • PUBLISHED

2024 • Aston University • IEEE PRML 2024 (pp. 8-15)

Table of Contents


1. Overview

Cosine similarity meets recurrent gating

CGLSTM adds a cosine-controlled gate to the vanilla LSTM so the network can emphasise inputs that align with the current trajectory and down-weight irrelevant signals. It replaces the traditional sigmoid-only gating with a similarity-aware mechanism, delivering consistent gains across synthetic sequence tasks, vision benchmarks, NLP datasets, and the SocNavGym prediction challenge.

Chart
99.07% Accuracy
Row-wise MNIST (↑0.65 pts vs LSTM/GRU)
Math
0.0225 MAE
Adding problem (T=1000) lowest error
Robot
35% ↓ MAE @ k=10
SocNavGym vs Transformer baseline
Overview of Cosine-Gated LSTM architecture

Architecture overview from my viva slides: vanilla LSTM (left), cosine-gated module (centre), and integrated output path (right).


2. Research Motivation


3. Architecture

Cosine-gated layer

def cosine_gate(x_proj, h_prev, h_curr, eps=1e-8):
    def cos(a, b):
        return torch.sum(a * b, dim=-1, keepdim=True) / (
            torch.norm(a, dim=-1, keepdim=True) * torch.norm(b, dim=-1, keepdim=True) + eps)

    gate_ic = cos(x_proj, h_prev)
    gate_co = cos(x_proj, h_curr)

    gated = gate_ic * x_proj + h_curr
    gated = gate_co * gated
    return torch.cat([gated, h_curr], dim=-1)

Model footprint


4. Benchmark Evaluation

The IEEE PRML 2024 paper reports results across synthetic sequence modelling, vision, and NLP tasks (mean across three seeds). The table summarises the headline metrics and deltas against the strongest recurrent baseline per task.

Task Metric CGLSTM Best baseline Δ
Adding problem (T=1000) Test MAE ↓ 0.0225 0.0391 (RAU) −42%
Row-wise MNIST Test accuracy ↑ 99.07% 98.70% (GRU) +0.37 pts
Fashion-MNIST Test accuracy ↑ 90.12% 89.68% (GRU) +0.44 pts
IMDB sentiment Test accuracy ↑ 86.30% 86.48% (GRU) −0.18 pts*
Penn Treebank Test perplexity ↓ 104.43 103.38 (Transformer) +1.05*

* CGLSTM matches or exceeds recurrent baselines but Transformer remains ahead on IMDB and PTB in the published results. This is noted as a limitation below.

Table of benchmark accuracies for CGLSTM versus LSTM, GRU, RAU, and Transformer

Benchmark comparison extracted from my viva deck: accuracy and MAE highlights across MNIST, Fashion-MNIST, IMDB, Penn Treebank, and the adding problem.


5. SocNavGym Performance

Cosine-gating was originally motivated by social navigation prediction. The IEEE PRML paper reports the following results when predicting latent trajectories in the SocNavGym environment configured with four moving humans, a table, and a flowerpot.

Model Pred. time (s) MAE k=1 MAE k=5 MAE k=10
Vanilla LSTM 0.00137 1.65e-2 9.61e-2 1.30e-1
GRU 0.00126 1.88e-2 9.49e-2 1.35e-1
Encoder Transformer 0.02793 1.22e-2 6.75e-2 1.12e-1
CGLSTM 0.00738 8.40e-3 6.41e-2 8.26e-2

Key takeaways:

Visual comparison of predictive trajectories in SocNavGym for CGLSTM and baselines

SocNavGym qualitative comparison from my viva: CGLSTM predictions (right) retain blue overlays indicating accurate future states, whereas vanilla LSTM and GRU (left) accumulate red error regions.


6. Implementation & Usage

Drop-in usage for PyTorch recurrent stacks

from cglstm import CosineGatedLSTMCell

class CosineGatedLSTM(nn.Module):
    def __init__(self, input_size, hidden_size):
        super().__init__()
        self.cell = CosineGatedLSTMCell(input_size, hidden_size)

    def forward(self, inputs, hx=None):
        outputs = []
        h, c = hx
        for x in inputs:  # sequence-first tensor
            h, c = self.cell(x, (h, c))
            outputs.append(h)
        return torch.stack(outputs), (h, c)

# Swap into a world model or classifier
vae = VAE(latent_dim=64)
cglstm = CosineGatedLSTM(latent_dim=64, hidden_size=128)

Practical guidance


7. Research Impact


8. Limitations & Next Steps

Planned improvements include cosine-gated GRUs, multi-task transfer experiments, and deeper analysis of horizon-adaptive strategies for social navigation.

Summary of CGLSTM limitations from viva presentation

Slide excerpt summarising trade-offs: Transformer still leads on PTB perplexity, and training cost increases relative to GRU.


9. Resources & Citation

Technologies: Python • PyTorch • TensorFlow • Sequential Modelling • Cosine Similarity

Key Links:

Citation (BibTeX)

@inproceedings{oguzie2024cglstm,
  title={Cosine-Gated Long Short-Term Memory for Sequential Learning},
  author={Oguzie, Goodluck},
  booktitle={IEEE 5th International Conference on Pattern Recognition and Machine Learning (PRML 2024)},
  pages={8--15},
  year={2024},
  organization={IEEE}
}
← Back to Research Portfolio