10-405/605/805�ML FROM LARGE DATASETS
Announcements
Outline
Outline
Recap: latent factors in a matrix
5
m movies
n users
m movies
x1 | y1 |
x2 | y2 |
.. | .. |
| |
| |
| |
| |
| |
… | … |
xn | yn |
a1 | a2 | .. | … | am |
b1 | b2 | … | … | bm |
v11 | … | | | |
… | … | | | |
| | | | |
| vij | | | |
| | | … | |
| | | | vnm |
~
V[i,j] = user i’s rating of movie j
MF for images
10,000 pixels
1000 images
1000 * 10,000,00
x1 | y1 |
x2 | y2 |
.. | .. |
| |
| |
| |
| |
| |
… | … |
xn | yn |
a1 | a2 | .. | … | am |
b1 | b2 | … | … | bm |
v11 | … | | … | … |
… | … | | | |
| | | | |
| vij | | | |
| | | … | |
| | | | vnm |
~
V[i,j] = pixel j in image i
2 prototypes
PC1
PC2
from Bishop’s textbook, “Pattern Recognition and ML”, 2006
7
MF for image modeling
Sample of images
“Clusters” (principal components)
Reconstructions given M clusters
Recovering latent factors in a corpus
8
m terms
n documents
doc term matrix
x1 | y1 |
x2 | y2 |
.. | .. |
| |
| |
| |
| |
| |
… | … |
xn | yn |
a1 | a2 | .. | … | am |
b1 | b2 | … | … | bm |
v11 | … | | | |
… | … | | | |
| | | | |
| vij | | | |
| | | … | |
| | | | vnm |
~
V[i,j] = TFIDF score of term j in doc i
Recap: k-means Clustering
9
centroids
k-means as MF
cluster means
n examples
0 | 1 |
1 | 0 |
.. | .. |
| |
| |
| |
| |
| |
… | … |
xn | yn |
a1 | a2 | .. | … | am |
b1 | b2 | … | … | bm |
v11 | … | | | |
… | … | | | |
| | | | |
| vij | | | |
| | | … | |
| | | | vnm |
~
original data set
indicators for r clusters
Z
M
X
Outline
BLOOM FILTER / COUNTMIN SKETCH REVIEW
Bloom Filters and Count-min sketches
v ≥ 0
Bloom filters: the clean version
Bloom filters: the clean version
Building one sketch with one hash function:
Building t sketches with t hash functions:
=
each
Test membership with AND of all the sketches:
Bloom filters: the clean version
(1) Assume for all a, i, x:
(2) Assume for all a, b, i, j, x, x’:
Probability over random choice of hi
… then repeat to get e-t
= up to k elements of {1…V }
< 𝛿
up to k elements of {1…V }
Bloom filters: a slightly different version
BF space is O(k lg 1/δ) bits
Bloom Filters and Count-min sketches�
a[‘fred flintstone’] += 2
0 | 2 | 0 | 2 | 0 | 0 | 0 | 2 | 0 |
a[‘barney rubble’] += 1
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
a[‘fred flintstone’] = min(2,3,2) = 2
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
Bloom Filters and CM sketches�
a[‘fred flintstone’] += 2
0 | 2 | 0 | 2 | 0 | 0 | 0 | 2 | 0 |
a[‘barney rubble’] += 1
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
a[‘fred flintstone’] = min(2,3,2) = 2
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
1 | 2 | 0 |
3 | 0 | 0 |
1 | 2 | 0 |
d=log 1/δ
w = 2/ε
By tradition* the CM sketch written as a matrix
* since Graham Cormode and S. Muthu Muthukrishnan
BF / CM Results
Bloom Filter:
Corollary 2 (CM sketches). Let M be a sketch that has been incremented < k times, let a[x] be the correct value for x, let w > ek , and let d > ln (1/𝛿). Then
Theorem (CM sketches). Let M be a sketch where total increments for all x < T, let a[x] be the correct value for x, let w > 2/ε , and let d > ln (1/𝛿). Then
Count-Min Sketches:
see my notes
space: O((1/ ε) * log(1/𝛿))
space: O(k log(1/𝛿))
BF / CM Results
Corollary 2 (CM sketches). Let M be a sketch that has been incremented < k times, let a[x] be the correct value for x, let w > ek , and let d > ln (1/𝛿). Then
Theorem (CM sketches). Let M be a sketch where total increments for all x < T, let a[x] be the correct value for x, let w > 2/ε , and let d > ln (1/𝛿). Then
Count-Min Sketches:
There’s a similar bound if a[ ] has at most k “large” values and the sum of the “tail” values is bounded by T.
Need: w > max( 1 / 3k , e/ε )
Bound: Pr(M.retrieve(x) > a[x] + εT ) < 𝛿
This holds for Zipf style skewed distributions
Order of BF or CM additions doesn’t matter…
a[‘fred flintstone’] += 2
0 | 2 | 0 | 2 | 0 | 0 | 0 | 2 | 0 |
a[‘barney rubble’] += 1
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
a[‘fred flintstone’] = min(2,3,2) = 2
1 | 2 | 0 | 3 | 0 | 0 | 1 | 2 | 0 |
So you can combine BF’s or CM sketches linearly
Outline
LOCALITY SENSITIVE HASHING (LSH)
LSH: key ideas
Random Projections
Random projections
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
Random projections are like Random Classifiers
w
-w
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
Random projections are like Random Classifiers
w
-w
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
Random projections ~ preserve distance
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
The dot products will be “close” if w is nearly orthogonal to the line between x1, x2
What if we use a random weight vector to classify two distant points?
Random projections
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
Any other direction will keep the distant points distant.
So if I pick a random r and r.x and r.x’ are closer then probably x and x’ were close to start with.
LSH: key ideas
[Slides: Ben van Durme]
[Slides: Ben van Durme]
[Slides: Ben van Durme]
[Slides: Ben van Durme]
[Slides: Ben van Durme]
Probability that a random projection does not “split” two points is exactly their cosine similarity
[Slides: Ben van Durme]
LSH applications
MINHASH VS SIMHASH
MinHash and Jaccard similarity
Jaccard(S, T) = |S ∩ T | / |S ∪ T|
S = {william, w, cohen, carnegie, mellon, univ}
T = {william, cohen, carnegie, mellon}
S ∩ T = {william, cohen, carnegie, mellon}
S ∪ T = {william, w, cohen, carnegie, mellon, univ}
Jaccard(S, T) = 4/6 = 0.667
MinHash and Jaccard similarity
Jaccard(S, T) = |S ∩ T | / |S ∪ T|
SimHash(x) = sign(r.dot(x)) where r is random projection
MinHash(S) = min(h(w) for w in S) where h is random hash
Claim: Pr(MinHash(S) = MinHash(T)) = Jaccard(S, T)
where probability is taken over random choice of h
Pr[sign(r.dot(x’)) = sign(r.dot(x)] = cos(θ(x,x’)) where prob is over r
MinHash and Jaccard similarity
Jaccard(S, T) = |S ∩ T | / |S ∪ T|
MinHash(S) = min(h(w) for w in S) where h is random hash
Claim: Pr(MinHash(S) = MinHash(T)) = Jaccard(S, T)
where probability is taken over random choice of h
MinHash and Jaccard similarity
A minhash signature for S is vector kS=k1,…,kM
ki= min(hi(w) for w in S)
It approximates Jaccard similarity, like SimHash vectors approximate cosine similarity
LSH comments
Outline
FROM SIGNATURES TO APPROXIMATE NEAREST NEIGHBORS
49
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
bf.contains(“fred flintstore”):
h1
h2
h3
bf.contains(“pebbles”):
1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
h1
h2
h3
return AND of all hashed bits
1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
Key idea: combine three weak signals to get a stronger one
LSH and approximate k-NN
ki(q) = min(hi(w) for w in q)
LSH and approximate k-NN
ki(q) = min(hi(w) for w in q)
LSH and approximate k-NN
ki(q) = min(hi(w) for w in q)
LSH and approximate k-NN
Summary:
At query time: given q
Multi-index hashing
Outline
Online LSH and Pooling
55
56
LSH algorithm
LSH[i] =
sum(x[f ]*r[i,f ] for f with non-zero weight in x) > 0 ? 1 : 0
57
LSH algorithm
58
LSH: “pooling” (van Durme)
LSH[i] = sum(
x[f] * pool[hash(i, f ) % poolSize] for f in x) > 0 ?
1 : 0
59
60
LSH: key ideas: pooling
61
Locality Sensitive Hashing (LSH) in an On-line Setting
62
LSH: key ideas: online computation
63
…guards at Pentonville prison in North London discovered that an escape attempt…
An American Werewolf in London is to be remade by the son of the original director…
…UK pop up shop on Monmouth Street in London today and on Friday the brand…
v(London): Pentonville, prison, in, North, …. and, on Friday
LSH: key ideas: online computation
64
is similar to:
is similar to:
is similar to:
LSH: key ideas: online computation
65
v(w) is very similar to a word embedding (eg, from word2vec or GloVE)
Levy, Omer, Yoav Goldberg, and Ido Dagan. "Improving distributional similarity with lessons learned from word embeddings." Transactions of the Association for Computational Linguistics 3 (2015): 211-225.
LSH: key ideas: online computation
66
…guards at Pentonville prison in North London discovered that an escape attempt…
An American Werewolf in London is to be remade by the son of the original director…
…UK pop up shop on Monmouth Street in London today and on Friday the brand…
v(London): Pentonville, prison, in, North, …. and, on Friday
LSH(v(London)): 0101101010111011101
LSH(v(Monmouth)): 0111101000110010111
…
67
v is context vector; d is vocab size
ri is i-th random projection
hi(v) i-th bit of LSH encoding
because context vector is sum of mention contexts
these come one by one as we stream thru the corpus
keep this
68
j-th hash of fi
weight of fi in rj
Experiment
69
Experiment
70
71
16
32
64
128
256
72
16
32
64
128
256
Closest based on cheap 64-bit vector
Points to review
73
Outline
LSH: APPLICATIONS AND ALTERNATIVES
LSH: key ideas
16
32
64
128
256
LSH: key ideas
16
32
64
128
256
LSH for k-NN search
An inverted index mapping LSH bitstrings b to vectors that map to b is a useful tool for fast k-NN algorithms
LSH for k-NN search
LSH for k-NN search
k-means for k-NN search
k-means Clustering
82
centroids
J-means for k-NN search
Vector quantization
Product quantization for k-NN search
A Recent(ish) Survey
2024