Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?
644 pointsI have zero AI/ML knowledge but Steve Yegge on Medium thinks that the team behind Transformers deserves a Nobel.
Makes me want to better understand this tech.
Edit: thank you for some amazing top level responses and links to valuable content on this subject.
Comments (228) 76 threads
Show all comments
Showing thread 68 of 76
As you read each panel of a comic book, you don't just look at the words in the speech bubbles, but you also pay attention to who's talking, what they're doing, and what happened in the previous panels. You might pay more attention to some parts than others. This is sort of like what the Transformer model does with text.
When the Transformer reads a sentence, it doesn't just look at one word at a time. It looks at all the words at once, and figures out which ones are most important to understand each other. This is called "attention." For example, in the sentence "The cat, which is black, sat on the mat," the Transformer model would understand that "cat" is connected to "black" and "sat on the mat."
The "attention" part is very helpful because, like in a comic book, understanding one part of a sentence often depends on understanding other parts. This makes the Transformer model really good at understanding and generating language.
Also, because the Transformer pays attention to all parts of the sentence at the same time, it can be faster than other models that read one word at a time. This is like being able to read a whole page of your comic book at once, instead of having to read each panel one by one.
The Transformer is unique because it uses a mechanism called "attention" to understand the relationships between words in a sentence, which works like this:
(1) Encoding: First, the Transformer turns each word in a sentence into a list of numbers, called a vector. These vectors capture information about the word's meaning.
(2) Self-Attention: Next, for each word, the Transformer calculates a score for every other word in the sentence. These scores determine how much each word should contribute to the understanding of the current word. This is the "attention" part. For example, in the sentence "The cat, which is black, sat on the mat," the words "cat" and "black" would get high scores when trying to understand the word "black" because they are closely related.
(3) Aggregation: The Transformer then combines the vectors of all the words, weighted by their attention scores, to create a new vector for each word. This new vector captures both the meaning of the word itself and the context provided by the other words in the sentence.
(4) Decoding: Finally, in a task like translation, the Transformer uses the vectors from the encoding phase to generate a sentence in the target language. It again uses attention to decide which words in the original sentence are most relevant for each word it's trying to generate in the new sentence.
One key advantage of the Transformer is that it can calculate the attention scores for all pairs of words at the same time, rather than one at a time like previous models. This allows it to process sentences more quickly, which is important for large tasks like translating a whole book.
I especially disagree with:
> natural language processing (NLP), which is the field of AI that deals with understanding and generating human language.
Continue this thread — 2 replies
The model is so named because it "transforms" one sequence into another. For example, in a machine translation task, it can transform a sentence in one language into a sentence in another language.
The key innovation of the Transformer model is the use of self-attention mechanisms. This means that instead of processing the input sequence word by word, the model considers all the words in the sequence at the same time and learns to pay "attention" to the most important ones for the given task.
In essence, the Transformer model is a design for building network architectures that can process data in parallel and focus on different parts of the data depending on the task at hand. The Transformer model has proven to be highly effective and flexible, and has been adopted in many variants and applications, including BERT, GPT, T5, and many others.
> in a machine translation task, it transforms a sentence in one language into a sentence in another language.
here English is being translated to which language - I'm assuming vectors? might be a silly question, I'm assuming that's where the origin of the word "Transformer" lies
Continue this thread — 1 reply
It's general computation model, does't have to work on text only.
It's also general in the sense that you can mask it - ie. with lower triangular matrix so future doesn't influence past (decoder, generation); leave it unmasked (ie. in encoder, ie. in text translation you want attention to have access to full input text) or anything else really.
So what does that actually mean in terms of looking at new text? How does it know the relationships? Does it have to be bootstrapped on labeled data for a specific language up front?
Is that something done in the training process - providing example sentences and illustrating the connections between words - or is that earlier?
Continue this thread — 1 reply
> In this work, we use sine and cosine functions of different frequencies:
> PE(pos,2i) = sin(pos/10000^{2i/d_model})
> PE(pos,2i+1) = cos(pos/10000^{2i/d_model})
> where pos is the position and i is the dimension. That is, each dimension of the positional encoding corresponds to a sinusoid. The wavelengths form a geometric progression from 2π to 10000 · 2π. We chose this function because we hypothesized it would allow the model to easily learn to attend by relative positions, since for any fixed offset k, PE_{pos+k} can be represented as a linear function of PE_{pos}.
(“Vector” is just an alternate way of talking about a coordinate point - you can say “a point is at (x,y)”, or equivalently you can also say “turn x degrees and then travel y units forward”, either method gives enough information to find the point exactly.)
I don’t think sine and cosine are actually factors in human language - rather, the process of turning words into vectors captures whatever are the factors in human language, translates them into vectors, and in that translation process the factors get turned into something that sine/cosine measurements of vectors is good at picking up.
A toy example would be that arithmetic doesn’t seem to be a factor in political orientation, but if you assess everyone’s political orientation with some survey questions and then put them on a line from 0 to 10, then you could do some subtraction and multiplication operations to find numbers that are close together - ie doing arithmetic to find similar politics. The reason that works is not because arithmetic has anything to do with political orientation, it’s because your survey questions captured information about political orientation and transformed it into something that arithmetic works on.
I guess this explanation doesn’t do much except push the unintuitiveness into the embedding process (that’s the process of turning words into vectors in a way that captures their relationship to other words).
Imagine that words are spread out in the space. Cosine similarity is a measure of similarity between two vectors (each word is encoded as a vector).
By measuring the cosine of the angle between the two vectors we can get:
1) whether 2 vectors have the same angle (2 words have the same meaning or close enough) when the cosine is close to 1
2) whether 2 vectors are perpendicular (2 words don't have anything to do with each other) when the cosine is close to zero
3) whether 2 vectors are opposite in direction (2 words have opposite meanings in some aspect) when the cosine is close to -1
Cosine similarity is like comparing two people's interests. If two people have similar interests, the angle between them is small, and the cosine similarity value will be high. If two people have completely different interests, the angle between them is large, and the cosine similarity value will be low. So, cosine similarity is a way to measure how similar two things are by looking at the angle between them.
Continue this thread — 5 replies
Does each sentence stand alone, or is the meaning of the sentence, and the words in the sentence, influenced by the sentences that come before and after it?
When you convert words (tokens) into vectors and calculate cosine similarity, you're typically doing what's called "word embedding". This process captures the semantic meaning of words in a high-dimensional space. Words that have similar meanings have vectors that are close to each other in this space. Cosine similarity is a measure of how similar two vectors are, which in this context equates to how similar the meanings of two words are.
On the other hand, the attention mechanism in Transformer models is a way to understand the relationships between words within a specific context. It determines how much each word in a sentence contributes to the understanding of every other word in the sentence. It's not just about the semantic similarity of words, but also about their grammatical and contextual relationships in the given sentence.
Here's an analogy: imagine you're trying to understand a conversation between a group of friends. Just knowing the meaning of their words (like word embeddings do) can help you understand some of what they're saying. But to fully understand the conversation, you also need to know who's speaking to whom, who's agreeing or disagreeing with whom, who's changing the topic, and so on. This is similar to what the attention mechanism does: it tells the model who's "talking" to whom within a sentence.
So while word embeddings and cosine similarity capture static word meanings, the attention mechanism captures dynamic word relationships within a specific context. Both are important for understanding and generating human language.
Continue this thread — 2 replies
Continue this thread — 1 more reply