Inferensys

Glossary

Contextual Embedding

A dynamic vector representation of a word that changes based on surrounding text, enabling models like BERT to distinguish between the cardiological and dermatological senses of 'MI'.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
DYNAMIC VECTOR REPRESENTATION

What is Contextual Embedding?

A contextual embedding is a dynamic vector representation of a word that changes based on its surrounding text, enabling models to distinguish between multiple meanings of polysemous terms.

A contextual embedding is a dense numerical vector that represents a token's meaning as a function of its entire linguistic context, rather than assigning a single static representation. Unlike traditional word embeddings like Word2Vec, where the word 'MI' has one fixed vector regardless of usage, a contextual model such as BERT or ClinicalBERT generates a unique embedding for each occurrence. This allows the model to encode the specific semantic properties activated by surrounding words, distinguishing between 'MI' meaning Myocardial Infarction in a cardiology note and 'MI' meaning Mitral Insufficiency in a surgical report.

The mechanism relies on a transformer architecture where self-attention layers compute weighted representations of every token relative to every other token in the input sequence. For clinical abbreviation disambiguation, this means the embedding for an ambiguous acronym is directly shaped by diagnostically relevant context words like 'troponin' or 'regurgitation.' The resulting vector positions the token in a high-dimensional semantic space where its proximity to candidate sense embeddings—such as UMLS concept vectors—can be measured via cosine similarity to select the correct expansion from a sense inventory.

DYNAMIC REPRESENTATION

Key Characteristics of Contextual Embeddings

Contextual embeddings are the foundational mechanism that allows modern NLP models to resolve linguistic ambiguity by generating word representations that are dynamically shaped by surrounding text, rather than using static, one-size-fits-all vectors.

01

Dynamic vs. Static Representation

Unlike static embeddings like Word2Vec where the word 'cold' has a single, unchanging vector, a contextual model generates a unique embedding for 'cold' in 'the patient feels cold' versus 'the patient has a cold.' This dynamic generation is the core mechanism enabling disambiguation. The model's internal attention layers weigh the surrounding words to produce a representation that captures the specific activated sense.

02

Attention-Based Context Weighing

The self-attention mechanism in transformer architectures computes a weighted sum of all other tokens in the input sequence to build the embedding for a target token. For an ambiguous abbreviation like 'MI', the model learns to assign high attention weights to relevant context words:

  • Cardiology context: 'chest pain,' 'troponin,' 'EKG' → high attention → 'Myocardial Infarction' sense.
  • Cardiology context: 'regurgitation,' 'leaflet,' 'valve' → high attention → 'Mitral Insufficiency' sense. This attention distribution is what makes the embedding contextual.
03

Bidirectional Context Integration

Contextual models like BERT and ClinicalBERT are bidirectional, meaning the embedding for a word is informed by both the tokens to its left and its right simultaneously. This is critical for clinical text where a defining term may appear after the abbreviation:

  • 'The patient's MI was treated with thrombolytics.'
  • 'The patient's MI was treated with a mitral clip.' A unidirectional model would fail to disambiguate until after processing the abbreviation, but a bidirectional model builds the correct sense into the embedding from the start.
04

Cosine Similarity for Sense Mapping

Once a contextual embedding is generated for an ambiguous mention, it must be mapped to a canonical concept. This is typically done by computing the cosine similarity between the mention's embedding and pre-computed embeddings for each candidate sense from a knowledge base like UMLS.

  • The mention embedding for 'MI' in a cardiology note will have a high cosine similarity (e.g., 0.95) with the embedding for the UMLS concept C0027051 (Myocardial Infarction).
  • The same mention in a dermatology note will score higher against C0025202 (Melanoma In Situ). A cosine similarity threshold is then used to accept or reject the top mapping.
05

Subword Tokenization for Rare Terms

Contextual models use subword tokenization algorithms like WordPiece or Byte-Pair Encoding (BPE). This allows them to construct embeddings for rare or misspelled clinical abbreviations by composing representations of known subword fragments. For example, an unseen abbreviation like 'TEE' (Transesophageal Echocardiogram) can be tokenized into ['T', '##EE'], and its embedding is built from the context surrounding these fragments. This is essential for handling the long tail of idiosyncratic clinical shorthand.

06

Layer-Wise Contextualization

Contextual embeddings are not a single output but a hierarchy of representations across the model's layers. Lower layers tend to capture local syntax and morphology, while upper layers capture high-level semantic meaning. For disambiguation, the final hidden layers are typically used as they encode the task-specific sense. Research shows that probing the upper layers of ClinicalBERT reveals distinct vector clusters for different senses of the same abbreviation, confirming that the disambiguation signal is encoded in the embedding geometry.

CONTEXTUAL EMBEDDING

Frequently Asked Questions

Explore the mechanics of dynamic vector representations that allow language models to resolve ambiguity in clinical text by understanding surrounding context.

A contextual embedding is a dynamic vector representation of a word whose numerical values change based on the surrounding text, unlike a static embedding which assigns a single, fixed vector regardless of context. Static models like Word2Vec generate one representation for 'MI,' forcing it to conflate the cardiological and dermatological senses. In contrast, a transformer architecture like BERT generates distinct contextual embeddings for 'MI' in 'Patient admitted with acute MI' versus 'MI diagnosed on skin exam,' allowing downstream systems to correctly distinguish between Myocardial Infarction and Malignant Melanoma. This dynamic generation is the foundational mechanism enabling robust Word Sense Disambiguation in clinical NLP.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.