1 of 86

10-405/605/805�ML FROM LARGE DATASETS

2 of 86

Announcements

3 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

4 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

5 of 86

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

6 of 86

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 of 86

7

MF for image modeling

Sample of images

“Clusters” (principal components)

Reconstructions given M clusters

8 of 86

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

9 of 86

Recap: k-means Clustering

9

centroids

10 of 86

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

  • k-means constrains each reconstruction to be equal to some cluster

​

  • SGD allows values to be reconstructed from as weighted sums of clusters

11 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

12 of 86

BLOOM FILTER / COUNTMIN SKETCH REVIEW

13 of 86

Bloom Filters and Count-min sketches

  • BF approximates set S and maps x to bool(x in S)
  • CM approximates a vector a and maps x to a[x] >= 0
  • you can only change S or a[x] by adding/incrementing to it

v ≥ 0

14 of 86

Bloom filters: the clean version

15 of 86

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:

16 of 86

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 }

17 of 86

Bloom filters: a slightly different version

BF space is O(k lg 1/δ) bits

18 of 86

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

19 of 86

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

20 of 86

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

  • If M.retrieve(x) >= a[x]
  • Pr(M.retrieve(x) > a[x] ) < 𝛿
  • If x is not in X then Pr(contains(S1:t(X), x) > 0) < 𝛿

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

  • If M.retrieve(x) >= a[x]
  • Pr(M.retrieve(x) > a[x] + εT ) < 𝛿

Count-Min Sketches:

see my notes

space: O((1/ ε) * log(1/𝛿))

space: O(k log(1/𝛿))

21 of 86

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

  • If M.retrieve(x) >= a[x]
  • Pr(M.retrieve(x) > a[x] ) < 𝛿

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

  • If M.retrieve(x) >= a[x]
  • Pr(M.retrieve(x) > a[x] + εT ) < 𝛿

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

22 of 86

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

23 of 86

So you can combine BF’s or CM sketches linearly

24 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

25 of 86

LOCALITY SENSITIVE HASHING (LSH)

26 of 86

LSH: key ideas

  • Goal:
    • map feature vector x to bit vector bx
    • ensure that bx preserves “similarity”

​

27 of 86

Random Projections

28 of 86

Random projections

+

+

+

+

+

+

+

+

+

-

-

-

-

-

-

-

-

-

29 of 86

Random projections are like Random Classifiers

w

-w

+

+

+

+

+

+

+

+

+

-

-

-

-

-

-

-

-

-

30 of 86

Random projections are like Random Classifiers

w

-w

+

+

+

+

+

+

+

+

+

-

-

-

-

-

-

-

-

-

 

+

 

 

31 of 86

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?

 

 

32 of 86

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.

​

33 of 86

LSH: key ideas

  • Goal:
    • map feature vector x to bit vector bx
    • ensure that bx preserves “similarity”
  • Basic idea: use random projections of x
    • Repeat many times:
      • Pick a random hyperplane r by picking random weights for each feature (say from a Gaussian)
      • Compute the inner product of r with x
      • Record if x is “close to” r (r.x>=0)
        • the next bit in bx
      • Theory says that is x’ and x have small cosine distance then bx and bx’ will have small Hamming distance

​

34 of 86

[Slides: Ben van Durme]

35 of 86

[Slides: Ben van Durme]

36 of 86

[Slides: Ben van Durme]

37 of 86

[Slides: Ben van Durme]

38 of 86

[Slides: Ben van Durme]

Probability that a random projection does not “split” two points is exactly their cosine similarity

39 of 86

[Slides: Ben van Durme]

40 of 86

LSH applications

  • Compact storage of data with bit vectors
    • and we can still compute similarities
  • LSH also gives very fast approximations:
    • approximate nearest neighbor method
      • just check other items with bx’=bx
      • there are also fast nearest-neighbor methods for Hamming distance
    • fast approximate clustering
      • cluster = all things with same bx vector

​

41 of 86

MINHASH VS SIMHASH

42 of 86

MinHash and Jaccard similarity

  • A useful similarity measure for sets:

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

​

43 of 86

MinHash and Jaccard similarity

  • A useful similarity measure for sets:

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

44 of 86

MinHash and Jaccard similarity

  • A useful similarity measure for sets:

Jaccard(S, T) = |S ∩ T | / |S ∪ T|

  • There is some word w in (S ∪ T) that has the minimum hash
  • MinHash(S) = MinHash(T) iff w is in |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

45 of 86

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

46 of 86

LSH comments

  • Hash kernels, Bloom Filters, CountMin sketches:
    • how to stop worrying and live with hash collisions
  • LSH:
    • Collisions are actually useful

​

47 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

48 of 86

FROM SIGNATURES TO APPROXIMATE NEAREST NEIGHBORS

49 of 86

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

50 of 86

LSH and approximate k-NN

  • Goal: given q find all q’ with sim(q, q’) > s0
    • We have M hashes

ki(q) = min(hi(w) for w in q)

    • Can we use all q’ for which k1(q’)=k1(q) ?
      • Very noisy, because this is a weak signal
    • Can we use q’ for which k(q’)=k(q) ?
      • Pr(q’ is missed) = 1 - sim(q,q’)M
      • Probably too few candidate matches

​

51 of 86

LSH and approximate k-NN

  • Goal: given q find all q’ with sim(q, q’) > s0
    • We have M hashes

ki(q) = min(hi(w) for w in q)

    • Idea: group M=b*r hashes into b bands with r rows in each band
      • Take q’ as a candidate if matches q on an entire row in any band.
      • Pr(q’ is missed) = (1 – sim(q, q’)r )b

52 of 86

LSH and approximate k-NN

  • Goal: given q find all q’ with sim(q, q’) > s0
    • We have M hashes

ki(q) = min(hi(w) for w in q)

    • Take q’ as a candidate if matches q on an entire row in any band.
    • Pr(q’ is missed) = (1 – sim(q, q’)r )b

53 of 86

LSH and approximate k-NN

Summary:

  • build signature for each x in the collection
  • group signature into b “bands” each with r weak signals (MinHash ints, or SimHash bits)
  • tune b, r for your use case
  • build an inverted index for each group of r signals

​

​

​

At query time: given q

  • recompute its signature
  • collect candidate q’ by following the b inverted indices
  • optionally rescore the q’ s

​

Multi-index hashing

54 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

55 of 86

Online LSH and Pooling

55

56 of 86

56

57 of 86

LSH algorithm

  • Naïve algorithm:
    • Initialization:
      • For i=1 to outputBits:
        • For each feature f:
          • Draw r(f,i) ~ Normal(0,1)
    • Given an instance x
      • For i=1 to outputBits:

LSH[i] =

sum(x[f ]*r[i,f ] for f with non-zero weight in x) > 0 ? 1 : 0

      • Return the bit-vector LSH

​

​

​

57

58 of 86

LSH algorithm

  • But: storing the k classifiers is expensive in high dimensions
    • For each of 256 bits, a dense vector of weights for every feature in the vocabulary
  • Storing seeds and random number generators:
    • Possible but somewhat fragile

​

58

59 of 86

LSH: “pooling” (van Durme)

  • Better algorithm:
    • Initialization:
      • Create a pool:
        • Pick a random seed s
        • For i=1 to poolSize:
          • Draw pool[i] ~ Normal(0,1)
      • For i=1 to outputBits:
        • Devise a random hash function hash(i,f):
    • Given an instance x
      • For i=1 to outputBits:

LSH[i] = sum(

x[f] * pool[hash(i, f ) % poolSize] for f in x) > 0 ?

1 : 0

      • Return the bit-vector LSH

​

59

60 of 86

60

61 of 86

LSH: key ideas: pooling

  • Advantages:
    • with pooling, this is a compact re-encoding of the data
      • you don’t need to store the r’s, just the pool

​

​

61

62 of 86

Locality Sensitive Hashing (LSH) in an On-line Setting

62

63 of 86

LSH: key ideas: online computation

  • Common task: distributional clustering
    • for a word w, v(w) is sparse vector of words that co-occur with w
    • cluster the v(w)’s

​

​

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

​

64 of 86

LSH: key ideas: online computation

  • Common task: distributional clustering
    • for a word w, v(w) is sparse vector of words that co-occur with w
    • cluster the v(w)’s – or here, compute similarities

​

​

64

is similar to:

is similar to:

is similar to:

65 of 86

LSH: key ideas: online computation

  • Common task: distributional clustering
    • for a word w, v(w) is sparse vector of words that co-occur with w
    • cluster the v(w)’s

​

​

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.

66 of 86

LSH: key ideas: online computation

  • Construct LSH(v(w)) for each word w in the corpus
    • stream through the corpus once
    • use minimal amount of memory
    • ideally no more than size of output

​

​

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 of 86

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 of 86

68

j-th hash of fi

weight of fi in rj

69 of 86

Experiment

  • Corpus: 700M+ tokens, 1.1M distinct bigrams
  • For each, build a feature vector of words that co-occur near it, using on-line LSH
  • Check results with 50,000 actual vectors

69

70 of 86

Experiment

70

71 of 86

71

16

32

64

128

256

72 of 86

72

16

32

64

128

256

Closest based on cheap 64-bit vector

73 of 86

Points to review

  • APIs for:
    • Bloom filters, CM sketch, LSH
  • Key applications of:
    • Very compact noisy sets
    • Efficient counters accurate for large counts
    • Fast approximate cosine distance
  • Key ideas:
    • Uses of hashing that allow collisions
    • Random projection
    • Multiple hashes to control Pr(collision)
    • Pooling to compress a lot of random draws

73

74 of 86

Outline

  • Review
    • Matrix factorization and k-means
    • Bloom Filters and Countmin Sketches
  • Locality Sensitive Hashing
    • Main idea
    • For approximate k-NN searches
    • Online LSH
    • Applications (and alternatives for k-NN)

75 of 86

LSH: APPLICATIONS AND ALTERNATIVES

76 of 86

LSH: key ideas

  • LSH can be used to compress a set of vectors
    • while preserving some information about their similarity
  • LSH can also be used to index a set of vectors
    • k-NN search maps a query vector qx to the k most similar vectors x1,…,xk in a collection

16

32

64

128

256

77 of 86

LSH: key ideas

  • LSH can be used to compress a set of vectors
    • while preserving some information about their similarity
  • LSH can also be used to index a set of vectors
    • approximate k-NN search maps a query vector qx to an approximation of the k most similar vectors x1,…,xk in a collection

16

32

64

128

256

78 of 86

LSH for k-NN search

  • LSH can be used to compress a set of vectors
    • while preserving some information about their similarity
  • LSH can also be used to index a set of vectors
    • approximate k-NN search maps a query vector qx to an approximation of the k most similar vectors x1,…,xk in a collection
    • vectors in the same LSH bin as qx will be similar
    • more generally: vectors with low Hamming distance with be similar

An inverted index mapping LSH bitstrings b to vectors that map to b is a useful tool for fast k-NN algorithms

79 of 86

LSH for k-NN search

  • Subproblem: given a bit vector q find the closest bit vector, h, in a collection
  • We could look at all buckets with Hamming distance 0, then 1, then 2, … but there are 2k buckets of distance k
  • Multi-index hashing is better approach
    • start out with a long code h
    • break it into m equal-size bitstrings h(1), …, h(m)
    • claim: if Hamming(h,g) < r, then for some z, Hamming(h(z),g (z)) < r/m

​

  • Example if m=4:
    • h and g differ in 3 bits 🡺 one of the four sub-bitstrings has Hamming(h(z),g (z)) <= 1
    • h and g differ in 7 bits 🡺 one of the four sub-bitstrings has Hamming(h(z),g (z)) <= 1

​

​

​

​

​

​

80 of 86

LSH for k-NN search

  • Multi-index hashing is better approach
    • start out with a long code h – say n bits
    • break it into m equal-size bitstrings h(1), …, h(m)
    • claim: if Hamming(h,g) < r, then for some z, Hamming(h(z),g (z)) < r/m

​

  • Example if m=4:
    • h and g differ in 3 bits 🡺 one of the four sub-bitstrings has Hamming(h(z),g (z)) <= 1
    • h and g differ in 7 bits 🡺 one of the four sub-bitstrings has Hamming(h(z),g (z)) <= 1

​

​

​

​

​

​

  • Old: look at buckets with Hamming distance 0 to g, then 1, … 🡺 2n
  • New: look at subbuckets Hamming distance 0 to g, then 1 … 🡺 m*2n/m

​

  • Fast in practice!

81 of 86

k-means for k-NN search

82 of 86

k-means Clustering

82

centroids

83 of 86

J-means for k-NN search

  • Does J-means help you find points close to qx?
  • After you have learned the j clusters of n objects
    • Find centroid i closest to qx
      • O(J) distance computations
    • Find element x in cluster i closest to qx
      • O(N/J) distance computations
  • If your distance satisfies the triangle inequality then this has bounded error
      • if J=sqrt(N): O(J) + O(N/J) beats O(N)

​

  • If this isn’t enough you can try running J-means internally in each cluster and so on
    • Build a tree of depth d
      • O(d J ) * O(N / Jd)

​

84 of 86

Vector quantization

  • We can use the same data structure to either
    • find approximate k-NN’s to qx
    • map qx to a centroid that is similar
      • centroid is an approximate encoding of qx’s
  • With a hierarchical clustering and millions of centroids this will be a good approximation of qx
    • but it is compact to describe (as a path through the J-means tree)
    • so k-means also compresses a set of vectors, like LSH

​

85 of 86

Product quantization for k-NN search

  • Another trick:
    • split x into m equal-size vectors x(1), …, x(m)
      • just like we did with bitstrings for multi-index hashing
    • build a J-means indices for each of the m sets of subvectors
    • to “encode” qx find closest centroids to qx(1), …, qx(m)
      • call them c(1), …, c(m)
      • call the concatenation c
    • there are Jm possible c’s
  • The larger space of encodings means PQ can be very accurate
  • Theory says that the encoding loss is lower for qx,c pairs that are highly similar
    • there are the important ones

​

​

​

​

​

86 of 86

A Recent(ish) Survey

2024