liveBook · Manning

Out-of-vocabulary word

As explained in Learn Generative AI with PyTorch

An out-of-vocabulary word or text unit is absent from a tokenizer’s or model’s available vocabulary and may be represented by a special unknown-token marker.

An out-of-vocabulary word is missing from the model’s token dictionary. Mapping it to a special UNK token gives it an index the model can process, instead of causing a lookup failure. section 12.2.1 section 10.1.1

Fallback versus subword tokens

If “technology” is absent from a word-level dictionary, looking it up without UNK can crash; adding UNK allows it to be encoded. By contrast, a tokenizer may split “French” into “fr” and “ench,” so the word need not be represented as one vocabulary entry. section 12.2.1 section 10.1.1

Listing 12.4 Mapping tokens to indexes
from collections import Counter   
  
word_counts=Counter(text_tokenized)    
words=sorted(word_counts, key=word_counts.get,
                      reverse=True)     
words.append("UNK")                                            #1
text_length=len(text_tokenized)
ntokens=len(words)                                             #2
print(f"the text contains {text_length} words")
print(f"there are {ntokens} unique tokens")  
word_to_int={v:k for k,v in enumerate(words)}                  #3
int_to_word={v:k for k,v in word_to_int.items()}               #4
print({k:v for k,v in word_to_int.items() if k in words[:10]})
print({k:v for k,v in int_to_word.items() if v in words[:10]})

See how UNK is added to the token list and tokens are mapped to integer indexes. section 12.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines the UNK fallback and shows vocabulary indexing, but does not detail alternative unknown-token strategies.
Examples
Contrasts missing “technology” with French split into “fr” and “ench,” and uses UNK as a text-generation prompt.
What it assumes
Assumes tokens are looked up in a dictionary for conversion to integer indexes.
What you can do after
Operate a token dictionary by mapping absent entries to UNK before converting tokens to indexes.
How it connects
Connects tokenization and vocabulary lookup to integer encoding and text generation.

Also explained in

Compare how 8 books explain out-of-vocabulary word →

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.