1 of 91

CS458 Natural language Processing

Lecture 8 Vector Semantics & Embeddings

Krishnendu Ghosh

Department of Computer Science & Engineering

Indian Institute of Information Technology Dharwad

2 of 91

What do words mean?

N-gram or text classification methods we've seen so far

    • Words are just strings (or indices wi in a vocabulary list)
    • That's not very satisfactory!

Introductory logic classes:

    • The meaning of "dog" is DOG; cat is CAT

∀x DOG(x) ⟶ MAMMAL(x)

Old linguistics joke by Barbara Partee in 1967:

    • Q: What's the meaning of life?
    • A: LIFE

That seems hardly better!

3 of 91

Desiderata

What should a theory of word meaning do for us?

Let's look at some desiderata

From lexical semantics, the linguistic study of word meaning

4 of 91

Lemmas and senses

mouse (N)�1. any of numerous small rodents...�2. a hand-operated device that controls a cursor...

sense

lemma

A sense or “concept” is the meaning component of a word

Lemmas can be polysemous (have multiple senses)

Modified from the online thesaurus WordNet

5 of 91

Relations between senses: Synonymy

Synonyms have the same meaning in some or all contexts.

    • filbert / hazelnut
    • couch / sofa
    • big / large
    • automobile / car
    • vomit / throw up
    • water / H20

6 of 91

Relations between senses: Synonymy

Note that there are probably no examples of perfect synonymy.

    • Even if many aspects of meaning are identical
    • Still may differ based on politeness, slang, register, genre, etc.

7 of 91

Relation: Synonymy?

water/H20

"H20" in a surfing guide?

big/large

my big sister != my large sister

8 of 91

The Linguistic Principle of Contrast

Difference in form > difference in meaning

9 of 91

Abbé Gabriel Girard 1718

​

[I do not believe that there is a synonymous word in any language]

​

"

"

Re: "exact" synonyms

Thanks to Mark Aronoff!

10 of 91

Relation: Similarity

      • Words with similar meanings. Not synonyms, but sharing some element of meaning

​

      • car, bicycle
      • cow, horse

​

11 of 91

Ask humans how similar 2 words are

word1

word2

similarity

vanish

disappear

9.8

behave

obey

7.3

belief

impression

5.95

muscle

bone

3.65

modest

flexible

0.98

hole

agreement

0.3

SimLex-999 dataset (Hill et al., 2015)

12 of 91

Relation: Word relatedness

Also called "word association"

Words can be related in any way, perhaps via a semantic frame or field

​

      • coffee, tea: similar
      • coffee, cup: related, not similar

13 of 91

Semantic field

Words that

    • cover a particular semantic domain
    • bear structured relations with each other.

​

hospitals

surgeon, scalpel, nurse, anaesthetic, hospital

restaurants

waiter, menu, plate, food, menu, chef

houses

door, roof, kitchen, family, bed

​

14 of 91

Relation: Antonymy

Senses that are opposites with respect to only one feature of meaning

Otherwise, they are very similar!

dark/light short/long fast/slow rise/fall

hot/cold up/down in/out

More formally: antonyms can

    • define a binary opposition or be at opposite ends of a scale
      • long/short, fast/slow
    • Be reversives:
      • rise/fall, up/down

15 of 91

Connotation (sentiment)

  • Words have affective meanings
    • Positive connotations (happy)
    • Negative connotations (sad)
  • Connotations can be subtle:
    • Positive connotation: copy, replica, reproduction
    • Negative connotation: fake, knockoff, forgery
  • Evaluation (sentiment!)
    • Positive evaluation (great, love)
    • Negative evaluation (terrible, hate)

​

16 of 91

Connotation

Words seem to vary along 3 affective dimensions:

    • valence: the pleasantness of the stimulus
    • arousal: the intensity of emotion provoked by the stimulus
    • dominance: the degree of control exerted by the stimulus

Osgood et al. (1957)

​

Word

Score

​

Word

Score

Valence

love

1.000

​

toxic

0.008

happy

1.000

​

nightmare

0.005

Arousal

elated

0.960

​

mellow

0.069

frenzy

0.965

​

napping

0.046

Dominance

powerful

0.991

​

weak

0.045

leadership

0.983

​

empty

0.081

Values from NRC VAD Lexicon (Mohammad 2018)

17 of 91

So far

Concepts or word senses

    • Have a complex many-to-many association with words (homonymy, multiple senses)

Have relations with each other

    • Synonymy
    • Antonymy
    • Similarity
    • Relatedness
    • Connotation

18 of 91

Vector Semantics

19 of 91

Computational models of word meaning

Can we build a theory of how to represent word meaning, that accounts for at least some of the desiderata?

We'll introduce vector semantics

The standard model in language processing!

Handles many of our goals!

20 of 91

Ludwig Wittgenstein

PI #43:

"The meaning of a word is its use in the language"

21 of 91

Let's define words by their usages

One way to define "usage":

words are defined by their environments (the words around them)

​

Zellig Harris (1954):

If A and B have almost identical environments we say that they are synonyms.

​

​

22 of 91

What does recent English borrowing ongchoi mean?

Suppose you see these sentences:

    • Ong choi is delicious sautéed with garlic.
    • Ong choi is superb over rice
    • Ong choi leaves with salty sauces

And you've also seen these:

    • …spinach sautéed with garlic over rice
    • Chard stems and leaves are delicious
    • Collard greens and other salty leafy greens

Conclusion:

    • Ongchoi is a leafy green like spinach, chard, or collard greens
      • We could conclude this based on words like "leaves" and "delicious" and "sauteed"

​

23 of 91

Ongchoi: Ipomoea aquatica "Water Spinach"

Yamaguchi, Wikimedia Commons, public domain

空心菜

kangkong

rau muống

…

24 of 91

Idea 1: Defining meaning by linguistic distribution

Let's define the meaning of a word by its distribution in language use, meaning its neighboring words or grammatical environments.

​

25 of 91

Idea 2: Meaning as a point in space (Osgood et al. 1957)

3 affective dimensions for a word

    • valence: pleasantness
    • arousal: intensity of emotion
    • dominance: the degree of control exerted

​

​

​

​

​

​

​

Hence the connotation of a word is a vector in 3-space

​

Word

Score

​

Word

Score

Valence

love

1.000

​

toxic

0.008

happy

1.000

​

nightmare

0.005

Arousal

elated

0.960

​

mellow

0.069

frenzy

0.965

​

napping

0.046

Dominance

powerful

0.991

​

weak

0.045

leadership

0.983

​

empty

0.081

NRC VAD Lexicon

(Mohammad 2018)

26 of 91

Idea 1: Defining meaning by linguistic distribution��Idea 2: Meaning as a point in multidimensional space

27 of 91

Defining meaning as a point in space based on distribution

Each word = a vector (not just "good" or "w45")

Similar words are "nearby in semantic space"

We build this space automatically by seeing which words are nearby in text

28 of 91

We define meaning of a word as a vector

Called an "embedding" because it's embedded into a space (see textbook)

The standard way to represent meaning in NLP

Every modern NLP algorithm uses embeddings as the representation of word meaning

Fine-grained model of meaning for similarity

​

​

29 of 91

Intuition: why vectors?

Consider sentiment analysis:

​

    • With words, a feature is a word identity
      • Feature 5: 'The previous word was "terrible"'
      • requires exact same word to be in training and test

​

    • With embeddings:
      • Feature is a word vector
      • 'The previous word was vector [35,22,17…]
      • Now in the test set we might see a similar vector [34,21,14]
      • We can generalize to similar but unseen words!!!

​

30 of 91

We'll discuss 2 kinds of embeddings

tf-idf

    • Information Retrieval workhorse!
    • A common baseline model
    • Sparse vectors
    • Words are represented by (a simple function of) the counts of nearby words

Word2vec

    • Dense vectors
    • Representation is created by training a classifier to predict whether a word is likely to appear nearby
    • Later we'll discuss extensions called contextual embeddings

31 of 91

From now on:�Computing with meaning representations�instead of string representations

32 of 91

Words and Vectors

33 of 91

Term-document matrix

Each document is represented by a vector of words

34 of 91

Visualizing document vectors

35 of 91

Vectors are the basis of information retrieval

Vectors are similar for the two comedies

​

But comedies are different than the other two

Comedies have more fools and wit and fewer battles.

36 of 91

Idea for word meaning: Words can be vectors too!!!

battle is "the kind of word that occurs in Julius Caesar and Henry V"

​

fool is "the kind of word that occurs in comedies, especially Twelfth Night"

37 of 91

More common: word-word matrix�(or "term-context matrix")

Two words are similar in meaning if their context vectors are similar

​

37

38 of 91

39 of 91

Cosine for Computing Word Similarity

40 of 91

Computing word similarity: Dot product and cosine

The dot product between two vectors is a scalar:

​

​

The dot product tends to be high when the two vectors have large values in the same dimensions

Dot product can thus be a useful similarity metric between vectors

​

41 of 91

Problem with raw dot-product

Dot product favors long vectors

Dot product is higher if a vector is longer (has higher values in many dimension)

Vector length:

​

​

Frequent words (of, the, you) have long vectors (since they occur many times with other words).

So dot product overly favors frequent words

42 of 91

Alternative: cosine for computing word similarity

Based on the definition of the dot product between two vectors a and b

​

43 of 91

Cosine as a similarity metric

-1: vectors point in opposite directions

+1: vectors point in same directions

0: vectors are orthogonal

​

​

​

But since raw frequency values are non-negative, the cosine for term-term matrix vectors ranges from 0–1

​

​

​

43

44 of 91

Cosine examples

44

​

pie

data

computer

cherry

442

8

2

digital

5

1683

1670

information

5

3982

3325

45 of 91

Visualizing cosines �(well, angles)

46 of 91

TF-IDF

47 of 91

But raw frequency is a bad representation

  • The co-occurrence matrices we have seen represent each cell by word frequencies.
  • Frequency is clearly useful; if sugar appears a lot near apricot, that's useful information.
  • But overly frequent words like the, it, or they are not very informative about the context
  • It's a paradox! How can we balance these two conflicting constraints?

​

48 of 91

Two common solutions for word weighting

 

Words like "the" or "it" have very low idf

See if words like "good" appear more often with "great" than we would expect by chance

49 of 91

Term frequency (tf) in the tf-idf algorithm

We could imagine using raw count:

� tft,d = count(t,d)

​

But instead of using raw count, we usually squash a bit:

​

50 of 91

Document frequency (df)

dft is the number of documents t occurs in.

(note this is not collection frequency: total count across all documents)

"Romeo" is very distinctive for one Shakespeare play:

​

​

​

51 of 91

Inverse document frequency (idf)

N is the total number of documents

in the collection

52 of 91

What is a document?

​

​

Could be a play or a Wikipedia article

But for the purposes of tf-idf, documents can be anything; we often call each paragraph a document!

​

53 of 91

Final tf-idf weighted value for a word

Raw counts:

​

​

​

tf-idf:

54 of 91

Word2Vec

55 of 91

Sparse versus dense vectors

tf-idf (or PMI) vectors are

    • long (length |V|= 20,000 to 50,000)
    • sparse (most elements are zero)

Alternative: learn vectors which are

    • short (length 50-1000)
    • dense (most elements are non-zero)

56 of 91

Sparse versus dense vectors

Why dense vectors?

    • Short vectors may be easier to use as features in machine learning (fewer weights to tune)
    • Dense vectors may generalize better than explicit counts
    • Dense vectors may do better at capturing synonymy:
      • car and automobile are synonyms; but are distinct dimensions
        • a word with car as a neighbor and a word with automobile as a neighbor should be similar, but aren't
    • In practice, they work better

56

57 of 91

Common methods for getting short dense vectors

“Neural Language Model”-inspired models

    • Word2vec (skipgram, CBOW), GloVe

Singular Value Decomposition (SVD)

    • A special case of this is called LSA – Latent Semantic Analysis

Alternative to these "static embeddings":

    • Contextual Embeddings (ELMo, BERT)
    • Compute distinct embeddings for a word in its context
    • Separate embeddings for each token of a word

58 of 91

Simple static embeddings you can download!

Word2vec (Mikolov et al)

https://code.google.com/archive/p/word2vec/

​

GloVe (Pennington, Socher, Manning)

http://nlp.stanford.edu/projects/glove/

​

59 of 91

Word2vec

Popular embedding method

Very fast to train

Code available on the web

Idea: predict rather than count

Word2vec provides various options. We'll do:

skip-gram with negative sampling (SGNS)

�

60 of 91

Word2vec

Instead of counting how often each word w occurs near "apricot"

    • Train a classifier on a binary prediction task:
      • Is w likely to show up near "apricot"?

We don’t actually care about this task

      • But we'll take the learned classifier weights as the word embeddings

Big idea: self-supervision:

      • A word c that occurs near apricot in the corpus acts as the gold "correct answer" for supervised learning
      • No need for human labels
      • Bengio et al. (2003); Collobert et al. (2011)

​

​

​

61 of 91

Approach: predict if candidate word c is a "neighbor"

  1. Treat the target word t and a neighboring context word c as positive examples.
  2. Randomly sample other words in the lexicon to get negative examples
  3. Use logistic regression to train a classifier to distinguish those two cases
  4. Use the learned weights as the embeddings

​

​

62 of 91

Skip-Gram Training Data

Assume a +/- 2 word window, given training sentence:

​

…lemon, a [tablespoon of apricot jam, a] pinch…

​

​

​

c1 c2 c3 c4

​

[target]

63 of 91

Skip-Gram Classifier

(assuming a +/- 2 word window)

​

…lemon, a [tablespoon of apricot jam, a] pinch…

c1 c2 [target] c3 c4

​

Goal: train a classifier that is given a candidate (word, context) pair

(apricot, jam)

(apricot, aardvark)

…

And assigns each pair a probability:

P(+|w, c)

P(−|w, c) = 1 − P(+|w, c)

​

​

​

​

64 of 91

Similarity is computed from dot product

Remember: two vectors are similar if they have a high dot product

    • Cosine is just a normalized dot product

So:

    • Similarity(w,c) ∝ w ∙ c

We’ll need to normalize to get a probability

    • (cosine isn't a probability either)

65 of 91

Turning dot products into probabilities

Sim(w,c) ≈ w ∙ c

To turn this into a probability

We'll use the sigmoid from logistic regression:

​

66 of 91

How Skip-Gram Classifier computes P(+|w, c)

This is for one context word, but we have lots of context words.

We'll assume independence and just multiply them:

67 of 91

Skip-gram classifier: summary

A probabilistic classifier, given

    • a test target word w
    • its context window of L words c1:L

Estimates probability that w occurs in this window based on similarity of w (embeddings) to c1:L (embeddings).

​

To compute this, we just need embeddings for all the words.

68 of 91

These embeddings we'll need: a set for w, a set for c

69 of 91

Word2Vec: Learning the embeddings

70 of 91

Skip-Gram Training data

​

…lemon, a [tablespoon of apricot jam, a] pinch…

c1 c2 [target] c3 c4

​

​

70

71 of 91

Skip-Gram Training data

​

…lemon, a [tablespoon of apricot jam, a] pinch…

c1 c2 [target] c3 c4

​

​

71

For each positive example we'll grab k negative examples, sampling by frequency

72 of 91

Skip-Gram Training data

​

…lemon, a [tablespoon of apricot jam, a] pinch…

c1 c2 [target] c3 c4

​

​

72

73 of 91

Word2vec: how to learn vectors

Given the set of positive and negative training instances, and an initial set of embedding vectors

The goal of learning is to adjust those word vectors such that we:

    • Maximize the similarity of the target word, context word pairs (w , cpos) drawn from the positive data
    • Minimize the similarity of the (w , cneg) pairs drawn from the negative data.

​

73

74 of 91

Loss function for one w with cpos , cneg1 ...cnegk

Maximize the similarity of the target with the actual context words, and minimize the similarity of the target with the k negative sampled non-neighbor words.

​

75 of 91

Learning the classifier

How to learn?

    • Stochastic gradient descent!

​

We’ll adjust the word weights to

    • make the positive pairs more likely
    • and the negative pairs less likely,
    • over the entire training set.

​

76 of 91

Intuition of one step of gradient descent

77 of 91

Reminder: gradient descent

 

78 of 91

The derivatives of the loss function

79 of 91

Update equation in SGD

Start with randomly initialized C and W matrices, then incrementally do updates

80 of 91

Two sets of embeddings

SGNS learns two sets of embeddings

Target embeddings matrix W

Context embedding matrix C

It's common to just add them together, representing word i as the vector wi + ci

81 of 91

Summary: How to learn word2vec (skip-gram) embeddings

Start with V random d-dimensional vectors as initial embeddings

Train a classifier based on embedding similarity

    • Take a corpus and take pairs of words that co-occur as positive examples
    • Take pairs of words that don't co-occur as negative examples
    • Train the classifier to distinguish these by slowly adjusting all the embeddings to improve the classifier performance
    • Throw away the classifier code and keep the embeddings.

82 of 91

Properties of Embeddings

83 of 91

The kinds of neighbors depend on window size

Small windows (C= +/- 2) : nearest words are syntactically similar words in same taxonomy

    • Hogwarts nearest neighbors are other fictional schools
      • Sunnydale, Evernight, Blandings

Large windows (C= +/- 5) : nearest words are related words in same semantic field

    • Hogwarts nearest neighbors are Harry Potter world:
      • Dumbledore, half-blood, Malfoy

​

84 of 91

Analogical relations

The classic parallelogram model of analogical reasoning (Rumelhart and Abrahamson 1973)

To solve: "apple is to tree as grape is to _____"

Add tree – apple to grape to get vine

​

85 of 91

Analogical relations via parallelogram

The parallelogram method can solve analogies with both sparse and dense embeddings (Turney and Littman 2005, Mikolov et al. 2013b)

king – man + woman is close to queen

Paris – France + Italy is close to Rome

For a problem a:a*::b:b*, the parallelogram method is:

​

86 of 91

Structure in GloVE Embedding space

87 of 91

Caveats with the parallelogram method

It only seems to work for frequent words, small distances and certain relations (relating countries to capitals, or parts of speech), but not others. (Linzen 2016, Gladkova et al. 2016, Ethayarajh et al. 2019a)

​

Understanding analogy is an open area of research (Peterson et al. 2020)

​

88 of 91

Embeddings as a window onto historical semantics

Train embeddings on different decades of historical text to see meanings shift

~30 million books, 1850-1990, Google Books data

William L. Hamilton, Jure Leskovec, and Dan Jurafsky. 2016. Diachronic Word Embeddings Reveal Statistical Laws of Semantic Change. Proceedings of ACL.

89 of 91

Embeddings reflect cultural bias!

Ask “Paris : France :: Tokyo : x”

    • x = Japan

Ask “father : doctor :: mother : x”

    • x = nurse

Ask “man : computer programmer :: woman : x”

    • x = homemaker

Bolukbasi, Tolga, Kai-Wei Chang, James Y. Zou, Venkatesh Saligrama, and Adam T. Kalai. "Man is to computer programmer as woman is to homemaker? debiasing word embeddings." In NeurIPS, pp. 4349-4357. 2016.

Algorithms that use embeddings as part of e.g., hiring searches for programmers, might lead to bias in hiring

90 of 91

Historical embedding as a tool to study cultural biases

    • Compute a gender or ethnic bias for each adjective: e.g., how much closer the adjective is to "woman" synonyms than "man" synonyms, or names of particular ethnicities
    • Embeddings for competence adjective (smart, wise, brilliant, resourceful, thoughtful, logical) are biased toward men, a bias slowly decreasing 1960-1990
    • Embeddings for dehumanizing adjectives (barbaric, monstrous, bizarre) were biased toward Asians in the 1930s, bias decreasing over the 20th century.
  • These match the results of old surveys done in the 1930s

​

Garg, N., Schiebinger, L., Jurafsky, D., and Zou, J. (2018). Word embeddings quantify 100 years of gender and ethnic stereotypes. Proceedings of the National Academy of Sciences 115(16), E3635–E3644.

91 of 91

Thank You