1. Home
  2. Explore
  3. Computer Science
  4. Word Embeddings in NLP

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Word Embeddings in NLP

  • 21Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Problems with One-Hot Vectors

What are two major problems with one-hot vectors for representing words in NLP?

21 questions

  1. 1

    What are two major problems with one-hot vectors for representing words in NLP?

    Model answer

    First, they are computationally inefficient because the vector length equals the vocabulary size. Second, they lack semantic meaning: the Euclidean distance between any two one-hot vectors is always 2\sqrt{2}2​, so no notion of word similarity.

    Explanation

    One-hot vectors have a 1 at the word's index and 0 elsewhere. The Euclidean distance between any two distinct one-hot vectors is 2\sqrt{2}2​ because the difference has two non-zero entries (1 and -1), squared to 1+1=2. This fails to reflect semantic relationships.

  2. 2

    What is the Euclidean distance between two different one-hot vectors?

    Model answer

    2\sqrt{2}2​.

    Explanation

    Each one-hot vector has a single 1 and the rest zeros. For two different words, the positions of the 1s differ. Subtracting yields a vector with a 1 and a -1; squaring gives 1+1=2; square root gives 2\sqrt{2}2​.

  3. 3

    Why does using one-hot vectors for a vocabulary of 500,000 words create a computational problem?

    Model answer

    Each word is represented by a vector of length 500,000, leading to enormous memory usage and many parameters to learn, increasing compute and data requirements.

    Explanation

    The feature dimension equals vocabulary size, so models have many input weights. Even after reducing vocabulary to frequent words, the embedding dimension remains large and sparse.

  4. 4

    What is the key property that word embeddings should have regarding semantic relationships?

    Model answer

    Similar words should have vectors that are close in Euclidean distance, and vector differences should capture semantic relationships (e.g., direction).

    Explanation

    Word embeddings aim to represent meaning such that geometric relationships (distance and direction) correspond to semantic relationships. For example, man⃗−woman⃗≈king⃗−queen⃗\vec{man} - \vec{woman} \approx \vec{king} - \vec{queen}man−woman≈king​−queen​.

  5. 5

    What does John Firth's statement 'You shall know a word by the company it keeps' imply for learning word embeddings?

    Model answer

    Words that occur in similar contexts (e.g., same sentences) tend to have similar meanings, so we can infer semantic relatedness from co-occurrence patterns.

    Explanation

    Firth's dictum is the foundation for distributional semantics. Words that appear in similar contexts (like 'movie' and 'film' both appearing in 'the ___ was great') are likely related. Embedding methods like GloVe exploit this by learning from co-occurrence statistics.

  6. 6

    What is a co-occurrence matrix in the context of word embeddings?

    Model answer

    A matrix where rows and columns correspond to words, and each entry XijX_{ij}Xij​ counts how many times word iii and word jjj appear in the same context (e.g., same sentence) across a corpus.

    Explanation

    The co-occurrence matrix captures global word-word association statistics. For example, 'deep' and 'learning' might co-occur many times, while 'banana' and 'tensor' rarely do.

  7. 7

    Why are the rows of a co-occurrence matrix for two synonyms (like 'movie' and 'film') expected to be similar?

    Model answer

    Synonyms appear in similar contexts, so their co-occurrence counts with other words will be similar, making their row vectors in the matrix resemble each other.

    Explanation

    If two words are interchangeable in sentences, they will co-occur with the same set of words. Thus, their co-occurrence patterns (rows) are highly correlated. GloVe embeddings aim to reconstruct these patterns.

  8. 8

    What is the GloVe model for word embeddings?

    Model answer

    GloVe models the logarithm of the co-occurrence count as the sum of word biases plus the dot product of two embedding vectors: log⁡Xij=bi+bj+wi⋅wj\log X_{ij} = b_i + b_j + \mathbf{w}_i \cdot \mathbf{w}_jlogXij​=bi​+bj​+wi​⋅wj​.

    Explanation

    The equation assumes the observed co-occurrence count arises from inherent word popularity (biases) and a learned interaction (dot product). The embeddings are learned to minimize the squared error between predicted and actual log co-occurrence.

  9. 9

    Why is the logarithm taken of co-occurrence counts in the GloVe model?

    Model answer

    To squash the wide range of positive counts, making the regression better behaved and reducing the influence of extreme values.

    Explanation

    Co-occurrence counts can vary from 0 to millions. Taking the log compresses this range, similar to using log-transformed income in regression. It avoids numerical issues and improves optimization.

  10. 10

    How are GloVe embeddings learned from data?

    Model answer

    By minimizing the mean squared error between predicted log co-occurrence (from biases and dot product) and actual log co-occurrence, using stochastic gradient descent.

    Explanation

    The objective is ∑i,jf(Xij)(wi⋅wj+bi+bj−log⁡Xij)2\sum_{i,j} f(X_{ij}) (\mathbf{w}_i\cdot\mathbf{w}_j + b_i + b_j - \log X_{ij})^2∑i,j​f(Xij​)(wi​⋅wj​+bi​+bj​−logXij​)2, where fff is a weighting function. Gradients are computed and parameters updated via SGD.

  11. 11

    How can we assess the quality of learned word embeddings?

    Model answer

    By checking how well they reconstruct the co-occurrence matrix. Also through qualitative tasks like analogies (e.g., 'king' - 'man' + 'woman' ≈ 'queen').

    Explanation

    If embeddings capture semantics, they should predict co-occurrence counts accurately. Analogy tasks test whether vector arithmetic reflects relationships. Both provide validation.

  12. 12

    What does the embedding dimension (e.g., 100) represent in GloVe?

    Model answer

    It is a hyperparameter that determines the length of the learned dense vector for each word; it captures latent semantic features.

    Explanation

    The dimension is chosen by the practitioner. Larger dimensions can capture more nuances but risk overfitting. Typical sizes are 50, 100, 200, 300.

  13. 13

    What is the role of biases bib_ibi​ and bjb_jbj​ in the GloVe model?

    Model answer

    They capture the inherent frequency of individual words, independent of their relationships to other words.

    Explanation

    Some words (like 'the') occur frequently in many contexts. Biases absorb this overall popularity so that the dot product focuses on relative co-occurrence patterns.

  14. 14

    What is the purpose of the 'embedding dimension' hyperparameter (e.g., 64 vs. 100) when training embeddings from scratch?

    Model answer

    It controls the size of the dense vector; larger dimensions offer more capacity to capture semantic features but increase risk of overfitting.

    Explanation

    Similar to the number of hidden units in a neural network, embedding dimension balances representational power and generalization. It is chosen via validation performance.

  15. 15

    How are sentences handled when using an Embedding layer in Keras with variable lengths?

    Model answer

    Sentences are padded or truncated to a fixed maximum length using a pad token (index 0). The embedding layer looks up vectors for each integer index.

    Explanation

    The TextVectorization layer outputs integer sequences. To create a fixed-size input tensor, sequences shorter than max_length are padded with 0, and longer ones are truncated. The Embedding layer maps each integer to its vector.

  16. 16

    In Keras, what does setting trainable=False on a pre-trained Embedding layer do?

    Model answer

    It freezes the embedding weights so they are not updated during backpropagation; the pre-trained vectors remain fixed.

    Explanation

    When using GloVe embeddings, you may want to keep them as static features, especially when data is limited. This prevents overfitting and reduces computational cost.

  17. 17

    How does the GlobalAveragePooling1D layer work after an Embedding layer?

    Model answer

    It averages the embedding vectors of all tokens in the sentence along the sequence dimension, producing a single vector per input.

    Explanation

    For a sentence padded to length L with embedding dimension D, the output of the Embedding layer is a tensor of shape (L, D). GlobalAveragePooling1D averages over the L dimension, yielding a (D,)-shaped vector. This compresses the sentence into a fixed-size representation.

  18. 18

    When is it beneficial to use pre-trained word embeddings like GloVe instead of training embeddings from scratch?

    Model answer

    When the available dataset is small, because pre-trained embeddings provide general language knowledge that can improve performance.

    Explanation

    Pre-trained embeddings capture broad semantic relationships from large corpora. With limited data, fine-tuning or freezing them helps the model generalize. With abundant data, training from scratch often yields better task-specific representations.

  19. 19

    What is a potential drawback of using pre-trained embeddings like GloVe for a medical or legal domain?

    Model answer

    They may not capture domain-specific jargon and specialized meanings, leading to poorer performance on that task.

    Explanation

    GloVe is trained on general text (e.g., Wikipedia). Terms like 'stenosis' or 'tort' may have different co-occurrence patterns. Fine-tuning on domain data or training from scratch can mitigate this.

  20. 20

    How do biases in the training data affect word embeddings learned from it?

    Model answer

    Embeddings will learn and perpetuate those biases, reflecting stereotypes present in the corpus.

    Explanation

    If the training data contains gender or racial biases (e.g., 'nurse' associated more with 'woman'), the embeddings will capture those associations. This can lead to unfair or harmful outcomes in downstream tasks.

  21. 21

    What is the main limitation of standalone (non-contextual) word embeddings like GloVe?

    Model answer

    They assign each word a single vector regardless of its context, so they cannot handle polysemy (words with multiple meanings).

    Explanation

    Words like 'bank' have different meanings (financial institution, river bank, to bank an airplane). Standalone embeddings average these into one representation, which is often suboptimal. Contextual embeddings (e.g., from transformers) solve this by producing different vectors per context.

1 of 21
Question 1 of 21

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent