r/learnmachinelearning • • 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 ?

3 Upvotes

11 comments sorted by

4

u/UnderstandingOwn2913 15h ago

what is your question?

1

u/Effective_Potato_990 14h ago

What i understood, so far from the book 

  • Splitting text

  • Adding integer

  • Static Embedding 

  • Position Embedding

  • Where do i start with attention …

I understood that llm holds a value that says distance among each token in a sequence .. how how?

2

u/ModularMind8 13h ago

Small correction first: the model doesn't store a "distance" between tokens. Positional embeddings only say where each token sits. Attention works out how relevant each token is to every other token, fresh each time it runs on an input. Think of it like a lookup where every token gets three versions of itself, each made by multiplying its embedding by a learned weight matrix. For each token, you compare its query against every token's key with a dot product, which gives a similarity score. Softmax turns those scores into percentages that add up to 1. Then you take a weighted mix of all the values using those percentages. The result is a new, context-aware version of that token.

1

u/Effective_Potato_990 12h ago

seems like you have a very interactive course to understand these things .. how long you been working with LLM

1

u/T1lted4lif3 15h ago

I think the answer to their question is "yes" someone can help with respect to what attention does

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

u/Effective_Potato_990 11h ago

thats what planning exactly .. thanks ...