Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Near-Neighbor Search Applications Matrix Formulation Minhashing.

Similar presentations


Presentation on theme: "1 Near-Neighbor Search Applications Matrix Formulation Minhashing."— Presentation transcript:

1 1 Near-Neighbor Search Applications Matrix Formulation Minhashing

2 2 Example Problem --- Face Recognition uWe have a database of (say) 1 million face images. uWe are given a new image and want to find the most similar images in the database. uRepresent faces by (relatively) invariant values, e.g., ratio of nose width to eye width.

3 3 Face Recognition --- (2) uEach image represented by a large number (say 1000) of numerical features. uProblem: given the features of a new face, find those in the DB that are close in at least ¾ (say) of the features.

4 4 Face Recognition --- (3) uMany-one problem : given a new face, see if it is close to any of the 1 million old faces. uMany-Many problem : which pairs of the 1 million faces are similar.

5 5 Simple Solution uRepresent each face by a vector of 1000 values and score the comparisons. uSort-of OK for many-one problem. uOut of the question for the many-many problem (10 6 *10 6 *1000 numerical comparisons). uWe can do better!

6 6 Multidimensional Indexes Don’t Work New face: [6,14,…] 0-4 5-9 10-14... Dimension 1 = Surely we’d better look here. Maybe look here too, in case of a slight error. But the first dimension could be one of those that is not close. So we’d better look everywhere!

7 7 Another Problem: Entity Resolution uTwo sets of 1 million name-address- phone records. uSome pairs, one from each set, represent the same person. uErrors of many kinds: wTypos, missing middle initial, area-code changes, St./Street, Bob/Robert, etc., etc.

8 8 Entity Resolution --- (2) uChoose a scoring system for how close names are. wDeduct so much for edit distance > 0; so much for missing middle initial, etc. uSimilarly score differences in addresses, phone numbers. uSufficiently high total score -> records represent the same entity.

9 9 Simple Solution uCompare each pair of records, one from each set. uScore the pair. uCall them the same if the score is sufficiently high. uUnfeasible for 1 million records. uWe can do better!

10 10 Yet Another Problem: Finding Similar Documents uGiven a body of documents, e.g., the Web, find pairs of docs that have a lot of text in common. uFind mirror sites, approximate mirrors, plagiarism, quotation of one document in another, “good” document with random spam, etc.

11 11 Complexity of Document Similarity uThe face problem had a way of representing a big image by a (relatively) small data-set. uEntity records represent themselves. uHow do you represent a document so it is easy to compare with others?

12 12 Complexity --- (2) uSpecial cases are easy, e.g., identical documents, or one document contained verbatim in another. uGeneral case, where many small pieces of one doc appear out of order in another, is very hard.

13 13 Representing Documents for Similarity Search 1.Represent doc by its set of shingles (or k-grams). 2.Summarize shingle set by a signature = small data-set with the property: wSimilar documents are very likely to have “similar” signatures. uAt that point, doc problem resembles the previous two problems.

14 14 Shingles uA k-shingle (or k-gram) for a document is a sequence of k characters that appears in the document. uExample: k=2; doc = abcab. Set of 2- shingles = {ab, bc, ca}. wOption: regard shingles as a bag, and count ab twice.

15 15 Shingles: Aside uAlthough we shall not discuss it, shingles are a powerful tool for characterizing the topic of documents. wk =5 is the right number; (#characters) 5 >> # shingles in typical document. uExample: “ng av” and “ouchd” are most common in sports articles.

16 16 Shingles: Compression Option uTo compress long shingles, we can hash them to (say) 4 bytes. uRepresent a doc by the set of hash values of its k-shingles. uTwo documents could (rarely) appear to have shingles in common, when in fact only the hash-values were shared.

17 17 MinHashing Data as Sparse Matrices Jaccard Similarity Measure Constructing Signatures

18 18 Roadmap Boolean matrices Market baskets Other apps Signatures Locality- Sensitive Hashing Minhashing Face- recognition Entity- resolution Other apps Shingling Documents

19 19 Boolean Matrix Representation uData in the form of subsets of a universal set can be represented by a (typically sparse) matrix. uExamples include: 1.Documents represented by their set of shingles (or hashes of those shingles). 2.Market baskets.

20 20 Matrix Representation of Item/Basket Data uColumns = items. uRows = baskets. uEntry (r, c ) = 1 if item c is in basket r ; = 0 if not. uTypically matrix is almost all 0’s.

21 21 In Matrix Form mcpbj {m,c,b}11010 {m,p,b}10110 {m,b}10010 {c,j}01001 {m,p,j}10101 {m,c,b,j}11011 {c,b,j}01011 {c,b}01010

22 22 Documents in Matrix Form uColumns = documents. uRows = shingles (or hashes of shingles). u1 in row r, column c iff document c has shingle r. uAgain expect the matrix to be sparse.

23 23 Aside uWe might not really represent the data by a boolean matrix. uSparse matrices are usually better represented by the list of places where there is a non-zero value. wE.g., baskets, shingle-sets. uBut the matrix picture is conceptually useful.

24 24 Assumptions 1.Number of items allows a small amount of main-memory/item. uE.g., main memory = Number of items * 100 2. Too many items to store anything in main-memory for each pair of items.

25 25 Similarity of Columns uThink of a column as the set of rows in which it has 1. uThe similarity of columns C 1 and C 2 = Sim (C 1,C 2 ) = is the ratio of the sizes of the intersection and union of C 1 and C 2. wSim (C 1,C 2 ) = |C 1  C 2 |/|C 1  C 2 | = Jaccard measure.

26 26 Example C 1 C 2 01 10 11Sim (C 1, C 2 ) = 002/5 = 0.41 01 * * * * * * *

27 27 Outline of Algorithm 1.Compute signatures of columns = small summaries of columns. wRead from disk to main memory. 2.Examine signatures in main memory to find similar signatures. wEssential: similarities of signatures and columns are related. 3.Optional: check that columns with similar signatures are really similar.

28 28 Warnings 1.Comparing all pairs of signatures may take too much time, even if not too much space. wA job for Locality-Sensitive Hashing. 2.These methods can produce false negatives, and even false positives if the optional check is not made.

29 29 Signatures uKey idea: “hash” each column C to a small signature Sig (C), such that: 1.Sig (C) is small enough that we can fit a signature in main memory for each column. 2.Sim (C 1, C 2 ) is the same as the “similarity” of Sig (C 1 ) and Sig (C 2 ).

30 30 An Idea That Doesn’t Work uPick 100 rows at random, and let the signature of column C be the 100 bits of C in those rows. uBecause the matrix is sparse, many columns would have 00...0 as a signature, yet be very dissimilar because their 1’s are in different rows.

31 31 Four Types of Rows uGiven columns C 1 and C 2, rows may be classified as: C 1 C 2 a11 b10 c01 d00 uAlso, a = # rows of type a, etc. uNote Sim (C 1, C 2 ) = a /(a +b +c ).

32 32 Minhashing uImagine the rows permuted randomly. uDefine “hash” function h (C ) = the number of the first (in the permuted order) row in which column C has 1. uUse several (100?) independent hash functions to create a signature.

33 33 Minhashing Example Input matrix 0101 0101 1010 1010 1010 1001 0101 3 4 7 6 1 2 5 Signature matrix M 1212 5 7 6 3 1 2 4 1412 4 5 2 6 7 3 1 2121

34 34 Surprising Property uThe probability (over all permutations of the rows) that h (C 1 ) = h (C 2 ) is the same as Sim (C 1, C 2 ). uBoth are a /(a +b +c )! uWhy? wLook down columns C 1 and C 2 until we see a 1. wIf it’s a type-a row, then h (C 1 ) = h (C 2 ). If a type-b or type-c row, then not.

35 35 Similarity for Signatures uThe similarity of signatures is the fraction of the rows in which they agree. wRemember, each row corresponds to a permutation or “hash function.”

36 36 Min Hashing – Example Input matrix 0101 0101 1010 1010 1010 1001 0101 3 4 7 6 1 2 5 Signature matrix M 1212 5 7 6 3 1 2 4 1412 4 5 2 6 7 3 1 2121 Similarities: 1-3 2-4 1-2 3-4 Col/Col 0.75 0.75 0 0 Sig/Sig 0.67 1.00 0 0

37 37 Minhash Signatures uPick (say) 100 random permutations of the rows. uThink of Sig (C) as a column vector. uLet Sig (C)[i] = according to the i th permutation, the number of the first row that has a 1 in column C.

38 38 Implementation --- (1) uNumber of rows = 1 billion (say). uHard to pick a random permutation from 1…billion. uRepresenting a random permutation requires 1 billion entries. uAccessing rows in permuted order is tough! wThe number of passes would be prohibitive.

39 39 Implementation --- (2) 1.Pick (say) 100 hash functions. 2.For each column c and each hash function h i, keep a “slot” M (i, c ) for that minhash value.

40 40 Implementation --- (3) for each row r for each column c if c has 1 in row r for each hash function h i do if h i (r ) is a smaller value than M (i, c ) then M (i, c ) := h i (r ) uNeeds only one pass through the data.

41 41 Example RowC1C2 1 1 0 2 0 1 3 1 1 4 1 0 5 0 1 h(x) = x mod 5 g(x) = 2x+1 mod 5 h(1) = 11- g(1) = 33- h(2) = 212 g(2) = 030 h(3) = 312 g(3) = 220 h(4) = 412 g(4) = 420 h(5) = 010 g(5) = 120 Sig1Sig2


Download ppt "1 Near-Neighbor Search Applications Matrix Formulation Minhashing."

Similar presentations


Ads by Google