Notes

Contextual Embeddings

“The bank refused the loan” and “we sat on the bank watching the river” use the same four letters for two unrelated things. A lookup table of word vectors has to pick one meaning and live with it. Contextual embeddings refuse the choice: the vector is computed from the sentence, so the two banks come out in different places.

Where the vector comes from
Instead of a fixed matrix indexed by word, the representation is the output of a neural encoder run over the whole input. Feed a sentence into ELMo or BERT and read off a hidden state per token; attention has already mixed in the surrounding words, so the same token in a different sentence yields a different vector. The practical consequences:

  • Polysemy stops being a modelling problem and becomes something the encoder handles for free.
  • Unseen words still get a representation, because the input is split into subword pieces rather than looked up whole.
  • Which layer you read matters — the top layers are the most task-specific, middle layers often transfer better.

What they are used for
Token vectors feed sequence labelling, and entity recognition and tagging improved sharply once taggers were built on contextual rather than static input. Pooling those vectors — or better, using an encoder trained for the job such as Sentence-BERT — gives a sentence vector for semantic similarity and dense retrieval, which is how a search box matches “running shoes” to a page that only says “trainers”. The cost is real: every embedding now requires a forward pass where a static table was a memory read.

Why it matters
Most of what a word means on a given page comes from its neighbours. Freezing one vector per word averages every sense together and guarantees a blurred representation; computing it in context is what made transfer learning the default way to start an NLP system.

Contextual embeddings represent a token with a vector computed from the sentence it appears in, produced by running a neural encoder such as ELMo or BERT over the input rather than looking the word up in a fixed table. The same word in two senses therefore receives two different vectors, and unseen words are covered through subword pieces. They underpin modern tagging, semantic similarity and dense retrieval, at the cost of a forward pass per use.

The word “bank” means one thing beside a river and another on the high street. You never notice the switch, because you read the whole sentence, not the word on its own.

Older software could not do that. Each word got one fixed set of numbers, with river and money blended together in it. Contextual embeddings fix this by working out a word's numbers freshly for every sentence it turns up in. The result is that a search for “cheap trainers” can find a page about affordable running shoes, and a program reading the news can tell the company called Apple from the fruit. Same spelling, different meaning, different numbers.