Natural Language Processing/Neural Networks & Word Embeddings
13 / 67

02/2024Natural Language Processing

Neural Networks & Word Embeddings

A PyTorch neural network for multi-class text classification, plus a look at the geometry of its word embeddings via cosine similarity and t-SNE.

╌╌╌╌

A feedforward neural network in PyTorch for multi-class classification, paired with an inspection of the word embeddings it learns — measuring closeness with cosine similarity and projecting the high-dimensional space to a plane with t-SNE.

The classifier is a network of linear layers and nonlinearities, mapping an input to class scores by alternating affine maps with an elementwise nonlinearity. With one hidden layer,

where is a nonlinearity such as ReLU. The nonlinearity is what lets the network represent functions a single linear layer cannot; stacked layers compose into richer decision boundaries.

The output scores become a distribution over the classes with softmax, and training minimizes the cross-entropy against the true label:

PyTorch handles the gradients by backpropagation; the weights update by gradient descent over minibatches.

The softmax carries a cost. That denominator sums over every class, which is cheap for a handful of labels but ruinous when the "classes" are a whole vocabulary, as in the skip-gram objectives that learn general-purpose embeddings. Negative sampling sidesteps the sum: each real (word, context) pair is trained against a few randomly drawn negative pairs, and one giant normalization collapses into a handful of binary present-or-absent decisions. The vectors that fall out carry the same geometry, learned at a fraction of the cost.

Embeddings carry meaning as geometry. The first layer maps each word to a dense vector, trained end to end. After training, related words sit close together, and closeness is measured by the angle between vectors rather than their length:

Cosine similarity ignores magnitude, so frequent and rare words compare on equal footing. t-SNE then compresses the embedding space to two dimensions for plotting, preserving local neighborhoods so clusters of related words show up visually — a qualitative check that the model learned structure and not noise.

A 2-D projection of a learned embedding space. Related words fall into loose clusters, and a consistent offset between analogous pairs (the dashed parallelogram) is the geometry behind .

The clusters are the payoff: words with similar usage land near each other, and the parallelogram shows that a relation like gender is a single shared direction, so the space encodes structure, not just proximity.

You can read the full report.

References

  1. Project repository
  2. Reference notes: Neural Networks and Neural Language Models
  3. Reference notes: Vector Semantics and Embeddings
  4. Reference notes: Static Word Embeddings: word2vec and After

╌╌ END ╌╌