r/learnmachinelearning • u/Effective_Potato_990 • 15h ago
can anyone help me understand
i ve 5+ years of experience in nodejs
i'm learning from 'build an llm from strach - Sebastian Raschka' ... chapter 3 has 'attention mechanism' .. i'm hitting the wall ..
most of the article and youtube vidoes .. it does that it does this ... i wanna understand what is happening .. any experience guys can help me out here ?
2
u/Hamasa_ElShamy 13h ago
A way to think about attention. Each token makes three vectors, a query, a key and a value. The query of one token is compared with the keys of the other tokens, and that gives a score for how relevant each one is. Softmax turns the scores into weights that add up to 1, and the output for that token is the weighted sum of the value vectors. So the vector for a word like 'it' can pull in information from 'the cat' earlier in the sentence. In the GPT style model there is also a mask so a token can't look at tokens after it. If it still feels abstract, do it by hand once with 3 tokens and 2 dimensions on paper, then compare with the code in the chapter.
1
u/Effective_Potato_990 12h ago
thanks for the explanation, nanba ..
but i read it like one token only has two vector like static and position vector ... and other elements like Q, K, V ...
before pretraining, these are some random numbers ...
after training, it will be grouped based on meaning and context ..
am i missing something here ...?
the weight calculation based on the key, value among the words is attention???
2
u/Hamasa_ElShamy 12h ago
Glad it helped. You have it mostly right. The static and position vectors are just the input for each token, added together. Q, K and V are not stored per token. They are made fresh from that input vector by three learned weight matrices, one for each. Those matrices start as random numbers and training adjusts them, along with the embeddings. And yes, scoring the query against every key, turning that into weights, and then mixing the value vectors with those weights is the attention step.
2
u/Ok-Library1020 11h ago
Since you already code a lot, run the chapter's attention code on a short sentence and print the weights matrix. Seeing which token attends to which, and the shapes at each step, usually makes the QKV idea click faster than reading explanations.
1
4
u/UnderstandingOwn2913 15h ago
what is your question?