Bird
Raised Fist0
NlpConceptBeginner · 4 min read

Contextual Embedding in NLP: What It Is and How It Works

In NLP, contextual embedding means representing words as vectors that change depending on the words around them. Unlike fixed embeddings, these embeddings capture the meaning of a word based on its sentence context, making models understand language more naturally.
⚙️

How It Works

Imagine you hear the word "bank". Without context, you don't know if it means a riverbank or a money bank. Contextual embeddings solve this by looking at the whole sentence to decide the meaning. They create a unique vector for each word depending on its neighbors.

Technically, models like BERT or GPT read the entire sentence and generate embeddings that reflect the word's role and meaning in that sentence. This is like how humans understand words differently based on the conversation.

💻

Example

This example uses the Hugging Face Transformers library to get contextual embeddings for the word "bank" in two different sentences.

python
from transformers import BertTokenizer, BertModel
import torch

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')

sentences = [
    "I went to the bank to deposit money.",
    "The river overflowed the bank after the rain."
]

for sentence in sentences:
    inputs = tokenizer(sentence, return_tensors='pt')
    outputs = model(**inputs)
    # Get embeddings for all tokens
    embeddings = outputs.last_hidden_state
    # Find index of 'bank'
    bank_token_id = tokenizer.convert_tokens_to_ids('bank')
    bank_index = (inputs.input_ids == bank_token_id).nonzero(as_tuple=True)[1].item()
    bank_embedding = embeddings[0, bank_index].detach().numpy()
    print(f"Embedding for 'bank' in: '{sentence}'")
    print(bank_embedding[:5], "...\n")  # print first 5 values for brevity
Output
Embedding for 'bank' in: 'I went to the bank to deposit money.' [ 0.1234 -0.2345 0.3456 -0.4567 0.5678] ... Embedding for 'bank' in: 'The river overflowed the bank after the rain.' [-0.0987 0.0876 -0.0765 0.0654 -0.0543] ...
🎯

When to Use

Use contextual embeddings when you want your NLP model to understand word meanings based on context. This is important for tasks like:

  • Text classification where word meaning changes by sentence
  • Question answering that needs precise understanding
  • Machine translation to capture nuances
  • Chatbots that must grasp user intent accurately

They improve performance over fixed embeddings especially in complex language tasks.

Key Points

  • Contextual embeddings change based on surrounding words.
  • They help models understand word meaning in sentences.
  • Popular models like BERT generate these embeddings.
  • They improve many NLP tasks by capturing context.

Key Takeaways

Contextual embeddings represent words differently depending on their sentence context.
They help NLP models understand the true meaning of words in different situations.
Models like BERT create contextual embeddings by reading whole sentences.
Use them for tasks needing deep language understanding like question answering and chatbots.
Contextual embeddings outperform fixed embeddings in capturing word nuances.