Live data from Hacker News

Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

news.ycombinator.com

221–230 of 240 posts

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#221
post #128

Okay, here's my attempt! First, we take a sequence of words and represent it as a grid of numbers: each column of the grid is a separate word, and each row of the grid is a measurement of some property of that word. Words with similar meanings are likely to have similar numerical values on a row-by-row basis. (During the training process, we create a dictionary of all possible words, with a column of numbers for each…

This was a very helpful visualization, thank you! The "entanglement" part intuitively makes sense to me, but one bit I always get caught up on the key, query, and value matrices. In every self-attention explanation I've read/watched they tend to get thrown out there and similar to what you did here but leave their usage/purpose a little vague. Would you mind trying to explain those in more detail? I've heard the data…

Its basically almost the same as convolution with image processing. For example, you take the 3 channel rgb value of a single pixel, do some math on it with the values of the surrounding pixels with weights, which gives you some value(s). Depending on the dimensions of everything, you can end up with a smaller dimension output, like a single 3 channel RGB value, or a higher dimension output (i.e for a 5x5 kernel, you can end up with a 9x9 output)

The confusing part that doesn't get mentioned is that the input vectors (Q, K, V) are weighted, i.e they are derived from the input with the standard linear transformation where y = A*x+b, where x is the input word, A is the linear layer matrix, and b is the bias. Those weighs are the things that are learned through the training process.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#222

There is annotated version of the paper. It also has a link to a GitHub repo in there. Google “The annotated transformer”. It’s off nlp.seas.harvard.edu.

I hope that soon LLMs will generate such notebooks for any scientific paper you ask. That would help to eliminate fake and unrealistic results ones.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#223
post #92

Earlier quoted context omitted.

I'm not a ML expert but I know a bit about math. It's "differentiable" in the same way that e.g. the "jump function" ( Heaviside step function ) is differentiable (not as a function from real numbers to real numbers, but as a distribution ). It's derivative is the "point impulse function" ( Dirac delta function ), which, again, is a distribution , not a real function. Distributions are nicely defined in math, but can…

Thanks for this explanation. I couldn't wrap my mind around the "differentiable hash table" analogy, but "distribution of keys" -> "distribution of values" starts to click. I'm not an ML expert either but I have taken graduate level courses and published papers with "machine learning" in the title, so I feel like I should be able to understand these things better. The field just moves so fast. It's a lot of work to k…

>The field just moves so fast. It's a lot of work to keep up. Easy-to-digest explanations like this are underrated.

This is really the truth. I can't possibly understand how people in this field who are talented can still keep up. I have a binder full of seminal papers that I have to cull to make room for more recent and relevant research every few months. I feel there is a lot of potential in simplifying the details of the mechanisms that drive a lot of it, but nobody has time to stop, consolidate the information and publish it. And if they did, it would just be another outdated textbook in a few years.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#224

Well here is my (a bit cynical) take on it. In the beginning, there was the matrix multiply. A simple neural network is a chain of matrix multiplies. Let's say you have your data A1 and weights W1 in a matrix. You produce A2 as A1xW1. Then you produce A3 as A2xW2, and so on. There are other operations in there like non-linearities (so that you can actually learn something interesting) and fancy batch norms, but let's…

Are transformers

- a hack devised/stumbled upon by AI workers or

- a theoretical concept concocted by a mathematician who has been thinking about what NNs do or,

- a set of techniques pipe-lined together by clever programmers who work with NNs? Or...

- something else?

I mean, if transformers really do something rational, then there should be a straightforward rational mathematical statement of the problem, a clear and clean expression of what they do!

What I see instead is a lot of complex cumbersome description and terminological noise: no clear problem statement, lots of steps, lots of moving parts, and downhill from there.

Now I'd be the first to admit that, if I believed I understood and could reproduce intelligence or language [and is intelligence merely language? An argument to that effect can be made - see Helen Keller], then if required I'd be prone to provide dense, noisy and incorrect explanations aplenty to potential competitors and even to honest inquisitive people. I would do that b/c revelation of the truth would destroy my competitive advantage. IOW I see every reason for ChatGPT et al developers and corporations to guide outsiders astray at this time.

Developing something like ChatGPT is like running an exposed Manhattan Project - everyone wants Da Bomb and you don't want them to have it - instead you want to lead them completely astray. Seems to be succeeding: certainly as far as I'm concerned.

Here's a simple test: has anyone within reach of my words made a version of these systems from scratch that does anything like the fullblown ChatGPT does and that (s)he will reveal?

Current times would indicate that we won't learn how it all works until someone leaks it or someone else figures it out (like happened to Heisenberg and Schrodinger). My bet is on the latter. And that's the guy/gal who should get the Fields medal or Nobel Prize. [I'd bet a mathematician will do it].

Meanwhile as we twiddle our matrices much effort must be afoot to infiltrate the ChatGPT working groups and get the goods and also to keep the current ChatGPT worker bees from flying off to other hives and revealing secrets. This may be one of the few times when tech workers' jobs seriously shorten their lives.

- Wandering in the desert...

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#225
post #69

Here is an attempt that sacrifices technical precision for intuition. We're going to represent words as vectors (a sequence of numbers). We would like it to be the case that the value of the numbers reflects the meaning of the words. Words that mean similar things should be near each other. We also want to represent higher level ideas, ideas that take multiple words to express, in the same way. You can think of all t…

Thank you for this explanation. I've found that the QKV concepts are some of the most glossed over parts of attention and I'll be honest, some of the most confusing. Would you mind actually going into more detail on the questions you asked towards the end of your post? I vaguely understand how an embedding might get learned over time, but I don't understand how queries are "constructed" or how these three separate matrices get learned, or what kind of information is being encoded in them when they're learned. I also don't really understand how the value matrix itself is used.

Any further detail, even if it gets into some technical details would be very helpful and appreciated!

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#226
post #225
post #69

Here is an attempt that sacrifices technical precision for intuition. We're going to represent words as vectors (a sequence of numbers). We would like it to be the case that the value of the numbers reflects the meaning of the words. Words that mean similar things should be near each other. We also want to represent higher level ideas, ideas that take multiple words to express, in the same way. You can think of all t…

Thank you for this explanation. I've found that the QKV concepts are some of the most glossed over parts of attention and I'll be honest, some of the most confusing. Would you mind actually going into more detail on the questions you asked towards the end of your post? I vaguely understand how an embedding might get learned over time, but I don't understand how queries are "constructed" or how these three separate ma…

Sure, so to see how these things can be learned, we should be a little more precise about how they work.

Each token is a vector, and from that vector we compute three things - a query, a key and a value. Each of these is typically computed by multiplying the token's vector by a matrix (aka a linear projection). It's the values in these matrices that we need to learn.

When performing an attention step, for a given token we compare its "query" to every token's "key" (including it's own key - a token can attend to itself). This gives us a score for how important we think that key is. We normalize those scores to sum to one (typically via a softmax operation). Essentially, we have one "unit" of attention, and we're going to spread it across all the tokens. Some we will pay a lot of attention to, and others very little.

But what does it mean to pay a lot of or a little attention to other tokens? At the end of this whole procedure, we're going to arrive at a new vector that represents our new understanding/meaning for the token we're working on. This vector will be computed as a weighted sum of the values from all the tokens we're attending to. The weights are our attention scores (determined by the query-key similarity scores).

So as a simple example, suppose I have three tokens, A B and C, and let's focus on the attention operation for A. Say A's query vector is [1 2 -1]. A's key vector is [3 -1 0], B's key vector is [3 -1 -1] and C's key vector is [0 1 -3]. This gives us raw attention scores of 1 for A (attending to itself), 4 for B, and 5 for C. Rather than take a messy softmax, let's just normalize these to 0.1, 0.4 and 0.5 for simplicity.

Now that we have our attention weights, we also need to know each token's value. Let's say they are [1 0 1] for A, [-1 2 0] for B, and [1 1 1] for C. So our final output for this attention step will be 0.1 * [1 0 1] + 0.4 * [-1 2 0] + 0.5 * [1 1 1]. This gives us [0.2 1.3 0.6] (assuming I eyeballed the math correctly), this will be our new representation of A for the next step. (in practice there are some additional network layers that do more processing).

Okay, so how can we learn any of the matrices that go from a token vector to a query, a key and a value? The important thing is that all of this is just addition and multiplication - it's all nicely differentiable. And because the attention is "soft" (meaning we always attend at least a little bit to everything, as opposed to "hard" attention where we ignore some items entirely), we can even compute gradients through the attention scores.

Put a simpler way, I can ask "if I had included a bit more of A's value and a bit less of B's value, would my final output have been closer to the target?". To include a bit less of B, I need to make A's query and B's key a little further apart (lower dot product). And to make them a little further apart, I need to adjust the numbers in the matrices that produces them. Similarly, I can ask "if C's value had been a little larger in the first slot and a little smaller in the third, would my final output have been closer to the target?", and adjust the value matrix in the same way. Even if the attention to another token is very low, there's at least a small sliver of contribution from it, so we can still learn that we would've been better off having more (or even less) of it.

Learning the initial embeddings (the vectors that represent each word in the vocabulary, before any processing) is done in the same way - you trace all the way back through the network and ask "if the embedding for the word 'bank' had been a little more like this would my answer have been closer?", and adjust accordingly.

Understanding what exactly the queries keys and values represent is often very difficult - sometimes we can look and see which words attend to which other words and make up a convincing story. "Oh, in this layer, the verb is attending to the corresponding subject" or whatever. But in practice, the real meaning of a particular internal representation is going to be very fuzzy and not have a single clear concept behind it.

There is no explicit guidance to the network like "you should attend to this word because it's relevant to this other word." The only guidance is what the correct final output is (usually for LLMs the training task is to predict the next word, but it could be something else). And then the training algorithm adjusts all the parameters, including the embeddings, the QKV matrices, and all the other weights of the network, in whatever direction would make that correct output more likely.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#227
post #217
post #206

Earlier quoted context omitted.

If you train in parallel, how do you combine the weights generated from different trainings in parallel?

Believe it or not, it's a simple as averaging or adding the gradients of each training result before adding it to the model weights. The same thing happens when you train a model using batches of inputs.

It actually isn't. You have to have a synchronizer, batchsize one or else "strange things" can happen and you waste a lot of cycles. Alternatively you can do non-simple changes to your network structure to enable distributed training.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#228
post #217

Earlier quoted context omitted.

Believe it or not, it's a simple as averaging or adding the gradients of each training result before adding it to the model weights. The same thing happens when you train a model using batches of inputs.

It actually isn't. You have to have a synchronizer, batchsize one or else "strange things" can happen and you waste a lot of cycles. Alternatively you can do non-simple changes to your network structure to enable distributed training.

It really is that simple. Yes, there's many different approaches to this (which can become quite clever and complex, which is true of training in general), but it all really boils down to adding or averaging the gradients in most cases.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#229

Okay, here's my attempt! First, we take a sequence of words and represent it as a grid of numbers: each column of the grid is a separate word, and each row of the grid is a measurement of some property of that word. Words with similar meanings are likely to have similar numerical values on a row-by-row basis. (During the training process, we create a dictionary of all possible words, with a column of numbers for each…

why are the words cols and properties are rows. seems counter intuitive

just tilt your head 90 degrees and it'll be fine.

this is rows/columns from a math/matrix/tensor perspective where they are the arbitrary first and second dimensions of a data-containing object.

it's not rows/columns from a database perspective where you expect columns to define a static schema and rows to be individual records.

Re: Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

#230
post #226
post #225

Earlier quoted context omitted.

Thank you for this explanation. I've found that the QKV concepts are some of the most glossed over parts of attention and I'll be honest, some of the most confusing. Would you mind actually going into more detail on the questions you asked towards the end of your post? I vaguely understand how an embedding might get learned over time, but I don't understand how queries are "constructed" or how these three separate ma…

Sure, so to see how these things can be learned, we should be a little more precise about how they work. Each token is a vector, and from that vector we compute three things - a query, a key and a value. Each of these is typically computed by multiplying the token's vector by a matrix (aka a linear projection). It's the values in these matrices that we need to learn. When performing an attention step, for a given tok…

This was an excellent explanation, thank you for taking the time to write it out!
Post reply on HN