1 of 44

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

2 of 44

Announcements

  • Today:
    • Jay Yoon Lee guest lecture, plus some review
  • Wednesday:
    • lecture: Autodiff and GPUs (new material)
    • HW2 slot B
  • Thursday
    • HW3 slot A
  • Friday
    • HW3 writing session (no recitation)
    • Release practice exam questions
  • Mon Oct 5
    • Lecture: Exam review
    • HW3 slot B
  • Wed Oct 7
    • In-class exam; one hand-written set of notes

​

3 of 44

Outline

  • Review
    • Countmin Sketches
    • LSH
  • Locality Sensitive Hashing and k-NN
    • LSH and multi-index hashing
    • hierarchical k-means and k-NN search
    • vector quantization of product quantization
  • Reverse-mode automatic differentiation

4 of 44

Countmin notes

  • Negative values a[x] < 0:
    • the proof breaks (see notes) because it uses the Markov inequality
    • negative increments
      • seem to be ok if a[x]>0
    • count-median sketch extends count-min to negative values a[x]
    • visualization:

X is a positive random variable

5 of 44

Outline

  • Review
    • Countmin Sketches
    • LSH
  • Locality Sensitive Hashing and k-NN
    • LSH and multi-index hashing
    • hierarchical k-means and k-NN search
    • vector quantization of product quantization
  • Reverse-mode automatic differentiation

6 of 44

LSH review

  • 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

7 of 44

LSH review

8 of 44

LSH review

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

9 of 44

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

10 of 44

k-NN search and Multi-index Hashing

  • 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

​

​

​

​

​

​

11 of 44

k-NN search and Multi-index Hashing

  • 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!

12 of 44

An alternative: k-means for k-NN search

13 of 44

kJ-means for k-NN search

14 of 44

J-means Clustering

14

centroids

15 of 44

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 – like in this visualization
      • O(d J ) * O(N / Jd)

​

16 of 44

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!

17 of 44

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 set 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 makes PQ accurate

​

  • Visualization in 2 dimensions
  • Visualization on text

​

​

​

​

​

​

​

18 of 44

A Recent(ish) Survey

2024

19 of 44

Outline

  • Review
    • Countmin Sketches
    • LSH
  • Locality Sensitive Hashing and k-NN
    • LSH and multi-index hashing
    • hierarchical k-means and k-NN search
    • vector quantization of product quantization
  • Reverse-mode automatic differentiation

20 of 44

Automatic Differentiation and Deep Learning

20

21 of 44

Recap: Polars LazyFrame actions

  • For large data it’s useful to explicitly represent the “code” you want to run
    • For optimization
    • For distribution to workers
    • For recovery from errors
    • …
  • This happens over and over in different contexts

21

We will use the same trick to make gradient computations for complex, arbitrary functions easy

22 of 44

Autodiff

22

Wengert list

What we did in calculus 101

Flattened version of computation graph

We don’t want a symbolic form for the derivative – we want an algorithm to compute it at any given point

23 of 44

Autodiff

23

x1

x2

add

add

square

f

Flattened version of computation graph

Autodiff on Wengert lists (“tape” of operations) is simple

24 of 44

Autodiff

24

x1=2

x2=3

add=4

add=7

square=49

f=49

Flattened version of computation graph

We can compute the value of f at a given point, like (x1,x2)=(2,3)

Want an algorithm to compute partial derivatives at any given points

g1(2,3)=28

g2(2,3)=14

Goal: use the compute graph to compute g1(x1,x2), g2(x1,x2)

g1(x1,x2) =

g2(x1,x2) =

What we did in calculus 101

25 of 44

Wengert lists: forward (eval)

25

Possible Python data structure:

Plus the definitions:

26 of 44

Wengert lists: forward (eval)

26

Plus the definitions:

and input values:

27 of 44

Wengert lists: backward (gradients)

27

Need the partial derivatives of each function wrt. each input:

Still need the definitions:

and input values:

28 of 44

Wengert lists: backward (gradients)

28

val[…] from forward pass

final output of Wengert list

backpropagated error

29 of 44

Why is this correct?

30 of 44

Simple case: a chain of functions

30

High school: symbolic differentiation, compute a symbolic form of the deriv of f

Now: automatic differentiation, find an algorithm to compute f’(a) at any point a

31 of 44

Differentiating a Wengert list: a simple case

31

Now: automatic differentiation, find an algorithm to compute f’(a) at any point a

Notation (1):

🡪

ai is the i-th output

on input a

32 of 44

Differentiating a Wengert list: a simple case

32

for all a

m-1,0

What did Liebnitz mean with this?

 

 

 

Notation (1):

🡪

ai is the i-th output

on input a

 

33 of 44

Differentiating a Wengert list: a simple case

33

for all a

reverse-mode compute order

need to track:

 

34 of 44

Wengert lists: backward (gradients)

  • Step 0: a function of n variables, coded as a series of assignments
  • Step 1: evaluate each variable
  • Step 2: back propagate by going thru the list in reverse order, starting with…

​

  • …and using the chain rule

​

34

e.g.

Wengert list

return

inputs:

Step 1: forward

Step 2: backward

evaluated at (x1,…,xn)

Computed in previous step

inputs, func. for step 7

35 of 44

Wengert lists: backward (gradients)

35

Plus the definitions:

and input values:

36 of 44

Wengert lists: backward (gradients)

36

Need the partial derivatives of each function wrt. each input:

Now “backpropagate errors (loss)” to each variable. We want:

37 of 44

Wengert lists: backward (gradients)

37

38 of 44

Ridge Regression as a Wengert list

38

Autodiff with linear algebra operations is much more useful

Also a little more confusing…

​

df/dw is a vector <df/dw1, … df/dwn>

39 of 44

Logistic Regression as a Wengert list

39

Like logistic

where:

40 of 44

Logistic Regression as a Wengert list

40

loss = crossEntropy(softmax(z), y) is widely used

but it’s important to use this version

41 of 44

Logistic Regression as a Wengert list

41

loss = crossEntropy(softmax(z), y) is widely used

Also if p is the output of the softmax, there is a simple form of d loss / d z

​

d loss / d z = p - y

42 of 44

Logistic Regression as a Wengert list

42

How can you take advantage of this?

(1) write a special crossEntropyOfSoftmax(z, y) function in your library of functions.

torch.nn.CrossEntropyLoss

(2) optimize the “generated code”

Most systems do a lot of both

43 of 44

Notes on Wengert lists

  • Most ML frameworks sort-of conceal the computation graph
    • Torch, PyTorch, TensorFlow2 let you build a graph on-the-fly as you evaluate expressions
  • It’s still there and helpful for understanding

43

44 of 44

Notes on Wengert lists

  • You can do reverse-mode backprop without them
    • Accumulate gradients on-the-fly by traversing the computation graph
  • They are helpful conceptually (I think)
  • Sometimes a Wengert list is called a “tape”

44