liveBook · Manning

Beginning-of-sequence token

As explained in Natural Language Processing in Action, Second Edition

A start-of-sequence token is a special token marking the beginning of a sequence and signaling a generative decoder to begin output.

A beginning-of-sequence token marks the start of a sequence. In generation, it prompts the decoder to produce a suitable first token. Common spellings include `<SOS>` and `<s>`. section 9.2.3

For translation, register the marker as a special token with the tokenizer. Choose a spelling that does not occur as ordinary text in the dataset. An end-of-sequence token serves a different purpose: it tells the decoder when to stop generating. section 9.2.3

>>> SRC = 'en'                             #1
>>> TGT = 'de'                                  #2
>>> SOS, EOS = '<s>', '</s>'
>>> PAD, UNK, MASK = '<pad>', '<unk>', '<mask>'
>>> SPECIAL_TOKS = [SOS, PAD, EOS, UNK, MASK]
>>> VOCAB_SIZE = 10_000
...
>>> from tokenizers import ByteLevelBPETokenizer     #3
>>> tokenize_src = ByteLevelBPETokenizer()
>>> tokenize_src.train_from_iterator(
...     [x[SRC] for x in sents['train']['translation']],
...     vocab_size=10000, min_frequency=2,
...     special_tokens=SPECIAL_TOKS)
>>> PAD_IDX = tokenize_src.token_to_id(PAD)
...
>>> tokenize_tgt = ByteLevelBPETokenizer()
>>> tokenize_tgt.train_from_iterator(
...     [x[TGT] for x in sents['train']['translation']],
...     vocab_size=10000, min_frequency=2,
...     special_tokens=SPECIAL_TOKS)
>>> assert PAD_IDX == tokenize_tgt.token_to_id(PAD)

The tokenizer setup registers `<s>` alongside end, padding, unknown, and mask tokens for separate source and target languages. section 9.2.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines the marker, explains its decoder-start role, and shows its registration with tokenizers.
Examples
Uses `<SOS>` and `<s>` as marker spellings and registers `<s>` in separate English and German tokenizers.
What it assumes
Assumes a translation-tokenization workflow while defining the marker’s purpose in plain language.
What you can do after
Apply it via Hugging Face tokenizers by registering a distinct start marker for sequence generation.
How it connects
Connects beginning-of-sequence tokens to transformer decoders, translation tokenization, and end-of-sequence tokens.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.