ENع
→Tokens
الدرس 18 · النماذج اللغوية الكبيرة عمليًا

Tokens

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

A language model never sees letters, only numbers. A tokenizer cuts text into tokens, pieces such as words or parts of words, and gives each one an ID. Train a real byte pair encoding (BPE) tokenizer on four words, watch neighbouring letters fuse into tokens, split a word it has never seen into known pieces, then follow its IDs into the model's embedding table and back out as text.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 18 · تفاعلي
01 / 10

Models only see numbers

A language model does arithmetic, so it cannot read letters. Before any text goes in, a tokenizer cuts it into pieces called tokens and gives each piece a number, its ID. The model only ever sees those IDs.

text  ->  tokenizer  ->  tokens  ->  IDs  ->  model

token  a piece of text: a word, part of a word,
       a letter or a punctuation mark
ID     the number the tokenizer gives each token
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Models only see numbers

    A language model does arithmetic, so it cannot read letters. Before any text goes in, a tokenizer cuts it into pieces called tokens and gives each piece a number, its ID. The model only ever sees those IDs.

    text  ->  tokenizer  ->  tokens  ->  IDs  ->  model
    
    token  a piece of text: a word, part of a word,
           a letter or a punctuation mark
    ID     the number the tokenizer gives each token
  2. Start from single letters

    To build a tokenizer, take a training corpus, a sample of text: here four words and how often each appears. Split every word into single characters. These 10 characters are the first vocabulary, IDs 0 to 9.

    corpus = {"low": 5, "lower": 2, "newest": 6, "widest": 3}
    
      low     x5  ->  l o w
      lower   x2  ->  l o w e r
      newest  x6  ->  n e w e s t
      widest  x3  ->  w i d e s t
    
    vocabulary  d e i l n o r s t w
    IDs         0 .. 9
  3. Count neighbouring pairs

    Byte pair encoding (BPE) counts every pair of neighbouring symbols, weighted by how often its word appears. "e s" is in newest (6 times) and widest (3): 9 in total. "s t" also scores 9; the first one found wins.

    pairs = count_pairs(words)
    
      e s    9
      s t    9
      w e    8
      l o    7
      o w    7
      n e    6
      ...
    best = ('e', 's')   (9; first of the tied pairs)
  4. Merge the top pair

    The winning pair becomes one new symbol, "es", with the next free ID, 10. Every "e s" in the corpus is joined into "es". The merge is written down: the ordered list of merges is what the tokenizer learns.

    best = max(pairs, key=pairs.get)
    words = merge_all(words, best)
    merges.append(best)
    
      low     l o w
      lower   l o w e r
      newest  n e w es t
      widest  w i d es t
    
    merges = [('e', 's')]    "es" -> ID 10
  5. Repeat: tokens grow

    Count again, merge again. "es t" scores 9, so "est" gets ID 11. Then "l o" (7) becomes "lo", ID 12, and "lo w" (7) becomes "low", ID 13. We stop after 4 merges; GPT-2's tokenizer learned 50,000.

    pass  best pair   count  new token  ID
      1   e s            9  es         10
      2   es t           9  est        11
      3   l o            7  lo         12
      4   lo w           7  low        13
    
      low     low
      lower   low e r
      newest  n e w est
      widest  w i d est
  6. Tokenize a new word

    "lowest" was never in the corpus. Split it into letters, then apply the 4 merges in the order they were learned: es, est, lo, low. Two tokens are left: "low" and "est". An unseen word still splits into known pieces.

    def encode(word, merges):
        symbols = tuple(word)
        for pair in merges:
            symbols = merge(symbols, pair)
    
      start     l o w e s t
      + es      l o w es t
      + est     l o w est
      + lo      lo w est
      + low     low est
  7. Look up the IDs

    Each token is looked up in the vocabulary: "low" is 13 and "est" is 11. So the 6 letters of "lowest" become 2 numbers, [13, 11]. Frequent pieces get their own token; rare words take several.

    vocab = sorted(set("".join(corpus)))
            + ["".join(p) for p in merges]
    vocab.index("low")  ->  13
    vocab.index("est")  ->  11
    
    "lowest"  ->  [13, 11]   (6 letters, 2 tokens)
  8. Limits are counted in tokens

    The model reads a sequence of IDs, up to a fixed length called the context window. GPT-2's was 1,024 tokens. Limits and prices are counted in tokens, not words: English text averages about 4 characters per token.

    context window  [13, 11, ...]
    GPT-2           1,024 tokens at most
    
    rule of thumb   1 token ~ 4 characters of English
                    100 tokens ~ 75 words
  9. Each ID picks a vector

    Inside the model, each ID selects one row of a learned table, the embedding table. The row is a vector, a list of numbers the transformer layers work on. GPT-2 small: 50,257 rows of 768 numbers each.

    ID 13  ->  row 13  ->  vector of 768 numbers
    ID 11  ->  row 11  ->  vector of 768 numbers
    
    GPT-2 small  50,257 x 768 table
      = 50,000 merges + 256 bytes + 1 end token
    the numbers are learned during training
  10. Decode IDs back to text

    The model answers one token ID at a time. Decoding runs the tokenizer backwards: look up each ID and join the pieces. If it outputs 13 then 11, the text is "low" + "est" = "lowest".

    print("".join(vocab[i] for i in ids))
    
      13  ->  "low"
      11  ->  "est"
      "low" + "est"  ->  "lowest"

اختبار يتاح بعد الخطوة الأخيرة.