How to initialize word-embeddings for Out of Vocabulary Word?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Initializing word embeddings for out-of-vocabulary (OOV) words is a critical challenge in natural language processing (NLP). Word embeddings transform words into vectors such that words with similar meanings are mapped closely in the vector space. Pretrained models like Word2Vec, GloVe, or FastText provide embeddings for a fixed vocabulary, but when a new word appears — typically called an OOV word — the system needs a strategy to handle it, as failing to do so can degrade model performance.
Approaches to Handle Out-Of-Vocabulary Words
1. Random Initialization
One straightforward method is randomly initializing the OOV word embedding. Although this method doesn't directly incorporate semantic information, it allows the model to eventually learn relevant patterns during training.
Advantages:
- Simple to implement.
- Provides a baseline for comparison with more sophisticated methods.
Disadvantages:
- Lacks semantic specificity.
- Might take longer for the model to learn meaningful representations.
2. Subword Embeddings
Approaches like FastText consider subword information, allowing embeddings to be composed from n-grams of characters. This enables handling morphological variations and OOV words by constructing word vectors using the embeddings of their subword units.
Advantages:
- Captures morphological information efficiently.
- Useful for languages with rich morphology.
Disadvantages:
- Requires additional computation.
- May struggle with languages lacking clear subword structures.
3. Similar Word Approximation
Another strategy is to initialize the OOV word embeddings by averaging the vectors of similar in-vocabulary words. Such techniques often rely on string similarity or existing partial contexts from training data.
Example: If the OOV word is "chatbot," find similar words like "bot," "chat," etc., and average their vectors.
Advantages:
- Leverages semantic information of existing words.
- Can be effective when similar words are present in the vocabulary.
Disadvantages:
- Dependency on in-vocabulary similarity.
- Overhead of identifying similar words.
4. Contextual Embeddings
Contextual embeddings from models like BERT or GPT dynamically generate vectors for OOV words based on context. These models inherently avoid the OOV problem since they rely on subword tokenization.
Advantages:
- Context-aware, providing more relevant embeddings.
- Generally superior performance in capturing polysemy.
Disadvantages:
- Computationally expensive.
- Requires significant training data and resources.
5. Use of Lexical Resources
Leveraging available lexical databases (e.g., WordNet) or creating mappings based on linguistic features can provide better initialization for OOV words. This method involves linking OOV words to known entities and using their embeddings.
Advantages:
- Uses rich linguistic information.
- Useful for specialized vocabularies.
Disadvantages:
- Requires maintaining and updating lexical resources.
- May not cover all OOV scenarios.
Technical Example
Consider a sentence processing system where the sentence is:
"The chatbot responded instantly."
- Word2Vec/GloVe may not contain "chatbot."
- FastText would segment it into subwords like "chat," "bot," "ch," "at," and construct an embedding.
- A system using BERT would tokenize as `['The', 'chat', '##bot', 'responded', 'instantly']` and generate contextual embeddings accordingly.
Evaluation and Considerations
- Performance Impact: Experiment with different methods to evaluate which provides the best results for your specific application. Use intrinsic measures like cosine similarity or extrinsic tasks such as classification or clustering.
- Computational Efficiency: Contextual and subword models may substantially increase computational demands. Consider trade-offs between accuracy and processing time.
- Domain Specificity: Tailor the embeddings by considering domain-specific features, especially when dealing with technical or niche vocabularies.
Summary Table
| Initialization Method | Advantages | Disadvantages |
| Random Initialization | Simple, Baseline | Non-semantic, Slow learning |
| Subword Embeddings | Captures morphology Well-suited for rich morphology | Extra computation Struggles with non-native segmentation |
| Similar Word Approximation | Utilizes semantic similarity | Depends on vocabulary Overhead in finding similar meanings |
| Contextual Embeddings | Context-aware Performs well with polysemy | Computationally heavy |
| Lexical Resources | Rich linguistic information | Dependency on resource availability Needs updates |
In conclusion, initializing word embeddings for OOV words involves a mix of strategies depending on the resources, available computation, and domain specificity. The right choice balances semantic accuracy with computational feasibility, aiming to enhance overall model robustness and adaptability.
Related reading
- How to load the saved tokenizer from pretrained model
- How to make the tensorflow hub embeddings servable using tensorflow serving?
- How to make use of pre-trained word embeddings when training a model in sklearn?
- How to Merge Numerical and Embedding Sequential Models to treat categories in \`RNN\`
- How to Merge Numerical and Embedding Sequential Models to treat categories in `RNN`
- How to parse product titles unstructured into structured data?
- How to proceed with NLP task for recognizing intent and slots
- How to read a file without newlines?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.