69
10-405 Big ML 1

10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

10-405BigML

1

Page 2: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MatrixFactorization

2

Page 3: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

WhatisMFandwhatcanyoudowithit?

3

Page 4: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m columns

v11 …

… …

vij

vnm

n ro

ws

4

Page 5: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

K * m

n *

K

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

5

Page 6: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Whatisthisfor?

K * m

n *

K

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

6

Page 7: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFforcollaborativefiltering

7

Page 8: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Whatiscollaborativefiltering?

Page 9: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m movies

v11 …

… …

vij

vnm

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

n us

ers

9

Page 10: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m movies

n us

ers

m movies

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

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

10

Page 11: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

11

Page 12: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

12

Page 13: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m movies

n us

ers

m movies

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

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

13

Page 14: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFforimagemodeling

14

Page 15: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

15

Page 16: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFforimages

10,000 pixels

1000

imag

es

1000 * 10,000,00

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 … … …

… …

vij

vnm

~

V[i,j] = pixel j in image i

2 prototypes

PC1

PC2

Page 17: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFformodelingtext

17

Page 18: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m terms

n do

cum

ents

doc term matrix

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

V[i,j] = TFIDF score of term j in doc i

18

Page 19: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m terms

n do

cum

ents

doc term matrix

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

V[i,j] = TFIDF score of term j in doc i

19https://technowiki.wordpress.com/2011/08/27/latent-semantic-analysis-lsa-tutorial/

• The Neatest Little Guide to Stock Market Investing

• Investing For Dummies, 4th Edition• The Little Book of Common Sense Investing:

The Only Way to Guarantee Your Fair Share of Stock Market Returns

• The Little Book of Value Investing• Value Investing: From Graham to Buffett and

Beyond• Rich Dad’s Guide to Investing: What the Rich

Invest in, That the Poor and the Middle Class Do Not!

• Investing in Real Estate, 5th Edition• Stock Investing For Dummies• Rich Dad’s Advisors: The ABC’s of Real

Estate Investing: The Secrets of Finding Hidden Profits Most Investors Miss

Page 20: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m terms

n do

cum

ents

doc term matrix

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

V[i,j] = TFIDF score of term j in doc i

20

estate … land

invest rich

dummy stock saving advice ...

Doc = weighted sum of topics

Page 21: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Investing for real estate

Rich Dad’s Advisor’s: The ABCs of Real

Estate Investment …

Page 22: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

The little book of common

sense investing: …

Neatest Little Guide to Stock

Market Investing

Page 23: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFvsotherlearningtasks

23

Page 24: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFislikelinearregression

24

Page 25: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFislikemultiple-outputmulti-variablelinearregression

25

𝑦K = 𝒙 N 𝒘K𝑦P = 𝒙 N 𝒘P

𝑦Q = 𝒙 N 𝒘Q

Page 26: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Multi-outputlinearregressionasMF

m weight vectors

n ex

ampl

es

x11 x12

x21 x22

.. ..

… …

xn1 yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

m outputs for each xiexamples

X

W

Y

output1output2

….

𝑦K = 𝒙 N 𝒘K𝑦P = 𝒙 N 𝒘P

𝑦Q = 𝒙 N 𝒘Q

Page 27: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFislikeclustering

27

Page 28: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

k-means Clustering

centroids

28

Each point is in one cluster

Each cluster is a weighted average of points

Page 29: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

k-meansasMF

cluster means

n ex

ampl

es

0 1

1 0

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

original data setindicators for r

clusters

Z

M

X

Page 30: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFis“soft”clustering– eachexampleisaweightedsumofclusters

K * m

n *

K

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

30

Page 31: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

HowdoyoudoMF?

31

Page 32: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

talk pilfered from à …..

KDD 2011

32

Page 33: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

33

Page 34: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Recoveringlatentfactorsinamatrix

m movies

n us

ers

m movies

x1 y1

x2 y2

.. ..

… …

xn yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

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

r

W

H

V

34

Page 35: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

35

Page 36: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

36

Page 37: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

37

Page 38: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MatrixfactorizationasSGD

step size why does this work?

38

Page 39: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MatrixfactorizationasSGD- whydoesthiswork?Here’sthekeyclaim:

39

Page 40: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Checkingtheclaim

Think for SGD for logistic regression• LR loss = compare y and ŷ = dot(w,x)• similar but now update w (user weights) and x (movie weight)

40

Page 41: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Whatlossfunctionsarepossible?

41

Page 42: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Whatlossfunctionsarepossible?

42

Page 43: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

ALS = alternating least squares

43

limited memory quasi-Newton

Page 44: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

talk pilfered from à …..

KDD 2011

44

Page 45: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

45

iterative SGD, no mixing

limited memory quasi-Newton

param mixing

alternating least squares

IPM

Page 46: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MatrixfactorizationasSGD- whydoesthiswork?Here’sthekeyclaim:

46

Page 47: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

47

Page 48: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

48

Page 49: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

49

H1 H2 H3W1 V

11

W2 V22

W3 V33

H1 H2 H3W1 V

12

W2 V23

W3 V31

H1 H2 H3W1 V

13

W2 V21

W3 V32

Node1

Node2

Node3

Strata1 Strata2 Strata3

Epoch1

Page 50: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

50

iterative SGD, no mixing

limited memory quasi-Newton

param mixing

alternating least squares

IPM

Page 51: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

51

Page 52: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

52

Page 53: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

53

Page 54: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Hadoopscalability

Hadoop process setup time starts

to dominate

54

Page 55: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

MFislikelogisticregression

55

Page 56: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

LinearregressionasMF

weight vectors

n ex

ampl

es

x11 x12

x21 x22

.. ..

… …

xn1 yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

training dataexamples

X

W

Y

output1output2

….

𝑦K = 𝒙 N 𝒘K𝑦P = 𝒙 N 𝒘P

𝑦Q = 𝒙 N 𝒘Q

Page 57: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Logistic?regressionasMF

weight vectors

n ex

ampl

es

x11 x12

x21 x22

.. ..

… …

xn1 yn

a1 a2 .. … am

b1 b2 … … bmv11 …

… …

vij

vnm

~

training dataexamples

X

W

Y

output1output2

….

Page 58: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Vectorizinglogisticregression• ManyMLmethodscanberewrittenusingnothingbutvector-matrixoperations(“vectorizing”)

• Whydothis?–Simpler(onceyouunderstanditwell)–Faster(giventherightinfrastructure- e.g.,numpy,GPUs,…)

–Cansimplifyoptimization(morelater)

58

Page 59: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Vectorized minibatch logistic regression

• Computationwe’dliketovectorize:–Foreachxintheminibatch,compute

• Foreachfeaturej:updatewj using

59

Page 60: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Vectorizing logistic regression

• Computationwe’dliketoparallelize:–Foreachxintheminibatch Xbatch,compute

60

𝑋ghijk𝒘 = 𝑥KK ⋯ 𝑥K

n

⋮ ⋱ ⋮𝑥qK ⋯ 𝑥q

n

𝑤K

⋮𝑤n

=𝒘 N 𝒙𝟏⋮

𝒘 N 𝒙𝑩

Page 61: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Vectorizing logistic regression

• Computationwe’dliketoparallelize:–Foreachxintheminibatch Xbatch,compute

61

𝒘 N 𝒙𝟏⋮

𝒘 N 𝒙𝑩+ 1

innumpyifMisamatrixM+1doesthe“rightthing”

sodoesM.exp(),M.dot(),M.reciprocal(),…

Page 62: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Vectorizing logistic regression

• Computationwe’dliketoparallelize:–Foreachxintheminibatch,compute

62

def logistic(X):return(X.exp()+1).reciprocal()p=logistic(Xb.dot(w))#Brows,1column

Page 63: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Binary to softmax logistic regression

63

𝑋ghijk𝒘 = 𝑥KK ⋯ 𝑥K

n

⋮ ⋱ ⋮𝑥qK ⋯ 𝑥q

n

𝑤K

⋮𝑤n

=𝒘 N 𝒙𝟏⋮

𝒘 N 𝒙𝑩

Page 64: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Binary to softmax logistic regression

64

𝑝x ≡exp(𝒙 N 𝒘x)

∑ exp(𝒙 N�x| 𝒘x|)

X𝑊 = 𝑥KK ⋯ 𝑥K

n

⋮ ⋱ ⋮𝑥qK ⋯ 𝑥q

n

𝑤K

⋮𝑤n

=𝒘 N 𝒙𝟏⋮

𝒘 N 𝒙𝑩

XW= 𝑥KK ⋯ 𝑥K

n

⋮ ⋱ ⋮𝑥qK ⋯ 𝑥q

n

𝑤KxK … 𝑤K

x~

⋮ ⋱ ⋮𝑤nxK … 𝑤n

x~=

𝒘xK N 𝒙K … 𝒘x~ N 𝒙K⋮ ⋱ ⋮

𝒘xK N 𝒙q … 𝒘x~ N 𝒙q

Page 65: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

65

http://minpy.readthedocs.io/en/latest/get-started/logistic_regression.html

Matrixmultiply,;thenexponentiate

component-wise

Sumthecolumnstogetthedenominator;

keepdim=Truemeans…

𝑝x ≡exp(𝒙 N 𝒘x)

∑ exp(𝒙 N�x| 𝒘x|)

XW= 𝑥KK ⋯ 𝑥K

n

⋮ ⋱ ⋮𝑥qK ⋯ 𝑥q

n

𝑤KxK … 𝑤K

x~

⋮ ⋱ ⋮𝑤nxK … 𝑤n

x~=

𝒘xK N 𝒙K … 𝒘x~ N 𝒙K⋮ ⋱ ⋮

𝒘xK N 𝒙q … 𝒘x~ N 𝒙q

… thatthislinewillworkcorrectlyeventhough’a’

and‘a_sum’havedifferentshapes

prob willhaveBrowsandKcolumns,andeach

rowwillsumto1

Page 66: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

66

http://minpy.readthedocs.io/en/latest/get-started/logistic_regression.html

Page 67: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

67

http://minpy.readthedocs.io/en/latest/get-started/logistic_regression.html

Page 68: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

68

http://minpy.readthedocs.io/en/latest/get-started/logistic_regression.html

Erroroneachexamplexinbatchandeachclassy

pythonbug:shouldbex.T (transpose)

Thegradientstep!

x.Tdy= 𝑥KK ⋯ 𝑥qK⋮ ⋱ ⋮𝑥Kn ⋯ 𝑥q

nN𝑑𝑦�K

xK … 𝑑𝑦�Kx~

⋮ ⋱ ⋮𝑑𝑦�q

xK … 𝑑𝑦�qx~

Page 69: 10-405 Big ML - Carnegie Mellon School of Computer Sciencewcohen/10-405/sgd-for-mf.pdf · • The Little Book of Common Sense Investing: The Only Way to Guarantee Your Fair Share

Vectorizing logistic regression

• ManyMLmethodscanberewrittenusingnothingbutvector-matrixoperations(“vectorizing”)

• Whydothis?–Simpler(onceyouunderstanditwell)–Faster(giventherightinfrastructure- e.g.,numpy,GPUs,…)

–Cansimplifyoptimization(morelater)

69