Presentation is loading. Please wait.

Presentation is loading. Please wait.

Efficient Logistic Regression with Stochastic Gradient Descent William Cohen.

Similar presentations


Presentation on theme: "Efficient Logistic Regression with Stochastic Gradient Descent William Cohen."— Presentation transcript:

1 Efficient Logistic Regression with Stochastic Gradient Descent William Cohen

2 Admin Phrase assignment extension – We forgot about discarding phrases with stop words – Aside: why do stop words hurt performance? How many map-reduce assignments does it take to screw in a lightbulb? – ? AWS mechanics – I will circulate keys for $100 of time …soon – You’ll need to sign up for an account with a credit/debit card anyway as guarantee Let me know if this is a major problem – $100 should be plenty Let us know if you run out

3 Reminder: Your map-reduce assignments are mostly done Old NB learning Stream & sort Stream – sort – aggregate – Counter update “message” – Optimization: in-memory hash, periodically emptied – Sort of messages – Logic to aggregate them Workflow is on one machine New NB learning Map-reduce Map – shuffle - reduce – Counter update Map – Combiner – (Hidden) shuffle & sort – Sum-reduce Workflow is done in parallel

4 NB implementation summary java CountForNB train.dat … > eventCounts.dat java CountsByWord eventCounts.dat | sort | java CollectRecords > words.dat java requestWordCounts test.dat | tee words.dat | sort | java answerWordCountRequests | tee test.dat| sort | testNBUsingRequests id 1 w 1,1 w 1,2 w 1,3 …. w 1,k1 id 2 w 2,1 w 2,2 w 2,3 …. id 3 w 3,1 w 3,2 …. id 4 w 4,1 w 4,2 … id 5 w 5,1 w 5,2 …... X=w1^Y=sports X=w1^Y=worldNews X=.. X=w2^Y=… X=… … 5245 1054 2120 37 3 … train.dat counts.dat Map to generate counter updates + Sum combiner + Sum reducer

5 Implementation summary java CountForNB train.dat … > eventCounts.dat java CountsByWord eventCounts.dat | sort | java CollectRecords > words.dat java requestWordCounts test.dat | tee words.dat | sort | java answerWordCountRequests | tee test.dat| sort | testNBUsingRequests words.dat wCounts associated with W aardvarkC[w^Y=sports]=2 agentC[w^Y=sports]=1027,C[w^Y=worldNews]=564 …… zyngaC[w^Y=sports]=21,C[w^Y=worldNews]=4464 Map Reduce

6 Implementation summary java CountForNB train.dat … > eventCounts.dat java CountsByWord eventCounts.dat | sort | java CollectRecords > words.dat java requestWordCounts test.dat | tee words.dat | sort | java answerWordCountRequests | tee test.dat| sort | testNBUsingRequests wCounts aardvarkC[w^Y=sports]=2 agent… … zynga … found~ctr to id 1 aardvark ~ctr to id 2 … today ~ctr to id i … wCounts aardvarkC[w^Y=sports]=2 aardvark~ctr to id1 agentC[w^Y=sports]=… agent~ctr to id345 agent~ctr to id9854 …~ctr to id345 agent~ctr to id34742 … zyngaC[…] zynga~ctr to id1 output looks like this input looks like this words.dat Map + IdentityReduce; save output in temp files Identity Map with two sets of inputs; custom secondary sort (used in shuffle/sort) Reduce, output to temp

7 Implementation summary java CountForNB train.dat … > eventCounts.dat java CountsByWord eventCounts.dat | sort | java CollectRecords > words.dat java requestWordCounts test.dat | tees words.dat | sort | java answerWordCountRequests | tee test.dat| sort | testNBUsingRequests Output: id1 ~ctr for aardvark is C[w^Y=sports]=2 … id1 ~ctr for zynga is …. … id 1 found an aardvark in zynga’s farmville today! id 2 … id 3 …. id 4 … id 5 ….. Output looks like this test.dat Identity Map with two sets of inputs; custom secondary sort

8 Implementation summary java CountForNB train.dat … > eventCounts.dat java CountsByWord eventCounts.dat | sort | java CollectRecords > words.dat java requestWordCounts test.dat | tees words.dat | sort | java answerWordCountRequests | tee test.dat| sort | testNBUsingRequests Input looks like this KeyValue id1found aardvark zynga farmville today ~ctr for aardvark is C[w^Y=sports]=2 ~ctr for found is C[w^Y=sports]=1027,C[w^Y=worldNews]=564 … id2w 2,1 w 2,2 w 2,3 …. ~ctr for w 2,1 is … …… Reduce

9 Reminder: The map-reduce assignments are mostly done Old NB testing Request-answer – Produce requests – Sort requests and records together – Send answers to requestors – Sort answers and sending entities together – … – Reprocess input with request answers Workflows are isomorphic New NB testing Two map-reduces – Produce requests w/ Map – (Hidden) shuffle & sort with custom secondary sort – Reduce, which sees records first, then requests – Identity map with two inputs – Custom secondary sort – Reduce that sees old input first, then answers Workflows are isomporphic

10 Outline Reminder about early/next assignments Logistic regression and SGD – Learning as optimization – Logistic regression: a linear classifier optimizing P(y|x) – Stochastic gradient descent “streaming optimization” for ML problems – Regularized logistic regression – Sparse regularized logistic regression – Memory-saving logistic regression

11 Learning as optimization: warmup Goal: Learn the parameter θ of a binomial Dataset: D={x 1,…,x n }, x i is 0 or 1 MLE estimate of θ, Pr(x i =1) – #[flips where x i =1]/#[flips x i ] = k/n Now: reformulate as optimization…

12 Learning as optimization: warmup Goal: Learn the parameter θ of a binomial Dataset: D={x 1,…,x n }, x i is 0 or 1, k of them are 1 Easier to optimize:

13 Learning as optimization: warmup Goal: Learn the parameter θ of a binomial Dataset: D={x 1,…,x n }, x i is 0 or 1, k are 1

14 Learning as optimization: warmup Goal: Learn the parameter θ of a binomial Dataset: D={x 1,…,x n }, x i is 0 or 1, k of them are 1 = 0 θ = 0 θ = 1 k- kθ – nθ + kθ = 0  nθ = k  θ = k/n

15 Learning as optimization: general procedure Goal: Learn the parameter θ of a classifier – probably θ is a vector Dataset: D={x 1,…,x n } Write down Pr(D|θ) as a function of θ Maximize by differentiating and setting to zero

16 Learning as optimization: general procedure Goal: Learn the parameter θ of … Dataset: D={x 1,…,x n } – or maybe D={(x 1,y 1 )…,(x n, y n) } Write down Pr(D|θ) as a function of θ – Maybe easier to work with log Pr(D|θ) Maximize by differentiating and setting to zero – Or, use gradient descent: repeatedly take a small step in the direction of the gradient

17 Learning as optimization: general procedure for SGD Goal: Learn the parameter θ of … Dataset: D={x 1,…,x n } – or maybe D={(x 1,y 1 )…,(x n, y n) } Write down Pr(D|θ) as a function of θ Big-data problem: we don’t want to load all the data D into memory, and the gradient depends on all the data Solution: – pick a small subset of examples B<<D – approximate the gradient using them “on average” this is the right direction – take a step in that direction – repeat…. B = one example is a very popular choice

18 Learning as optimization for logistic regression Goal: Learn the parameter θ of a classifier – Which classifier? – We’ve seen y = sign(x. w) but sign is not continuous… – Convenient alternative: replace sign with the logistic function

19 Learning as optimization for logistic regression Goal: Learn the parameter w of the classifier Probability of a single example (y,x) would be Or with logs:

20

21 p

22

23 Key computational point: if x j =0 then the gradient of w j is zero so when processing an example you only need to update weights for the non-zero features of an example. An observation

24 Learning as optimization for logistic regression Final algorithm: -- do this in random order

25 Another observation Consider averaging the gradient over all the examples D={(x 1,y 1 )…,(x n, y n) }

26 Learning as optimization for logistic regression Goal: Learn the parameter θ of a classifier – Which classifier? – We’ve seen y = sign(x. w) but sign is not continuous… – Convenient alternative: replace sign with the logistic function Practical problem: this overfits badly with sparse features – e.g., if w j is only in positive examples, its gradient is always positive !

27 Outline [other stuff] Logistic regression and SGD – Learning as optimization – Logistic regression: a linear classifier optimizing P(y|x) – Stochastic gradient descent “streaming optimization” for ML problems – Regularized logistic regression – Sparse regularized logistic regression – Memory-saving logistic regression

28 Regularized logistic regression Replace LCL with LCL + penalty for large weights, eg So: becomes:

29 Regularized logistic regression Replace LCL with LCL + penalty for large weights, eg So the update for wj becomes: Or

30 Learning as optimization for logistic regression Algorithm: -- do this in random order

31 Learning as optimization for regularized logistic regression Algorithm: Time goes from O(nT) to O(nmT) where n = number of non-zero entries, m = number of features, T = number of passes over data Time goes from O(nT) to O(nmT) where n = number of non-zero entries, m = number of features, T = number of passes over data

32 Learning as optimization for regularized logistic regression Final algorithm: Initialize hashtable W For each iteration t=1,…T – For each example (x i,y i ) p i = … For each feature W[j] – W[j] = W[j] - λ2μW[j] – If x i j >0 then » W[j] = W[j] + λ(y i - p i )x j

33 Learning as optimization for regularized logistic regression Final algorithm: Initialize hashtable W For each iteration t=1,…T – For each example (x i,y i ) p i = … For each feature W[j] – W[j] *= (1 - λ2μ) – If x i j >0 then » W[j] = W[j] + λ(y i - p i )x j

34 Learning as optimization for regularized logistic regression Final algorithm: Initialize hashtable W For each iteration t=1,…T – For each example (x i,y i ) p i = … For each feature W[j] – If x i j >0 then » W[j] *= (1 - λ2μ) A » W[j] = W[j] + λ(y i - p i )x j A is number of examples seen since the last time we did an x>0 update on W[j]

35 Learning as optimization for regularized logistic regression Final algorithm: Initialize hashtables W, A and set k=0 For each iteration t=1,…T – For each example (x i,y i ) p i = … ; k++ For each feature W[j] – If x i j >0 then » W[j] *= (1 - λ2μ) k-A[j] » W[j] = W[j] + λ(y i - p i )x j » A[j] = k k-A[j] is number of examples seen since the last time we did an x>0 update on W[j]

36 Learning as optimization for regularized logistic regression Final algorithm: Initialize hashtables W, A and set k=0 For each iteration t=1,…T – For each example (x i,y i ) p i = … ; k++ For each feature W[j] – If x i j >0 then » W[j] *= (1 - λ2μ) k-A[j] » W[j] = W[j] + λ(y i - p i )x j » A[j] = k Time goes from O(nmT) back to to O(nT) where n = number of non-zero entries, m = number of features, T = number of passes over data memory use doubles (m  2m) Time goes from O(nmT) back to to O(nT) where n = number of non-zero entries, m = number of features, T = number of passes over data memory use doubles (m  2m)

37 Outline [other stuff] Logistic regression and SGD – Learning as optimization – Logistic regression: a linear classifier optimizing P(y|x) – Stochastic gradient descent “streaming optimization” for ML problems – Regularized logistic regression – Sparse regularized logistic regression – Memory-saving logistic regression

38 Pop quiz In text classification most words are a.rare b.not correlated with any class c.given low weights in the LR classifier d.unlikely to affect classification e.not very interesting

39 Pop quiz In text classification most bigrams are a.rare b.not correlated with any class c.given low weights in the LR classifier d.unlikely to affect classification e.not very interesting

40 Pop quiz Most of the weights in a classifier are – important – not important

41 How can we exploit this? One idea: combine uncommon words together randomly Examples: – replace all occurrances of “humanitarianism” or “biopsy” with “humanitarianismOrBiopsy” – replace all occurrances of “schizoid” or “duchy” with “schizoidOrDuchy” – replace all occurrances of “gynecologist” or “constrictor” with “gynecologistOrConstrictor” – … For Naïve Bayes this breaks independence assumptions – it’s not obviously a problem for logistic regression, though I could combine – two low-weight words (won’t matter much) – a low-weight and a high-weight word (won’t matter much) – two high-weight words (not very likely to happen) How much of this can I get away with? – certainly a little – is it enough to make a difference?

42 How can we exploit this? Another observation: – the values in my hash table are weights – the keys in my hash table are strings for the feature names We need them to avoid collisions But maybe we don’t care about collisions? – Allowing “schizoid” & “duchy” to collide is equivalent to replacing all occurrences of “schizoid” or “duchy” with “schizoidOrDuchy”

43 Learning as optimization for regularized logistic regression Algorithm: Initialize hashtables W, A and set k=0 For each iteration t=1,…T – For each example (x i,y i ) p i = … ; k++ For each feature j: x i j >0: » W[j] *= (1 - λ2μ) k-A[j] » W[j] = W[j] + λ(y i - p i )x j » A[j] = k

44 Learning as optimization for regularized logistic regression Algorithm: Initialize arrays W, A of size R and set k=0 For each iteration t=1,…T – For each example (x i,y i ) Let V be hash table so that p i = … ; k++ For each hash value h: V[h]>0: » W[h] *= (1 - λ2μ) k-A[j] » W[h] = W[h] + λ(y i - p i )V[h] » A[j] = k

45 Learning as optimization for regularized logistic regression Algorithm: Initialize arrays W, A of size R and set k=0 For each iteration t=1,…T – For each example (x i,y i ) Let V be hash table so that p i = … ; k++ For each hash value h: V[h]>0: » W[h] *= (1 - λ2μ) k-A[j] » W[h] = W[h] + λ(y i - p i )V[h] » A[j] = k ???


Download ppt "Efficient Logistic Regression with Stochastic Gradient Descent William Cohen."

Similar presentations


Ads by Google