DEV Community

Divyakush Punjabi
Divyakush Punjabi

Posted on

Self-attention isn't just for language — SASRec for recommendations

Self-attention is famous for powering language models — but the mechanism was never really about language. It's about letting a model decide, for each element in a sequence, which other elements matter most. That idea is just as powerful for predicting what you'll click next as it is for predicting the next word, and putting it to work is what drives my Content Recommendation Engine.

Not everything in your history matters equally

Your last fifty actions aren't equally relevant to what you want next. Some were one-offs; a few were the real signal. The hard part of sequential recommendation is figuring out which past actions to weigh heavily for this prediction — and that weighting should change depending on where you are in your journey.

That's precisely the problem self-attention solves. For each position in a sequence, it computes how much every other position should influence it — a learned, context-dependent weighting rather than a fixed rule like "only the last item counts" or "average everything equally."

Attention over actions, not words

The engine uses a self-attention architecture — the SASRec approach — to model a user's interaction history:

  • The model learns what to focus on. Instead of hand-coding how much recent versus old behavior matters, attention learns it from data, and can lean on a purchase from weeks ago when it's the relevant one while ignoring yesterday's noise.
  • Context reshapes the weighting. The same past action can matter a lot or a little depending on what surrounds it — attention captures that flexibility, which a fixed recency rule simply can't.
  • Sequences in, next-item out. The architecture is built to consume an ordered history and predict the next step, which is exactly the shape of the recommendation problem — the same machinery that made transformers dominate language, pointed at behavior.

The takeaway

The most valuable thing I took from this project is that a great mechanism outlives its original domain. Self-attention wasn't a language trick — it was a general answer to "which parts of this sequence matter for this prediction?" Recognizing that a technique is really about structure, not its first application, is how you carry the best ideas across fields.

Building the recommender on self-attention taught me to look at a famous technique and ask what problem it actually solves. The full architecture is on the project page.

👉 See it: www.divyakush.com/projects/content-recommendation-engine


Divyakush Punjabi — Full-Stack & AI Systems Engineer

🌐 https://www.divyakush.com · 💼 LinkedIn · 💻 GitHub

Top comments (0)