TECHNOLOGIESTieInn Technologies

From neural networks to transformers: tokens, attention and embeddings

ML Foundations · 10 questions

Short notes

Key idea

Transformers, the architecture behind LLMs, process a whole sequence of tokens at once. Attention lets each token look at every other token and decide which ones matter for its meaning. Tokens start as embeddings, and position information is added so word order counts.

Syntax

  • Tokenise: text → token IDs.
  • Embed: each ID → a vector; add position information.
  • Attention: each token makes a query, key and value; it scores its query against every key, softmaxes the scores and takes a weighted sum of values.
  • Layers: attention + a feed-forward network, stacked dozens of times.
  • Output: probabilities for the next token (decoder models like chat LLMs).

Example

In "The bank of the river was muddy", attention lets "bank" weigh "river" heavily, so its vector moves towards the riverbank meaning rather than the money one. In "I deposited cash at the bank", "cash" and "deposited" pull the other way.

Traps

  • Attention cost grows with the square of sequence length; that's one reason context windows are limited.
  • Embeddings capture usage, not truth.
  • Transformers don't "understand" like people; they model patterns in text.
  • Encoder models (for embeddings and classification) and decoder models (for generation) are used for different jobs.

Want the full lesson with step-by-step examples, a project and a certificate? ML Foundations course, ₹180 →

Questions

Open in Code Lab5 free questions
5 questions

    From neural networks to transformers: tokens, attention and embeddings

  1. Question 1easy

    Look up the embedding rows for the token ids of sentence in E and store the resulting matrix's shape (a tuple) in result.

    Store your answer in result.

  2. From neural networks to transformers: tokens, attention and embeddings

  3. Question 2medium

    What does this print?

    import numpy as np
    s = np.array([0.3, 0.3, 0.3, 0.3])
    e = np.exp(s - s.max())
    print((e / e.sum()).tolist())
  4. From neural networks to transformers: tokens, attention and embeddings

  5. Question 3medium

    What does an attention layer's output for a token contain?

  6. From neural networks to transformers: tokens, attention and embeddings

  7. Question 4medium

    With these attention weights, what is the output for the first token (weights @ V, first row)?

    import numpy as np
    weights = np.array([[0.5, 0.5], [0.0, 1.0]])
    V = np.array([[2.0, 0.0], [0.0, 4.0]])
    print((weights @ V)[0].tolist())
  8. From neural networks to transformers: tokens, attention and embeddings

  9. Question 5hard

    How are temperature (in sampling) and attention connected?

5 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.