liveBook · Manning

Embedding

As explained in Natural Language Processing in Action, Second Edition

A vector representation is a numerical vector that encodes the properties or meaning of a text, statement, word, or document.

An embedding is a numerical vector that represents aspects of a word’s meaning, so a computer can compare words and perform mathematical operations on them. The same approach can represent sentences and paragraphs. An encoder or vectorizer turns text into a numerical representation. section 1.1.1 section 6.2

Think of a word vector as an unlabeled list of attributes: each number occupies a consistent position, so corresponding entries line up when vectors are combined. In an embedding space, words that are closer together tend to be more similar in meaning. The individual entries need not have simple labels like the attributes on a character sheet. section 6.0 section 6.2.1

>>> from nessvec.indexers import Index      #1
>>> index = Index(num_vecs=100_000)        #2
>>> index.get_nearest("Engineer").round(2)
Engineer       0.00
engineer       0.23
Engineers      0.27
Engineering    0.30
Architect      0.35
engineers      0.36
Technician     0.36
Programmer     0.39
Consultant     0.39
Scientist      0.39

This Python example uses a nessvec index to find neighbors of “Engineer.” The results include related occupations as well as spelling and capitalization variants. section 6.2.1

Word2Vec learns word representations from local context: words occurring near one another provide training signals without a dictionary or manually labeled definitions. Training starts with a large collection of text, tokenized and segmented into sentences; small neighborhoods of words supply the context. section 6.2.4

Not every text vector is an embedding. A bag-of-words vector counts vocabulary items, while TF–IDF weights terms for retrieval. These representations are useful, but their word-count features differ from embeddings that support comparisons of meaning. Bag-of-words also discards word order, which can matter for logical relationships. section 1.1.1 section 1.7

Search and analogy

For semantic search, a query and candidate questions can be represented as vectors and compared to find a related question. In one FAQ example, a query about decreasing overfitting matches a question about reducing it. Word-vector arithmetic can also express an analogy: subtract the vector for germs from Louis Pasteur’s vector, then add the vector for physics. Such operations are useful experiments, not guarantees of a correct answer; an answer must be in the vocabulary. section 4.9 section 6.2.3

Choose the representation for the task, and test its results. A plausible match or analogy does not mean that a chatbot is generally intelligent; embeddings give software a way to calculate with patterns in language, not a guarantee of reliable understanding. section 1.5.3 section 6.2.5

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains vector intuition, local-context learning, similarity, and analogy arithmetic, with practical retrieval examples.
Examples
Uses a nessvec occupation-neighbor lookup, an overfitting FAQ search, and a scientist-and-subject analogy.
What it assumes
Assumes familiarity with tokenization and sentence segmentation when preparing text for Word2Vec training.
What you can do after
Apply it via nessvec to inspect nearby words and evaluate semantic matches and analogy queries.
How it connects
Connects embeddings to Word2Vec, semantic search, bag of words, TF–IDF, and latent semantic analysis.

Also explained in

Compare how 51 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.