Text representation as �Vector Semantics
Introduction
Information Retrieval (IR)
Goal: rank documents in a corpus (Web) by the match with the query.
How to quantify this match?
The vector space method:
Translate query/document into vectors in a vector space
one vector for each document
How to convert text into a vector?
Measure distance between vectors (documents)
How to measure vector distance?
A bag of words representation
I love this movie! It's sweet, but with satirical humor. The dialogue is great and the adventure scenes are fun… It manages to be whimsical and romantic while laughing at the conventions of the fairy tale genre. I would recommend it to just about anyone. I've seen it several times, and I'm always happy to see it again whenever I have a friend who hasn't seen it yet.
A bag of words representation
great | 2 |
dialogue | 1 |
recommend | 1 |
it | 6 |
I | 4 |
... | ... |
The Vector-Space Model (IR)
Dimension = t = |vocabulary|
dj = (w1j, w2j, …, wtj)
7
Graphic Representation
Example:
D1 = 2T1 + 3T2 + 5T3
D2 = 3T1 + 7T2 + T3
Q = 0T1 + 0T2 + 2T3
8
T3
T1
T2
D1 = 2T1+ 3T2 + 5T3
D2 = 3T1 + 7T2 + T3
Q = 0T1 + 0T2 + 2T3
7
3
2
5
Cosine Similarity Measure
9
D1 = 2T1 + 3T2 + 5T3 CosSim(D1 , Q) = 10 / √(4+9+25)(0+0+4) = 0.81
D2 = 3T1 + 7T2 + 1T3 CosSim(D2 , Q) = 2 / √(9+49+1)(0+0+4) = 0.13
Q = 0T1 + 0T2 + 2T3
θ2
t3
t1
t2
D1
D2
Q
θ1
D1 is 6 times better than D2 using cosine similarity but only 5 times better using inner product.
CosSim(dj, q) =
Example: Cosine similarity amongst 3 documents
term | SaS | PaP | WH |
affection | 115 | 58 | 20 |
jealous | 10 | 7 | 11 |
gossip | 2 | 0 | 6 |
wuthering | 0 | 0 | 38 |
Term frequencies (counts)
Example: Cosine similarity amongst 3 documents
term | SaS | PaP | WH |
affection | 3.06 | 2.76 | 2.30 |
jealous | 2.00 | 1.85 | 2.04 |
gossip | 1.30 | 0 | 1.78 |
wuthering | 0 | 0 | 2.58 |
cos(SaS,PaP) ≈
0.789 × 0.832 + 0.515 × 0.555 + 0.335 × 0.0 + 0.0 × 0.0
≈ 0.94
cos(SaS,WH) ≈ 0.79
cos(PaP,WH) ≈ 0.69
Question
The Problem
auto
engine
bonnet
tyres
lorry
boot
car
emissions
hood
make
model
trunk
make
hidden
Markov
model
emissions
normalize
Synonymy
Will have small cosine
but are related
Polysemy
Will have large cosine
but not truly related
What do words mean?
Words, Lemmas, Senses, Definitions
sense
lemma
definition
Lemma pepper
Sense 1: spice from pepper plant
Sense 2: the pepper plant itself
Sense 3: another similar plant (Jamaican pepper)
Sense 4: another plant with peppercorns (California pepper)
Sense 5: capsicum (i.e. chili, paprika, bell pepper, etc)
Name these items
Superordinate Basic Subordinate
chair office chair
piano chair rocking chair
furniture lamp torchiere
desk lamp
table end table coffee table
Distributional similarity �(Ludwig Wittgenstein):
"The meaning of a word is its use in the language"
A data-driven approach!
What does ongchoi mean?
Suppose you see these sentences:
Ong choi: Ipomoea aquatica �"Water Spinach"
Yamaguchi, Wikimedia Commons, public domain
Let's define words by their usages
word-word matrix�(or "term-context matrix")
23
24
| large | data | computer |
apricot | 1 | 0 | 0 |
digital | 0 | 1 | 2 |
information | 1 | 6 | 1 |
Which pair of words is more similar?
cosine(apricot,information) =
cosine(digital,information) =
cosine(apricot,digital) =
Visualizing cosines �(angles)
Ask humans how similar two 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)
Vector Semantics, part II:
�Dimensionality reduction & �Word embeddings
Alternative: dense vectors
29
Singular Value Decomposition (SVD)
Doc1 and doc2 similar 🡪 `computers’�Doc3 and doc4 similar 🡪 `automotive’
(the term ‘the’ is not really related to any topic)
SVD Example
SVD decomposes a matrix A into the product of three specially formed matrices (mysteriously named) U, S and V
S – describes the relative strength of features
U – relationship between terms and features
Vt – relationship between features and documents
Dimensionality Reduction
{A}={U}{S}{V}T
{~A}~={~U}{~S}{~V}T
(Pre-trained) dense word embeddings
Word2Vec and Word embeddings
Word2Vec: Model I
Can you compute Softmax?
Can you compute Softmax?
Word2Vec: Model II
“ … who passes the … “
Skip-Gram Training Data
43
9/13/23
Asssume context words are those in +/- 2 word window
Basic idea behind skip-gram embeddings
from an input word w(t) in a document
construct hidden layer that “encodes” that word
So that the hidden layer will predict likely nearby words w(t-K), …, w(t+K)
final step of this prediction is a softmax over lots of outputs
Basic idea behind skip-gram embeddings
Training data:
positive examples are pairs of words w(t), w(t+j) that co-occur
Training data:
negative examples are samples of pairs of words w(t), w(t+j) that don’t co-occur
You want to train over a very large corpus (100M words+) and hundreds+ dimensions
Skip-Gram Training
46
9/13/23
Skip-Gram Training
47
9/13/23
k=2
Negative sampling
increase the probability for less frequent words and decrease the probability for more frequent words.
Learning the classifier
Evaluating embeddings
Ask humans how similar two 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)
Words as vectors of context words
Results from word2vec
https://www.tensorflow.org/versions/r0.7/tutorials/word2vec/index.html
Analogy: Embeddings capture relational meaning!
56
Word2Vec vs. �Distributional Semantic Models
Question
Conclusion