Compare one-hot and learned representations

Read a complete operation before typing its result. · 177 words

One-hot encoding turns each item in a fixed vocabulary into a vector with one 1 and all other entries 0. For the vocabulary {cat, dog, bird}, cat is [1,0,0], dog is [0,1,0], and bird is [0,0,1]. The dot product of cat and dog is 0, and so is the dot product of cat and bird. The encoding records identity but not similarity. One-hot is useful for exact lookup, but every distinct word remains equally distant in this representation.

A learned representation, often called an embedding, assigns each item a dense vector whose values are adjusted during model training. Suppose the model learns cat=[1,0], dog=[0.8,0.2], and bird=[0,1]. Compare them with cosine similarity, the dot product divided by both vector lengths. Cat and dog score 0.8/(1*sqrt(0.68)), about 0.97. Dog and bird score 0.2/(sqrt(0.68)*1), about 0.24. Thus the learned space says cat is much closer to dog than to bird, although all three one-hot vectors are equally far apart. These embedding values are given training outputs for a hand-checkable example; in a real system, training would produce them from data.

Check your recall

Using the learned vectors, which pair is more similar, and what are their approximate cosine scores?

Check your answer

Cat and dog are more similar: their cosine score is about 0.97, compared with about 0.24 for dog and bird. Their one-hot dot products are both 0.

Sources and credits

Original KeyTouch lesson. Background reading · Google · Neural networks.