BERT Embeddings — Contextual Text Representations

Machine Learning
Introduced by
Devlin, Chang, Lee & Toutanova (Google AI)
Year
2019
Type
Contextual transformer text-representation method

What does it do?

BERT-based text embeddings, introduced by Devlin and colleagues at Google AI in 2019, turn text into context-sensitive dense vectors using a bidirectional Transformer encoder.

Because a word's meaning depends on its context, BERT assigns it different vectors in different sentences, unlike static word-vector methods such as Word2Vec.

The intuition behind the method

A word like "bank" means something different on a river than in finance, yet older embedding methods give it a single fixed vector regardless of where it appears. BERT reads each word together with the words around it, so the same word receives a different vector depending on its sentence. This contextual reading is what allows the embeddings to capture meaning that static word vectors miss.

History

BERT was introduced by Devlin, Chang, Lee and Toutanova (2019), who pretrained a deep bidirectional Transformer on large text corpora and reported strong results on language-understanding tasks. A probing study by Tenney, Das and Pavlick (2019) analysed where linguistic information is localised within BERT and found that the network represents the steps of the classical NLP pipeline (POS tagging, parsing, named entity recognition, semantic roles, and coreference) in an interpretable way, appearing in roughly that order.

When to use it

Choose BERT embeddings when your data is text and you need representations that depend on context, such as when the same word can carry different meanings across documents. A pretrained model should exist for the language and domain of your corpus; if it does not, results will be weaker. BERT embeddings suit classification, similarity, and relationship analysis. If you have very little text, or only a small labelled set for fine-tuning, fine-tuning may be unstable; a static pretrained embedding such as GloVe is a sensible baseline to compare against on your own validation data. If you have no text data at all, BERT embeddings cannot be produced, and methods designed for your actual data type are more appropriate.

Limitations and considerations

  • A suitable model must be selected, multilingual or domain-specific, to match the corpus; a mismatch degrades the vectors.
  • Encoding is computationally demanding, so large corpora are usually processed on GPU hardware.
  • Fine-tuning requires a labelled dataset large enough to support it, and very small corpora do not provide that.
  • Embeddings are high-dimensional and less directly interpretable than frequency- or lexicon-based features.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Context-sensitive: the same word gets different vectors depending on its surrounding text.
  • Captures context-dependent meaning, whereas static methods such as Word2Vec assign one vector per word regardless of context.
  • Bidirectional encoding reads each token in light of the full context on both sides.
  • Pretrained models cover many languages and specialised domains, so analysis can start without training a model from scratch.

Applications

  • Text classification using context-aware document representations.
  • Measuring semantic similarity between documents or passages.
  • Relationship and explanatory analysis over text where word meaning depends on context.
  • Producing features for downstream NLP models.

How it works

How it works

1

An appropriate pretrained BERT model is chosen to match the corpus: a multilingual model for non-English or mixed-language text, or a domain-specific model when the text comes from a specialised field. The match between the model and the language and domain of the corpus has a large influence on embedding quality.

2

Each document is split into the sub-word tokens the chosen model expects. Tokenisation breaks words into the units the model was pretrained on, so that unfamiliar or compound words can still be represented.

3

The bidirectional Transformer encoder passes over the tokens and produces a context-sensitive vector for each one, reading every token in light of the surrounding text. The same word yields different vectors in different contexts, which is what gives the representation its richness.

4

The resulting embeddings feed downstream tasks such as classification, similarity, or relationship analysis. BERT produces one vector per token, so a document-level representation requires a pooling choice: the vector of the special [CLS] token, or the mean of the token vectors, and optionally the encoder layer from which the vectors are taken. The choice should be stated and validated on your task, because different choices can give different results. On larger corpora the model can also be fine-tuned to the task, which requires considerably more labelled data than extracting embeddings alone. Encoding many documents is computationally demanding, so larger jobs are usually run on GPU hardware.

Common pitfalls

  • Applying an English-only model to text in another language degrades quality; choose a multilingual or matching model.
  • Fine-tuning on a very small labelled dataset can be unstable; comparing against a static embedding baseline such as GloVe on your own validation data is advisable.
  • Without text data, BERT embeddings cannot be generated at all; the method does not apply to structured, non-textual variables.
  • Encoding a large corpus on CPU alone can be very slow, so the compute budget should be planned before starting.
  • Standard BERT models accept a fixed maximum number of sub-word tokens. Longer documents are truncated or must be split into chunks, and the chunking choice changes the resulting vectors.

Frequently asked questions

How is BERT different from Word2Vec or GloVe?

Word2Vec and GloVe assign each word a single static vector regardless of context. BERT is contextual: it reads each word together with its surrounding text and produces a different vector for the same word in different sentences, which captures meaning that static embeddings miss.

How much text do I need?

Extracting embeddings from a pretrained model does not require training, so it can be done on small collections. Fine-tuning the model to your task needs considerably more labelled data. If your corpus is small, fine-tuning may be unstable, so comparing against a static embedding baseline such as GloVe on your own validation data is advisable.

Do I need a GPU?

Not necessarily for small datasets, but a GPU is recommended for large ones because encoding many documents through a Transformer is computationally demanding and can be very slow on a CPU.

Can BERT handle languages other than English?

Yes, provided you select a model that matches your text: a multilingual model, or a model trained for the specific language or domain of your corpus. Applying an English-only model to other-language text is a common cause of poor results.

Related methods

Sources

  1. Devlin, J., Chang, M.-W., Lee, K. & Toutanova, K. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. NAACL-HLT, 4171-4186.

    Open DOI reference
  2. Tenney, I., Das, D. & Pavlick, E. (2019). BERT Rediscovers the Classical NLP Pipeline. Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (ACL), 4593-4601.

    Open DOI reference