liveBook · Manning

Beginning-of-sequence token

As explained in Learn Generative AI with PyTorch

The pair of special sequence markers that denote the beginning and end of a tokenized sequence.

A beginning-of-sequence (BOS) token is a special marker placed at the start of a tokenized sentence. It gives an autoregressive decoder an explicit starting point: the decoder reads BOS, predicts the first output token, then continues from the growing sequence until an end-of-sequence (EOS) token signals completion. section 10.1.1 section 9.1.2

In an English-to-French Transformer pipeline, BOS participates in several practical steps:

  • Insert BOS before every tokenized phrase and EOS after it, so the model can learn explicit sequence boundaries. section 10.1.1
  • Assign BOS an integer through the vocabulary dictionary, alongside PAD and UNK, before converting tokens into model inputs. section 10.1.1
  • Begin decoding with BOS, predict one token at a time, and stop when the decoder predicts EOS. section 10.5
  • Use BOS and EOS for sentence-based translation, unlike text generation inputs built from a predetermined number of tokens. section 10.1.2

Starting a translation

For the French translation of “I do not speak French,” the decoder first receives BOS. If it predicts “Je,” the next input becomes “BOS Je”; each later prediction extends that sequence until EOS appears. section 9.1.2

Listing 10.2 Mapping English tokens to indexes
from collections import Counter
  
en=df["en"].tolist()                                           #1
  
en_tokens=[["BOS"]+tokenizer.tokenize(x)+["EOS"] for x in en]  #2
PAD=0
UNK=1
word_count=Counter()
for sentence in en_tokens:
    for word in sentence:
        word_count[word]+=1
frequency=word_count.most_common(50000)                        #3
total_en_words=len(frequency)+2
en_word_dict={w[0]:idx+2 for idx,w in enumerate(frequency)}    #4
en_word_dict["PAD"]=PAD
en_word_dict["UNK"]=UNK
en_idx_dict={v:k for k,v in en_word_dict.items()}              #5

Inspect how BOS and EOS are added around tokenized English phrases before frequency counts and integer vocabulary mappings are created. section 10.1.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines placement, vocabulary indexing, and decoding behavior, but does not detail BOS embedding internals.
Examples
Demonstrates BOS with English-to-French phrases, including “I do not speak French” and the prediction sequence “BOS Je.”
What it assumes
Assumes tokenization and integer vocabulary mapping while establishing BOS, EOS, PAD, and UNK roles explicitly.
What you can do after
Apply it via a token dictionary and autoregressive decoder to start sequence generation and recognize completion with EOS.
How it connects
Connects BOS with EOS, autoregressive decoding, tokenization, vocabulary mapping, padding, and Transformer decoders.

Also explained in

Compare how 3 books explain beginning-of-sequence token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.