56
Piazza Recitation session: ¡ Review of linear algebra § Location: Thursday, April 11, from 3:30-5:20 pm in SIG 134 (here) Deadlines next Thu, 11:59 PM: ¡ HW0, HW1 How to find teammates for project? ¡ Piazza Team Search ¡ Make sure you have a good dataset accessible 4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 1

Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

Piazza

Recitation session:

¡ Review of linear algebra

§ Location: Thursday, April 11, from 3:30-5:20 pm in SIG 134 (here)

Deadlines next Thu, 11:59 PM:

¡ HW0, HW1

How to find teammates for project?¡ Piazza Team Search

¡ Make sure you have a good dataset accessible

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 1

Page 2: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

CS246: Mining Massive DatasetsJure Leskovec, Stanford University

http://cs246.stanford.edu

Page 3: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Task: Given a large number (N in the millions or billions) of documents, find “near duplicates”

¡ Problem:§ Too many documents to compare all pairs

¡ Solution: Hash documents so that similar documents hash into the same bucket§ Documents in the same bucket are then

candidate pairs whose similarity is then evaluated

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 3

Page 4: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 4

ShinglingDocu-ment

The setof stringsof length kthat appearin the doc-ument

Min-Hash-ing

Signatures:short integervectors thatrepresent thesets, andreflect theirsimilarity

Locality-sensitiveHashing

Candidatepairs:those pairsof signaturesthat we needto test forsimilarity

Page 5: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ A k-shingle (or k-gram) is a sequence of k tokens that appears in the document§ Example: k=2; D1 = abcab

Set of 2-shingles: C1 = S(D1) = {ab, bc, ca}¡ Represent a doc by a set of hash values of its

k-shingles¡ A natural similarity measure is then the

Jaccard similarity:sim(D1, D2) = |C1ÇC2|/|C1ÈC2|§ Similarity of two documents is the Jaccard similarity of

their shingles4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 6

Page 6: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Min-Hashing: Convert large sets into short signatures,

while preserving similarity: Pr[h(C1) = h(C2)] = sim(D1, D2)

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 7

Similarities of columns andsignatures (approx.) match!

1-3 2-4 1-2 3-4Col/Col 0.75 0.75 0 0Sig/Sig 0.67 1.00 0 0

Signature matrix M

5

7

6

3

1

2

4

4

5

1

6

7

3

2

0101

0101

1010

1010

1010

1001

0101 Input matrix (Shingles x Documents)

3

4

7

2

6

1

5

Permutation p

1212

1412

2121

Page 7: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Hash columns of the signature matrix M:Similar columns likely hash to same bucket§ Divide matrix M into b bands of r rows (M=b·r)

§ Candidate column pairs are those that hash

to the same bucket for ≥ 1 band

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 8

r rows

b bands

Buckets

Matrix MSimilarity

Prob

. of s

harin

g≥

1 bu

cket

Thre

shol

d s

Page 8: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 9

PointsHash func.

Signatures: short integer signatures that reflect point similarity Locality-

sensitiveHashing

Candidate pairs:those pairs of signatures that we need to test for similarity

Design a locality sensitivehash function (for a given

distance metric)

Apply the“Bands” technique

Page 9: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ The S-curve is where the “magic” happens

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 10

Similarity t of two sets

Prob

abilit

y of

sha

ring

≥ 1

buck

et

Remember:Probability ofequal hash-values= similarity

This is what 1 hash-code gives youPr[hp(C1) = hp(C2)] = sim(D1, D2)

No chanceif t<s

Probability=1 if t>s

This is what we want!How to get a step-function?

By choosing r and b!

Thre

shol

d s

Similarity t of two sets

Page 10: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Remember: b bands, r rows/band¡ Let sim(C1 , C2) = sWhat’s the prob. that at least 1 band is equal?¡ Pick some band (r rows)§ Prob. that elements in a single row of

columns C1 and C2 are equal = s§ Prob. that all rows in a band are equal = sr

§ Prob. that some row in a band is not equal = 1 - sr

¡ Prob. that all bands are not equal = (1 - sr)b

¡ Prob. that at least 1 band is equal = 1 - (1 - sr)b

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 11

P(C1, C2 is a candidate pair) = 1 - (1 - sr)b

Page 11: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Picking r and b to get the best S-curve§ 50 hash-functions (r=5, b=10)

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 12

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Similarity, s

Prob

. sha

ring

a bu

cket

Page 12: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 13

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Similarity

r = 1..10, b = 1

Prob

(Can

dida

te p

air)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Prob

(Can

dida

te p

air)

r = 1, b = 1..10

r = 5, b = 1..50

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

r = 10, b = 1..50

Similarityprob = 1 - (1 - t r)b

Given a fixedthreshold s.

We want choose r and b such that the P(Candidate pair) has a “step” right around s.

Page 13: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

Min-Hash-ing

Signatures:short vectors that represent the sets, andreflect theirsimilarity

Locality-sensitiveHashing

Candidatepairs:those pairsof signaturesthat we needto test forsimilarity

Page 14: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ We have used LSH to find similar documents§ More generally, we found similar columns in large

sparse matrices with high Jaccard similarity

¡ Can we use LSH for other distance measures?§ e.g., Euclidean distances, Cosine distance § Let’s generalize what we’ve learned!

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 15

Page 15: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ d() is a distance measure if it is a function from pairs of points x,y to real numbers such that:§ ! ", $ ≥ 0§ ! ", $ = 0 ()) " = $§ !(", $) = !($, ")§ ! ", $ ≤ !(", -) + !(-, $) (triangle inequality)

¡ Jaccard distance for sets = 1 - Jaccard similarity¡ Cosine distance for vectors = angle between the vectors¡ Euclidean distances:

§ L2 norm: d(x,y) = square root of the sum of the squares of the differences between x and y in each dimension§ The most common notion of “distance”

§ L1 norm: sum of absolute value of the differences in each dimension§ Manhattan distance = distance if you travel along coordinates only

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 16

Page 16: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ For Min-Hashing signatures, we got a Min-Hash function for each permutation of rows

¡ A “hash function” is any function that allows us to say whether two elements are “equal”

§ Shorthand: h(x) = h(y) means “h says x and y are equal”

¡ A family of hash functions is any set of hash functions from which we can pick one at random efficiently§ Example: The set of Min-Hash functions generated

from permutations of rows4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 20

Page 17: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Suppose we have a space S of points with a distance measure d(x,y)

¡ A family H of hash functions is said to be (d1, d2, p1, p2)-sensitive if for any x and y in S:

1. If d(x, y) < d1, then the probability over all hÎ H, that h(x) = h(y) is at least p1

2. If d(x, y) > d2, then the probability over all hÎ H, that h(x) = h(y) is at most p2

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 21

With a LS Family we can do LSH!

Critical assumption

Page 18: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 22

Pr[h

(x) =

h(y

)]

Distance d(x,y)

d1 d2

p2

p1

Small distance,high probability

Large distance,low probabilityof hashing to the same value

Distancethreshold t

Notice it’s a distance, not similarity,hence the S-curve is flipped!

Page 19: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Let:§ S = space of all sets, § d = Jaccard distance, § H is family of Min-Hash functions for all

permutations of rows¡ Then for any hash function hÎ H:

Pr[h(x) = h(y)] = 1 - d(x, y)

§ Simply restates theorem about Min-Hashing in terms of distances rather than similarities

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 23

Page 20: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Claim: Min-hash H is a (1/3, 2/3, 2/3, 1/3)-sensitive family for S and d.

¡ For Jaccard similarity, Min-Hashing gives a (d1,d2,(1-d1),(1-d2))-sensitive family for any d1<d2

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 24

If distance < 1/3(so similarity ≥ 2/3)

Then probabilitythat Min-Hash valuesagree is > 2/3

Page 21: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Can we reproduce the “S-curve” effect we saw before for any LS family?

¡ The “bands” technique we learned for signature matrices carries over to this more general setting

¡ Can do LSH with any (d1, d2, p1, p2)-sensitivefamily!

¡ Two constructions:§ AND construction like “rows in a band”§ OR construction like “many bands”

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 25

Similarity t

Prob

. of s

harin

ga

buck

et

Page 22: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob
Page 23: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Given family H, construct family H’ consisting of r functions from H

¡ For h = [h1,…,hr] in H’, we sayh(x) = h(y) if and only if hi(x) = hi(y) for all i§ Note this corresponds to creating a band of size r

¡ Theorem: If H is (d1, d2, p1, p2)-sensitive, then H’ is (d1,d2, (p1)r, (p2)r)-sensitive

¡ Proof: Use the fact that hi ’s are independent

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 27

1 £ i £ r

Lowers probability forlarge distances (Good)

Also lowers probabilityfor small distances (Bad)

Page 24: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Independence of hash functions (HFs) really means that the prob. of two HFs saying “yes” is the product of each saying “yes”§ But two particular hash functions could be highly

correlated§ For example, in Min-Hash if their permutations agree in

the first one million entries

§ However, the probabilities in definition of a LSH-family are over all possible members of H, H’ (i.e., average case and not the worst case)

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 28

Page 25: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Given family H, construct family H’ consisting of b functions from H

¡ For h = [h1,…,hb] in H’, h(x) = h(y) if and only if hi(x) = hi(y) for at least 1 i

¡ Theorem: If H is (d1, d2, p1, p2)-sensitive, then H’ is (d1, d2, 1-(1-p1)b, 1-(1-p2)b)-sensitive

¡ Proof: Use the fact that hi’s are independent

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 29

Raises probability forsmall distances (Good)

Raises probability forlarge distances (Bad)

Page 26: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ AND makes all probs. shrink, but by choosing rcorrectly, we can make the lower prob. approach 0 while the higher does not

¡ OR makes all probs. grow, but by choosing b correctly, we can make the higher prob. approach 1 while the lower does not

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 30

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

ANDr=1..10, b=1

Pro

b. s

harin

g a

buck

et

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Pro

b. s

harin

g a

buck

et

ORr=1, b=1..10

Similarity of a pair of items Similarity of a pair of items

Page 27: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ By choosing b and r correctly, we can make the lower probability approach 0 while the higher approaches 1

¡ As for the signature matrix, we can use the AND construction followed by the OR construction§ Or vice-versa

§ Or any sequence of AND’s and OR’s alternating

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 31

Page 28: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ r-way AND followed by b-way OR construction§ Exactly what we did with Min-Hashing

§ AND: If bands match in all r values hash to same bucket§ OR: Cols that have ³ 1 common bucket à Candidate

¡ Take points x and y s.t. Pr[h(x) = h(y)] = s§ H will make (x,y) a candidate pair with prob. s

¡ Construction makes (x,y) a candidate pair with probability 1-(1-sr)b The S-Curve!§ Example: Take H and construct H’ by the AND

construction with r = 4. Then, from H’, construct H’’by the OR construction with b = 4

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 32

Page 29: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 33

s p=1-(1-s4)4.2 .0064.3 .0320.4 .0985.5 .2275.6 .4260.7 .6666.8 .8785.9 .9860

r = 4, b = 4 transforms a (.2,.8,.8,.2)-sensitive family into a (.2,.8,.8785,.0064)-sensitive family.

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1Similarity s

Prob

(can

dida

te p

air)

Page 30: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob
Page 31: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Picking r and b to get desired performance§ 50 hash-functions (r = 5, b = 10)

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 35

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1 Blue area X: False Negative rateThese are pairs with sim > s but the Xfraction won’t share a band and then will never become candidates. This means we will never consider these pairs for (slow/exact) similarity calculation!Green area Y: False Positive rateThese are pairs with sim < s butwe will consider them as candidates. This is not too bad, we will consider them for (slow/exact) similarity computation and discard them.

Similarity s

Prob

(Can

dida

te p

air)

Thre

shol

d s

Page 32: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Picking r and b to get desired performance§ 50 hash-functions (r * b = 50)

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 36

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

r=2, b=25r=5, b=10r=10, b=5

Thre

shol

d s

Similarity s

Prob

(Can

dida

te p

air)

Page 33: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Apply a b-way OR construction followed by an r-way AND construction

¡ Transforms similarity s (probability p)into (1-(1-s)b)r§ The same S-curve, mirrored horizontally and

vertically

¡ Example: Take H and construct H’ by the ORconstruction with b = 4. Then, from H’, construct H’’ by the AND construction with r = 4

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 37

Page 34: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 38

s p=(1-(1-s)4)4.1 .0140.2 .1215.3 .3334.4 .5740.5 .7725.6 .9015.7 .9680.8 .9936

The example transforms a (.2,.8,.8,.2)-sensitive family into a (.2,.8,.9936,.1215)-sensitive family

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1Similarity s

Prob

(can

dida

te p

air)

Page 35: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Example: Apply the (4,4) OR-AND construction followed by the (4,4) AND-OR construction

¡ Transforms a (.2, .8, .8, .2)-sensitive family into a (.2, .8, .9999996, .0008715)-sensitive family

§ Note this family uses 256 (=4*4*4*4) of the original hash functions

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 39

Page 36: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Fixpoint: For each AND-OR S-curve 1-(1-sr)b, there is a threshold t, for which 1-(1-tr)b = t

¡ Above t, high probabilities are increased; below t, low probabilities are decreased

¡ You improve the sensitivity as long as the low probability is less than t, and the high probability is greater than t§ Iterate as you like

¡ Similar observation for the OR-AND type of S-curve: (1-(1-s)b)r

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 40

Page 37: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

Thresholdt

t4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 41

ProbabilityIs lowered

ProbabilityIs raised

s

Prob

(Can

dida

te p

air)

Page 38: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Pick any two distances d1 < d2

¡ Start with a (d1, d2, (1- d1), (1- d2))-sensitive family

¡ Apply constructions to amplify(d1, d2, p1, p2)-sensitive family, where p1 is almost 1 and p2 is almost 0

¡ The closer to 0 and 1 we get, the more hash functions must be used!

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 42

Page 39: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob
Page 40: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ LSH methods for other distance metrics:§ Cosine distance: Random hyperplanes§ Euclidean distance: Project on lines

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 44

PointsHash func.

Signatures: short integer signatures that reflect their similarity Locality-

sensitiveHashing

Candidate pairs:those pairs of signatures that we need to test for similarity

Design a (d1, d2, p1, p2)-sensitivefamily of hash functions (for that

particular distance metric)

Amplify the family using AND and OR

constructions

Depends on the distance function used

Page 41: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 45

DataHash func.

Signatures: short integer signatures that reflect their similarity Locality-

sensitiveHashing

Candidate pairs:those pairs of signatures that we need to test for similarity

MinHash 1 5 1 52 3 1 36 4 6 4

0 1 0 01 1 1 00 0 0 10 1 0 10 0 1 01 0 0 1

“Bands” technique

RandomHyperplanes -1 +1 -1 -1

+1 +1 +1 -1-1 -1 -1 -1

0 1 0 01 1 1 00 0 0 10 1 0 10 0 1 01 0 0 1

“Bands” technique

Doc

umen

tsD

ata

poin

ts

Candidate pairs

Candidate pairs

Page 42: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Cosine distance = angle between vectors from the origin to the points in questiond(A, B) = q = arccos(A×B / ǁAǁ·ǁBǁ)§ Has range [", $] (equivalently [0,180°])

§ Can divide q by $ to have distance in range [0,1]¡ Cosine similarity = 1-d(A,B)§ But often defined as cosine sim: cos(*) = -⋅/

- /

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 46

A

B

A×BǁBǁ

- Has range -1…1 for general vectors- Range 0..1 for non-negative vectors(angles up to 90°)

Page 43: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ For cosine distance, there is a technique called Random Hyperplanes§ Technique similar to Min-Hashing

¡ Random Hyperplanes method is a (d1, d2, (1-d1/!), (1-d2/!))-sensitive family for any d1 and d2

¡ Reminder: (d1, d2, p1, p2)-sensitive1. If d(x,y) < d1, then prob. that h(x) = h(y) is at least p1

2. If d(x,y) > d2, then prob. that h(x) = h(y) is at most p2

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 47

Page 44: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Each vector v determines a hash function hvwith two buckets

¡ hv(x) = +1 if v×x ³ 0; = -1 if v×x < 0

¡ LS-family H = set of all functions derived from any vector

¡ Claim: For points x and y, Pr[h(x) = h(y)] = 1 – d(x,y) / !

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 48

Page 45: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 49

x

y

Look in theplane of xand y.

θHyperplanenormal to v’.Here h(x) ≠ h(y)

v’

Hyperplanenormal to v.Here h(x) = h(y)

v

Note: what is important is thathyperplane is outside the angle,not that the vector is inside.

Page 46: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 50

So: Prob[Red case] = θ / !So: P[h(x)=h(y)] = 1- θ/" = 1-d(x,y)/"

Page 47: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Pick some number of random vectors, and hash your data for each vector

¡ The result is a signature (sketch) of +1’s and –1’s for each data point

¡ Can be used for LSH like we used the Min-Hash signatures for Jaccard distance

¡ Amplify using AND/OR constructions

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 51

Page 48: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Expensive to pick a random vector in Mdimensions for large M§ Would have to generate M random numbers

¡ A more efficient approach§ It suffices to consider only vectors v

consisting of +1 and –1 components§ Why? Assuming data is random, then vectors of +/-1 cover

the entire space evenly (and does not bias in any way)

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 52

Page 49: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Idea: Hash functions correspond to lines

¡ Partition the line into buckets of size a

¡ Hash each point to the bucket containing its projection onto the line§ An element of the “Signature” is a bucket id for

that given projection line

¡ Nearby points are always close; distant points are rarely in same bucket

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 53

Page 50: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ “Lucky” case:§ Points that are close

hash in the same bucket

§ Distant points end up in different buckets

¡ Two “unlucky” cases:§ Top: unlucky

quantization

§ Bottom: unlucky projection

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 54

vv

Line

Buckets of size av v

vv

v v

vv

vv

Page 51: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 55

v v

vv

vv

vv

Page 52: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 56

Bucketwidth a

Randomlychosen line

Points atdistance d If d << a, then

the chance thepoints are in thesame bucket isat least 1 – d/a.

Page 53: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 57

Bucketwidth a

Points atdistance d

θ

d cos θ

If d >> a, θ mustbe close to 90o

for there to beany chance pointsgo to the samebucket.

Randomlychosen line

Page 54: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ If points are distance d < a/2, prob. they are in same bucket ≥ 1- d/a = ½

¡ If points are distance d > 2a apart, then they can be in the same bucket only if d cos θ ≤ a§ cos θ ≤ ½

§ 60 < θ < 90, i.e., at most 1/3 probability

¡ Yields a (a/2, 2a, 1/2, 1/3)-sensitive family of hash functions for any a

¡ Amplify using AND-OR cascades

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 58

Page 55: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 61

DataHash func.

Signatures: short integer signatures that reflect their similarity Locality-

sensitiveHashing

Candidate pairs:those pairs of signatures that we need to test for similarity

Design a (d1, d2, p1, p2)-sensitivefamily of hash functions (for that

particular distance metric)

Amplify the family using AND and OR

constructions

MinHash 1 5 1 52 3 1 36 4 6 4

0 1 0 01 1 1 00 0 0 10 1 0 10 0 1 01 0 0 1

“Bands” technique

RandomHyperplanes -1 +1 -1 -1

+1 +1 +1 -1-1 -1 -1 -1

0 1 0 01 1 1 00 0 0 10 1 0 10 0 1 01 0 0 1

“Bands” technique

Doc

umen

tsD

ata

poin

ts

Candidate pairs

Candidate pairs

Page 56: Recitation session Review of linear algebra · ¡Remember: bbands, rrows/band ¡Let sim(C 1, C 2) = s What’s the prob. that at least 1 band is equal? ¡Pick some band (rrows) §Prob

¡ Property P(h(C1)=h(C2))=sim(C1,C2) of hash function h is the essential part of LSH, without which we can’t do anything

¡ LS-hash functions transform data to signatures so that the bands technique (AND, OR constructions) can then be applied

4/10/19 Tim Althoff, UW CS547: Machine Learning for Big Data, http://www.cs.washington.edu/cse547 62