1 of 79

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

2 of 79

Announcements

  • Upcoming special events
    • Recitation Fri 9/18:
      • Probability and Distributed PyTorch
    • No class Mon 9/21 (Yom Kippur)
    • Guest lecture 9/28
      • Jay Yoon Lee, Seoul National U

3 of 79

Outline

  • SGD for Matrix Factorization
    • A non-convex optimization
  • Randomized algorithms
    • Bloom Filters
    • Count-Min Sketches

4 of 79

Matrix Factorization

4

5 of 79

Recovering 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 79

Recovering latent factors in a matrix as optimization

6

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

r

W

H

V

7 of 79

Matrix factorization as SGD

7

step size

why does this work?

8 of 79

Matrix factorization as SGD - why does this work? Here’s the key claim:

8

like linear least-squares regression

  • LR loss = compare y and ŷ = dot(w,x)
  • MF loss = compare Vij and dot(Wi*, H*j)

9 of 79

Recommendation losses differentiated

9

~ least squares regression

10 of 79

Recommendation losses differentiated

10

~ ridge regression

11 of 79

Other recommendation losses

11

N1, N2 - diagonal matrixes, sort of like IDF factors for the users/movies

“generalized” KL-divergence

12 of 79

12

ALS = alternating least squares

(special MF optimization method)

Netflix challenge dataset: 100M ratings from ~ 480k customers on ~ 18k movies

13 of 79

13

talk pilfered from 🡪 …..

KDD 2011

14 of 79

14

8 machines, 64 cores

15 of 79

Recap: minimal-mixing papers did get published!

Zinkevich et al, 2010

  • Baseline: one-processor SGD
  • Workers: learn P models from overlapping samples of full dataset (with SGD)
  • Mix parameters once
    • i.e average values
    • same as MSGD
  • Result: wall-clock speedup for same loss when you go from P=1 🡪 P=10 🡪P=100

​

​

16 of 79

Recovering latent factors in a matrix

16

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

17 of 79

Recovering latent factors in a matrix

17

m movies

n users

m movies

y1

x1

y2

x2

..

..

​

​

​

​

​

​

​

​

​

​

…

…

yn

xn

b1

b2

..

…

bm

a1

a2

…

…

am

v11

…

​

​

​

…

…

​

​

​

​

​

​

​

​

​

vij

​

​

​

​

​

​

…

​

​

​

​

​

vnm

~

V[i,j] = user i’s rating of movie j

18 of 79

18

Averaging different clusters!

19 of 79

19

Converging but slowly…

20 of 79

20

21 of 79

21

22 of 79

22

23 of 79

23

24 of 79

24

25 of 79

25

DSGD

ALS

iterative mixing

LBFGS

one mixing round

26 of 79

Wall Clock Time

26

DSGD

ALS

iterative mixing

LBFGS

one mixing round

27 of 79

Outline

  • SGD for Matrix Factorization
    • A non-convex optimization
      • so iterative parameter mixing matters
  • Randomized algorithms
    • Bloom Filters
    • Count-Min Sketches

28 of 79

Outline

  • SGD for Matrix Factorization
    • A non-convex optimization
      • so iterative parameter mixing matters
  • Randomized algorithms
    • Bloom Filters
    • Count-Min Sketches

29 of 79

Recap: SGD for Logistic Regression

30 of 79

Recap: The “hash trick”

an array V = <0…0>

of fixed length M

h(xi)= j

Assume a hash function h(token) = hash(token) % M

V[j] = V[j]

array V

⍺

31 of 79

Recap: Hash Trick - Insights

  • Save memory:
    • don’t store hash keys (strings)
    • ignore collisions
      • even though it distorts your data some
    • Use fixed amount of memory
      • not data-dependent
  • Let the learner (downstream) take up the slack

​

​

32 of 79

Recap: Hash Kernels

  • Observation: in NLP (especially with substrings and terms#class as features) there are often highly-correlated features “duplicates”
    • Or you can create duplicates explicitly by hashing “William#1”, “William#2”, “William#3”, etc.
  • Q: How much loss of information is there if you assume duplicates exist?
  • A: very little! because

Pr(all duplicates of any feature collide) gets low

​

Bloom filters also exploit this idea

​

33 of 79

BLOOM FILTERS

34 of 79

Bloom filters

  • A noisy “set” where you can test membership (and that’s it)

​

  • class BloomFilter:

def __init__ (self, maxSize:int, p: double):

# start with empty set

def bf.add(String s):

# insert string s into the set

def bf.contains(String s) 🡪 bool:

# if s was added return true;

# else with probability at least 1-p return false;

# else with probability at most p return true;

​

35 of 79

Bloom filters

  • An implementation
    • Allocate M bits, bit[0]…,bit[1-M]
    • Pick K hash functions hash(1,2),hash(2,s),….
      • E.g: hash(i,s) = hash(s+ randomString[i])
    • To add string s:
      • For i=1 to k, set bit[hash(i,s)] = 1
    • To check contains(s):
      • For i=1 to k, test bit[hash(i,s)]
      • Return “true” if they’re all set; otherwise, return “false”
    • We’ll discuss how to set M and K soon, but for now:
      • Let M = 1.5*maxSize // less than two bits per item!
      • Let K = 2*log(1/p) // about right with this M

​

36 of 79

Bloom filters

0

0

0

0

0

0

0

0

0

0

bf.add(“fred flintstone”):

0

1

1

0

0

0

0

1

0

0

h1

h2

h3

bf.add(“barney rubble”):

1

1

1

0

0

1

0

1

0

0

h1

h2

h3

37 of 79

Bloom filters

1

1

1

0

0

1

0

1

0

0

bf.contains (“fred flintstone”):

1

1

1

0

0

1

0

1

0

0

h1

h2

bf.contains(“barney rubble”):

1

1

1

0

0

1

0

1

0

0

h1

h2

h3

✅

✅

38 of 79

Bloom filters

bf.contains(“wilma flintstone”):

1

1

1

0

0

1

0

1

0

0

h1

h2

h3

1

1

1

0

0

1

0

1

0

0

bf.contains(“wilma flintstone”):

1

1

1

0

0

1

0

1

0

0

h1

h2

h3

✅

❌

39 of 79

Bloom filters: analysis

  • Analysis (m bits, k hashers):
    • Assume hash(i,s) is a random function
    • Look at Pr(bit j is unset after n add’s):

​

    • … and Pr(collision) = Pr(all k bits set)

​

​

​

​

    • …. fix m and n and minimize f(m,n,k) for k:

​

​

​

k =

f(m,n,k) =

40 of 79

Bloom filters

  • Analysis:
    • Plug optimal k=m/n*ln(2) back into Pr(collision):

​

​

​

    • Now we can fix any two of p, n, m and solve for the 3rd:

E.g., the value for m in terms of n and p:

​

​

​

f(m,n) =

41 of 79

Bloom filters

  • A noisy “set” where you can test membership (and that’s it)

​

  • class BloomFilter:

def __init__ (self, maxSize:int, n:int, p: double):

# start with empty set

def bf.add(String s):

# insert string s into the set

def bf.contains(String s) 🡪 bool:

# if s was added return true;

# else with probability at least 1-p return false;

# else with probability at most p return true;

​

42 of 79

Bloom filters: demo

43 of 79

Bloom filters: a slightly different version

Building one sketch with one hash function:

Building t sketches with t hash functions:

=

each

Test membership with AND of all the sketches:

44 of 79

Bloom filters: a slightly different version

45 of 79

Bloom filters: a slightly different version

(1) Assume for all a, i, x:

(2) Assume for all a, b, i, j, x, x’:

up to k elements of set of size V

Probability over random choice of hi

…then repeatedly apply (2)

up to k elements of set of size V

< 𝛿

46 of 79

Bloom filters: a slightly different version

47 of 79

What do you do with a Bloom filter?

48 of 79

Some uses of Bloom filters

  • An example application
    • Finding items in “sharded” data
      • Easy if you know the sharding rule
      • Harder if you don’t (like Google n-grams)

49 of 79

Some Uses of Bloom filters

  • An example application
    • Finding items in “sharded” data
      • Easy if you know the sharding rule
      • Harder if you don’t (like Google n-grams)
  • Simple idea:
    • Build a BF of the contents of each shard
    • To look for key, load in the BF’s one by one, and search only the shards that probably contain key
    • Analysis: you won’t miss anything, you might look in some extra shards
    • You’ll hit O(1) extra shards if you set p=1/#shards

50 of 79

Some Uses of Bloom filters

  • An example application
    • discarding singleton features from a classifier
  • Scan through data once and check each w:
    • if bf1.contains(w): bf2.add(w)
    • else bf1.add(w)
  • Now:
    • bf1.contains(w) ⬄ w appears >= once
    • bf2.contains(w) ⬄ w appears >= 2x
  • Then train, ignoring words not in bf2

51 of 79

Some Uses of Bloom filters

  • An example application
    • discarding rare features from a classifier
    • seldom hurts much, can speed up experiments
  • Scan through data once and check each w:
    • if bf1.contains(w):
      • if bf2.contains(w): bf3.add(w)
      • else bf2.add(w)
    • else bf1.add(w)
  • Now:
    • bf2.contains(w) ⬄ w appears >= 2x
    • bf3.contains(w) ⬄ w appears >= 3x
  • Then train, ignoring words not in bf3

52 of 79

Outline

  • SGD for Matrix Factorization
    • A non-convex optimization
      • so iterative parameter mixing matters
  • Randomized algorithms
    • Bloom Filters
    • Count-Min Sketches

53 of 79

THE COUNT-MIN SKETCH

Bloom Filter

Hash Kernel

Count-Min Sketch

​

​

​

​

​

54 of 79

Bloom Filter🡪Count-min sketch

CM sketch maps x to a number a[x]

But you can only update a[x] by adding to it

Important:

v ≥ 0

55 of 79

Bloom Filter🡪Count-min sketch

56 of 79

Bloom Filter🡪Count-min sketch

  • Implementation
    • Allocate a matrix CM with d rows, w columns
    • Pick d hash functions h1(s),h2(s),….
    • To increment counter A[s] for s by c
      • For i=1 to d, set CM[i, hash(i,s)] += c
    • To retrieve value of A[s]:
      • For i=1 to d, retrieve M[i, hash(i,s)]
      • Return minimum of these values
    • Similar idea s Bloom filter:
      • if there are d collisions, you return a value that’s too large; otherwise, you return the correct value.

57 of 79

Count-min sketch results

  • Suppose a[x] is the value that x “should have” in the sketch: i.e., if the increment calls for x were with values v1, …, vJ then
  • By the same argument used for the Bloom filter:

58 of 79

Count-min sketch results

  • Suppose denotes the L1 norm:

​

​

​

​

​

​

 

59 of 79

CM Sketch Structure

  • Each string is mapped to one bucket per row
  • Estimate A[j] by taking mink { CM[k,hk(j)] }
  • Errors are always over-estimates
  • Analysis: d=log 1/𝛿, w=2/𝜺 🡺 error is usually less than 𝜺||A||1

​

A Quick Intro to Data Stream Algorithmics – CS262

+c

+c

+c

+c

h1(s)

hd(s)

<s, +c>

d=log 1/δ

w = 2/ε

from: Minos Garofalakis�

i.e. with prob > 1-𝛿

60 of 79

  • You can find the sum of two sketches by doing element-wise summation

A Quick Intro to Data Stream Algorithmics – CS262

c

c

c

c

from: Minos Garofalakis�

d

d

d

d

c+d

d

d

d

c

c

c

+

  • Same result as adding <s,+c> and then <t,+d> to an empty sketch

<s, +c>

<t, +d>

  • Also, you can compute a weighted sum of MC sketches

61 of 79

CM Sketch Guarantees

​

  • Assuming ||A||1 is small is often reasonable:
    • Estimating a multinomial: if A[s] = Pr(s|…) then ||A||1 = 1
    • Multiclass classification: if Ax[s] = Pr(x in class s) then ||Ax||1 is probably small, since most x’s will be in only a few classes

​

A Quick Intro to Data Stream Algorithmics – CS262

61

from: Minos Garofalakis�

62 of 79

CM Sketch Guarantees

  • [Cormode, Muthukrishnan ‘04] CM sketch guarantees approximation error on point queries less than 𝜺||A||1 in space O(1/𝜺 log 1/𝛿)
  • CM sketches are also accurate for skewed values---i.e., only a few entries s with large A[s]

A Quick Intro to Data Stream Algorithmics – CS262

from: Minos Garofalakis�

63 of 79

Applications of Count-Min Sketches 1

64 of 79

Background

2002

65 of 79

Background

2002 – 12,000+ citations

Dataset of movie reviews (pos, negative)

66 of 79

An Application of a Count-Min Sketch

  • Problem: find the semantic orientation of a review (positive or negative) using a large corpus.
  • Idea:
    • positive words co-occur more frequently than expected near other positive words; likewise for negative words
    • so pick a few pos/neg seeds and compute

x appears near y

67 of 79

An Application of a Count-Min Sketch

x appears near y

Turney, 2002 used two seeds, “excellent” and “poor”

In general, SO(w) can be written in terms of logs of products of counters for w, with and without seeds

68 of 79

An Application of a Count-Min Sketch

  • Use 2B counters, 5 hash functions, “near” means a 7-word window, GigaWord (10 Gb) and GigaWord + Web news 50 Gb)

69 of 79

Math for Count-Min Sketches

70 of 79

An Application of a Count-Min Sketch

CM-CU: CM with “conservative update” - for <j,+c> increment counters just enough to make the new estimate for j grow by c

71 of 79

Bloom filter / Count-min sketch

72 of 79

Bloom filters: the math

(1) Assume for all a, i, x:

(2) Assume for all a, b, i, j, x, x’:

up to k elements of set of size V

Probability over random choice of hi

…then repeatedly apply (2)

73 of 79

Bloom filters: the math

< 𝛿

74 of 79

Count-min sketch results

  • Suppose a[x] is the value that x “should have” in the sketch: i.e., if the increment calls for x were with values v1, …, vJ then
  • By the same argument used for the Bloom filter:

75 of 79

Count-min sketch results

  • Suppose a[x] is the value that x “should have” in the sketch: i.e., if the increment calls for x were with values v1, …, vJ then
  • By the similar argument used for the Bloom filter:

76 of 79

CM-Sketch: the math

…then repeatedly apply (2)

recap of BF math

Pr(excess in r[h(x)] due to collisions with other x’) > 𝜺 ||a||1) < 1/e

​

77 of 79

CM-Sketch: the math

Expectation is:

Pr(excess in r[h(x)] due to collisions with other x’) > 𝜺 ||a||1) < 1/e

Goal: show that for a particular hash function t and x

So the goal is to show:

= 1/e

Markov

inequality

78 of 79

CM-Sketch: the Markov Inequality

when X is a random variable that has non-negative real values

≥

c

79 of 79

Count-min sketch result: recap