Skip to content
tezvyn:
Menu

Attention: Weighing Input by Relative Importance

Source: Wikipedia: Attention (machine learning)MediumHow cards are made

Attention: Weighing Input by Relative Importance

The attention mechanism lets a model decide which parts of a sequence are most important relative to others. In natural language processing, it assigns 'soft' weights to words, allowing the model to focus on what's most relevant for a given task. It's used to encode sequences of token embeddings, from short phrases to massive documents. The main pitfall is forgetting that these weights are contextual and relative, not absolute measures of a word's importance.

The mental model

Attention is a mechanism that allows a neural network to dynamically weigh the importance of different parts of an input sequence. Instead of compressing an entire sequence into a single fixed-size vector, which can lose information, attention lets the model look back at the entire sequence and decide which parts are most relevant for the current step. It answers the question: "Given this part of the sequence, which other parts should I pay the most attention to?"

How it works

The mechanism operates on sequences of vectors, known as token embeddings. For each token in the sequence, attention calculates a set of 'soft' weights. These weights represent the importance of every other token in the sequence relative to the current one. A higher weight means a stronger connection or relevance. The model then creates a new, context-aware representation for each token by taking a weighted sum of all token embeddings in the sequence, guided by these attention scores. This process effectively encodes the context from the entire sequence into each token's representation.

When to use it

• Natural Language Processing: It's a core component in modern NLP models for tasks where context between words is critical. • Encoding Long Sequences: It is used to encode context across sequences of token embeddings, which can range in size from tens to millions of tokens. • Relational Tasks: Any task where the relationship between different elements in a sequence determines the outcome.

When not to use it

• Independent Inputs: For problems where each part of the input sequence has equal and independent importance, and context between elements is irrelevant.

One canonical example

Consider a model processing the sentence, "The gray cat sat on the mat." The attention mechanism helps the model understand relationships. When focusing on the word "sat," the model might assign high attention weights to "cat" (who sat) and "mat" (where it sat). This is done by calculating these 'soft' weights for the token embeddings of each word in the sentence relative to "sat." The resulting representation for "sat" is now enriched with information about the subject and location, all learned dynamically from the sequence itself.

Interview question

Which statement best describes the nature of the 'soft' weights generated by an attention mechanism?

  • a.A) They assign a fixed, intrinsic importance score to each token, independent of its context.
  • b.B) They are binary indicators, marking tokens as either critical or irrelevant for the entire sequence.
  • c.C) They dynamically quantify the relevance of each token in relation to other tokens within a specific sequence context.Correct
  • d.D) They represent the overall frequency of a token's appearance within the entire dataset.
Why?

The card states that attention dynamically weighs importance and assigns 'soft' weights representing the importance of every other token relative to the current one, creating a context-aware representation. Therefore, option C accurately describes this dynamic, relative, and contextual nature. Option A is incorrect because the card explicitly warns against viewing these weights as 'absolute measures of a word's importance' and emphasizes their contextual nature.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on machine learning — each one lists the topics its interview covers.

See open roles