Live data from Hacker News

Understand how transformers work by demystifying the math behind them

osanseviero.github.io

81–90 of 139 posts

Re: Understand how transformers work by demystifying the math behind them

#81
post #61

The whole "mystery" of transformer is that instead of a linear sequence of static weights times values in each layer, you now have 3 different matrices that are obtained from the same input through multiplication of learned weights, and then you just multiply the matrices together. I.e more parallelism which works out nice, but very restrictive since the attention formula is static. We arent going to see more progres…

Genetic algorithms figured out GI the first time, but it took a while.

Could you please expand?

Re: Understand how transformers work by demystifying the math behind them

#82

Earlier quoted context omitted.

Yes to both, the "neuron" would basically be a weighted parameter. A parameter is an expression, it's a mathematical representation of a token and it's probabilistic weighting (theyre translated from input or to output token lists entering and exiting the model). Usually tokens are pre-set small groups of character combinations like "if " or "cha" that make up a word/sentence. The recorded path your value takes down…

This is all true in a neutral net, but Transformers aren't Neural Nets in the traditional sense. I was under that impression originally, but there's not a back propagation or Hebbian learning here, which were the key bits of biomimicry that earned classic NNs their name. Transformers do have coefficients that are fit, but that's more broad.. could be used for any sort of regression or optimization, and not necessaril…

Thanks for your reply, you raise a very good point, transformer models are a lot more complex. I'd argue conceptually they're the same, just the data and process is more abstracted. Autoencoded data implies using efficient representations, basically semantically abstracted data and opting for measures like back propagation through time.

Re: Understand how transformers work by demystifying the math behind them

#83
Is there an error in the positional encoding example? For example when calculating PE(1, 3), I'd expect i = 1 as 3 = 2 * 1 + 1

So for “World”

PE(1, 0) = sin(1 / 10000^(2*0 / 4)) = sin(1 / 10000^0) = sin(1) ≈ 0.84

PE(1, 1) = cos(1 / 10000^(2*0 / 4)) = cos(1 / 10000^0) = cos(1) ≈ 0.54

PE(1, 2) = sin(1 / 10000^(2*1 / 4)) = sin(1 / 10000^.5) ≈ 0.01

PE(1, 3) = cos(1 / 10000^(2*1 / 4)) = cos(1 / 10000^.5) ≈ 1

I also wondered if these formulae were devised with 1-based indexing in mind (though I guess for larger dimensions it doesn't make much difference), as the paper states

> The wavelengths form a geometric progression from 2π to 10000 · 2π

That led me to this chain of PRs - https://github.com/tensorflow/tensor2tensor/pull/177 - turns out the original code was actually quite different to that stated in the paper. I guess slight variations in how you calculate this encoding doesn't affect things too much?

Re: Understand how transformers work by demystifying the math behind them

#84
post #64

Earlier quoted context omitted.

If I had to guess, single characters are able to be encoded as tokens, but there's more "bandwidth" in the model being dedicated to handling them and there's less semantic meaning encoded in them "natively" compared to tokens for concrete words. If it decides to, it can recreate unknown sequences by copying over the tokens for the single letters or create them if it makes sense.

I think some earlier NLP applications have something called "Unknown token", which they will replace any unseen word. But for recent implementations, I don't think they are being used anymore. It still baffles me why such stochastic parrot / next token predictor, will recognize these "Unseen combinations of tokens" and reuse them in response.

Everything falls into place once you understand that LLMs are indeed learning hierarchical concepts inherent in the structured data it has been trained on. These concepts exist in a high dimensional latent space. Within this space is the concept of nonsense/gibberish/placeholder, which your sequence of unseen tokens map to. Then it combines this with the concept of SQL tables, resulting in hopefully the intended answer.

Re: Understand how transformers work by demystifying the math behind them

#85
post #17
post #13

Earlier quoted context omitted.

Does mystified math lie beyond behind how the ratio of input and output voltages is equal to the ratio of the primary and secondary windings? Can it be derived from Maxwell's equations? Off to a search...

I bet that an LLM (which uses transformers) can explain those aspects of a transformer to you.

I seen an LLM, or maybe another variant of “AI” [0] a while back that could aid design of electronic circuits by having a pool of data sheets added for referencing.

As you were querying specs for a board at component level it could give you a schematic, I think, with citations to the actual data sheets.

I suppose the same scale up could be used for systems that needed a varying number of specific power supplies.

[0] https://www.flux.ai/p

Re: Understand how transformers work by demystifying the math behind them

#86

I love and hate these. I love them because they do give another resource at explaining models such as transformers and I think this one is pretty well done (note: you really need to do something about the equation in 4.2...) First, the critique is coming from love. Great work, so I don't want it to be taken as I'm saying anything it isn't. Why I hate these is that they are labeled as "math behind" but I think this is…

Can you link a resource that is able to adequately explain why they're called Key, Query, and Value? Every explanation I've read eventually handwaved this. It feels like understanding why they're named that is key (heh) to understanding the concept, rather than just blindly implementing matmul.

https://stats.stackexchange.com/questions/421935/what-exactl...

It mentions it comes from the original Attention Is All You Need paper and goes on into more detail.

It seems to be named exactly as you would expect. Key/Value as in KV store, with Query being the term being retrived.

Re: Understand how transformers work by demystifying the math behind them

#87
> Uh oh! We’re getting NaNs! It seems our values are too high, and when being passed to the next encoder, they end up being too high and exploding! This is called gradient explosion.

As far as I understand this is wrong. You're not computing gradients at any point, so this is no gradient explosion. I believe the problem is with the implementation of softmax, here [0] you have an explanation of how to implement a numerically stable softmax.

[0]: https://jaykmody.com/blog/stable-softmax/

Re: Understand how transformers work by demystifying the math behind them

#88

Earlier quoted context omitted.

Yes to both, the "neuron" would basically be a weighted parameter. A parameter is an expression, it's a mathematical representation of a token and it's probabilistic weighting (theyre translated from input or to output token lists entering and exiting the model). Usually tokens are pre-set small groups of character combinations like "if " or "cha" that make up a word/sentence. The recorded path your value takes down…

This is all true in a neutral net, but Transformers aren't Neural Nets in the traditional sense. I was under that impression originally, but there's not a back propagation or Hebbian learning here, which were the key bits of biomimicry that earned classic NNs their name. Transformers do have coefficients that are fit, but that's more broad.. could be used for any sort of regression or optimization, and not necessaril…

> This is all true in a neutral net, but Transformers aren't Neural Nets in the traditional sense. I was under that impression originally, but there's not a back propagation or Hebbian learning here, which were the key bits of biomimicry that earned classic NNs their name.

Hebbian learning has never been used with much success in training neural nets. Backpropagation is not bio-inspired, but backpropagation is certainly used to train transformers.

Re: Understand how transformers work by demystifying the math behind them

#89
post #17

Earlier quoted context omitted.

I bet that an LLM (which uses transformers) can explain those aspects of a transformer to you.

I seen an LLM, or maybe another variant of “AI” [0] a while back that could aid design of electronic circuits by having a pool of data sheets added for referencing. As you were querying specs for a board at component level it could give you a schematic, I think, with citations to the actual data sheets. I suppose the same scale up could be used for systems that needed a varying number of specific power supplies. [0]…

Actually, in my experience chatgpt knows quite a lot about electronic components so I wouldn't be surprised if it was fed datasheets.

Re: Understand how transformers work by demystifying the math behind them

#90

I love and hate these. I love them because they do give another resource at explaining models such as transformers and I think this one is pretty well done (note: you really need to do something about the equation in 4.2...) First, the critique is coming from love. Great work, so I don't want it to be taken as I'm saying anything it isn't. Why I hate these is that they are labeled as "math behind" but I think this is…

> Idk, are people interested in these things? Yes, absolutely. Would be awesome to read deeper.

Seconded.
Post reply on HN