Lecture 3 MultiClassification KaiWei Chang CS UCLA kwkwchang

  • Slides: 79
Download presentation
Lecture 3: Multi-Classification Kai-Wei Chang CS @ UCLA kw@kwchang. net Couse webpage: https: //uclanlp.

Lecture 3: Multi-Classification Kai-Wei Chang CS @ UCLA kw@kwchang. net Couse webpage: https: //uclanlp. github. io/CS 269 -17/ ML in NLP 1

Previous Lecture v CS 6501 Lecture 3 2

Previous Lecture v CS 6501 Lecture 3 2

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one v Error correcting codes v Training a single classifier v Multiclass Perceptron: Kesler’s construction v Multiclass SVMs: Crammer&Singer formulation v Multinomial logistic regression CS 6501 Lecture 3 3

What is multiclass v CS 6501 Lecture 3 4

What is multiclass v CS 6501 Lecture 3 4

Multi-class Applications in NLP? ML in NLP 5

Multi-class Applications in NLP? ML in NLP 5

Two key ideas to solve multiclass v Reducing multiclass to binary v Decompose the

Two key ideas to solve multiclass v Reducing multiclass to binary v Decompose the multiclass prediction into multiple binary decisions v Make final decision based on multiple binary classifiers v Training a single classifier v Minimize the empirical risk v Consider all classes simultaneously CS 6501 Lecture 3 6

Reduction v. s. single classifier v Reduction v Future-proof: binary classification improved so does

Reduction v. s. single classifier v Reduction v Future-proof: binary classification improved so does muti-class v Easy to implement v Single classifier v Global optimization: directly minimize the empirical loss; easier for joint prediction v Easy to add constraints and domain knowledge CS 6501 Lecture 3 7

A General Formula v input model parameters output space CS 6501 Lecture 3 8

A General Formula v input model parameters output space CS 6501 Lecture 3 8

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one v Error correcting codes v Training a single classifier v Multiclass Perceptron: Kesler’s construction v Multiclass SVMs: Crammer&Singer formulation v Multinomial logistic regression CS 6501 Lecture 3 9

One against all strategy CS 6501 Lecture 3 10

One against all strategy CS 6501 Lecture 3 10

One against All learning v Multiclassifier v Function f : Rn {1, 2, 3,

One against All learning v Multiclassifier v Function f : Rn {1, 2, 3, . . . , k} v Decompose into binary problems CS 6501 Lecture 3 11

One-again-All learning algorithm v CS 6501 Lecture 3 12

One-again-All learning algorithm v CS 6501 Lecture 3 12

One against All learning v Multiclassifier v Function f : Rn {1, 2, 3,

One against All learning v Multiclassifier v Function f : Rn {1, 2, 3, . . . , k} v Decompose into binary problems Ideal case: only the correct label will have a positive score CS 6501 Lecture 3 13

One-again-All Inference v CS 6501 Lecture 3 14

One-again-All Inference v CS 6501 Lecture 3 14

One-again-All analysis v Not always possible to learn v Assumption: each class individually separable

One-again-All analysis v Not always possible to learn v Assumption: each class individually separable from all the others v No theoretical justification v Need to make sure the range of all classifiers is the same – we are comparing scores produced by K classifiers trained independently. v Easy to implement; work well in practice CS 6501 Lecture 3 15

One v. s. One (All against All) strategy CS 6501 Lecture 3 16

One v. s. One (All against All) strategy CS 6501 Lecture 3 16

One v. s. One learning v Multiclassifier v Function f : Rn {1, 2,

One v. s. One learning v Multiclassifier v Function f : Rn {1, 2, 3, . . . , k} v Decompose into binary problems Test Training CS 6501 Lecture 3 17

One-v. s-One learning algorithm v CS 6501 Lecture 3 18

One-v. s-One learning algorithm v CS 6501 Lecture 3 18

One-v. s-One Inference algorithm v Decision Options: v More complex; each label gets k-1

One-v. s-One Inference algorithm v Decision Options: v More complex; each label gets k-1 votes v Output of binary classifier may not cohere. v Majority: classify example x to take label i if i wins on x more often than j (j=1, …k) v A tournament: start with n/2 pairs; continue with winners CS 6501 Lecture 3 19

Classifying with One-vs-one Tournament Majority Vote 1 red, 2 yellow, 2 green ? All

Classifying with One-vs-one Tournament Majority Vote 1 red, 2 yellow, 2 green ? All are post-learning and might cause weird stuff CS 6501 Lecture 3 20

One-v. s. -one Assumption v Every pair of classes is separable It is possible

One-v. s. -one Assumption v Every pair of classes is separable It is possible to separate all k classes with the O(k 2) classifiers Decision Regions CS 6501 Lecture 3 21

Comparisons v CS 6501 Lecture 3 22

Comparisons v CS 6501 Lecture 3 22

Problems with Decompositions v Learning optimizes over local metrics v Does not guarantee good

Problems with Decompositions v Learning optimizes over local metrics v Does not guarantee good global performance v We don’t care about the performance of the local classifiers v Poor decomposition poor performance v Difficult local problems v Irrelevant local problems v Efficiency: e. g. , All vs. One vs. All v Not clear how to generalize multi-class to problems with a very large # of output CS 6501 Lecture 3 23

Still an ongoing research direction Key questions: v How to deal with large number

Still an ongoing research direction Key questions: v How to deal with large number of classes v How to select “right samples” to train binary classifiers v Error-correcting tournaments [Beygelzimer, Langford, Ravikumar 09] v Logarithmic Time One-Against-Some [Daume, Karampatziakis, Langford, Mineiro 16] v Label embedding trees for large multi-class tasks. [Bengio, Weston, Grangier 10] v… CS 6501 Lecture 3 24

Decomposition methods: Summary v General Ideas: v Decompose the multiclass problem into many binary

Decomposition methods: Summary v General Ideas: v Decompose the multiclass problem into many binary problems v Prediction depends on the decomposition v Constructs the multiclass label from the output of the binary classifiers v Learning optimizes local correctness v Each binary classifier don’t need to be globally correct and isn’t aware of the prediction procedure CS 6501 Lecture 3 25

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one v Error correcting codes v Training a single classifier v Multiclass Perceptron: Kesler’s construction v Multiclass SVMs: Crammer&Singer formulation v Multinomial logistic regression CS 6501 Lecture 3 26

Revisit One-again-All learning algorithm v CS 6501 Lecture 3 27

Revisit One-again-All learning algorithm v CS 6501 Lecture 3 27

Observation v CS 6501 Lecture 3 28

Observation v CS 6501 Lecture 3 28

Perceptron-style algorithm v CS 6501 Lecture 3 29

Perceptron-style algorithm v CS 6501 Lecture 3 29

A Perceptron-style Algorithm How to analyze this algorithm and simplify the update rules? v

A Perceptron-style Algorithm How to analyze this algorithm and simplify the update rules? v CS 6501 Lecture 3 30

Linear Separability with multiple classes v ? ? multiple models v. s. multiple data

Linear Separability with multiple classes v ? ? multiple models v. s. multiple data points CS 6501 Lecture 3 31

Kesler construction Assume we have a multi-class problem with K class and n features.

Kesler construction Assume we have a multi-class problem with K class and n features. v CS 6501 Lecture 3 32

Kesler construction Assume we have a multi-class problem with K class and n features.

Kesler construction Assume we have a multi-class problem with K class and n features. v CS 6501 Lecture 3 33

Kesler construction Assume we have a multi-class problem with K class and n features.

Kesler construction Assume we have a multi-class problem with K class and n features. v CS 6501 Lecture 3 34

Kesler construction Assume we have a multi-class problem with K class and n features.

Kesler construction Assume we have a multi-class problem with K class and n features. v CS 6501 Lecture 3 35

Kesler construction Assume we have a multi-class problem with K class and n features.

Kesler construction Assume we have a multi-class problem with K class and n features. CS 6501 Lecture 3 36

Linear Separability with multiple classes CS 6501 Lecture 3 37

Linear Separability with multiple classes CS 6501 Lecture 3 37

How can we predict? For input an input x, the model predict label is

How can we predict? For input an input x, the model predict label is 3 CS 6501 Lecture 3 38

How can we predict? For input an input x, the model predict label is

How can we predict? For input an input x, the model predict label is 3 CS 6501 Lecture 3 39

Constraint Classification v x -x Transform Examples 2>4 2>1 2>3 2>4 CS 6501 Lecture

Constraint Classification v x -x Transform Examples 2>4 2>1 2>3 2>4 CS 6501 Lecture 3 40

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3 41

An alternative training algorithm v CS 6501 Lecture 3 42

An alternative training algorithm v CS 6501 Lecture 3 42

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3 43

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3 44

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3 45

Consider multiclass margin CS 6501 Lecture 3 46

Consider multiclass margin CS 6501 Lecture 3 46

Marginal constraint classifier v Goal: for every (x, y) in the training data set

Marginal constraint classifier v Goal: for every (x, y) in the training data set CS 6501 Lecture 3 47

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3

A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3 48

Remarks v This approach can be generalized to train a ranker; in fact, any

Remarks v This approach can be generalized to train a ranker; in fact, any output structure v We have preference over label assignments v E. g. , rank search results, rank movies / products CS 6501 Lecture 3 49

A peek of a generalized Perceptron model Structured output Structural prediction/Inference Structural loss Model

A peek of a generalized Perceptron model Structured output Structural prediction/Inference Structural loss Model update v CS 6501 Lecture 3 50

Recap: A Perceptron-style Algorithm v CS 6501 Lecture 3 51

Recap: A Perceptron-style Algorithm v CS 6501 Lecture 3 51

Recap: Kesler construction Assume we have a multi-class problem with K class and n

Recap: Kesler construction Assume we have a multi-class problem with K class and n features. v CS 6501 Lecture 3 52

Geometric interpretation # features = n; # classes = K CS 6501 Lecture 3

Geometric interpretation # features = n; # classes = K CS 6501 Lecture 3 53

Recap: A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture

Recap: A Perceptron-style Algorithm How to interpret this update rule? v CS 6501 Lecture 3 54

Multi-category to Constraint Classification v Multiclass v (x, A) (x, (A>B, A>C, A>D) )

Multi-category to Constraint Classification v Multiclass v (x, A) (x, (A>B, A>C, A>D) ) v Multilabel v (x, (A, B)) (x, ( (A>C, A>D, B>C, B>D) ) v Label Ranking v (x, (5>4>3>2>1)) (x, ( (5>4, 4>3, 3>2, 2>1) ) CS 6501 Lecture 3 55

Generalized constraint classifiers v In all cases, we have examples (x, y) with y

Generalized constraint classifiers v In all cases, we have examples (x, y) with y Sk v Where Sk : partial order over class labels {1, . . . , k} v defines “preference” relation ( > ) for class labeling v Consequently, the Constraint Classifier is: h: X Sk v h(x) is a partial order v h(x) is consistent with y if (i<j) y (i<j) h(x) Just like in the multiclass we learn one wi Rn for each label, the same is done for multi-label and ranking. The weight vectors are updated according with the requirements from y Sk CS 6501 Lecture 3 56

Multi-category to Constraint Classification Solving structured prediction problems by ranking algorithms v Multiclass v

Multi-category to Constraint Classification Solving structured prediction problems by ranking algorithms v Multiclass v (x, A) (x, (A>B, A>C, A>D) ) v Multilabel v (x, (A, B)) (x, ( (A>C, A>D, B>C, B>D) ) v Label Ranking v (x, (5>4>3>2>1)) (x, ( (5>4, 4>3, 3>2, 2>1) ) y Sk h: X Sk CS 6501 Lecture 3 57

Properties of Construction (Zimak et. al 2002, 2003) v Can learn any argmax wi.

Properties of Construction (Zimak et. al 2002, 2003) v Can learn any argmax wi. x function (even when i isn’t linearly separable from the union of the others) v Can use any algorithm to find linear separation v Perceptron Algorithm v ultraconservative online algorithm [Crammer, Singer 2001] v Winnow Algorithm v multiclass winnow [ Masterharm 2000 ] v Defines a multiclass margin by binary margin in RKN v multiclass SVM [Crammer, Singer 2001] CS 6501 Lecture 3 58

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one v Error correcting codes v Training a single classifier v Multiclass Perceptron: Kesler’s construction v Multiclass SVMs: Crammer&Singer formulation v Multinomial logistic regression CS 6501 Lecture 3 59

Recall: Margin for binary classifiers v The margin of a hyperplane for a dataset

Recall: Margin for binary classifiers v The margin of a hyperplane for a dataset is the distance between the hyperplane and the data point nearest to it. - -- -- - + + ++ Margin with respect to this hyperplane CS 6501 Lecture 3 60

Multi-class SVM v CS 6501 Lecture 3 61

Multi-class SVM v CS 6501 Lecture 3 61

Multiclass Margin CS 6501 Lecture 3 62

Multiclass Margin CS 6501 Lecture 3 62

Multiclass SVM (Intuition) v Binary SVM v Maximize margin. Equivalently, Minimize norm of weights

Multiclass SVM (Intuition) v Binary SVM v Maximize margin. Equivalently, Minimize norm of weights such that the closest points to the hyperplane have a score 1 v Multiclass SVM v Each label has a different weight vector (like one-vs-all) v Maximize multiclass margin. Equivalently, Minimize total norm of the weights such that the true label is scored at least 1 more than the second best one CS 6501 Lecture 3 63

Multiclass SVM in the separable case Recall hard binary SVM Size of the weights.

Multiclass SVM in the separable case Recall hard binary SVM Size of the weights. Effectively, regularizer The score for the true label is higher than the score for any other label by 1 CS 6501 Lecture 3 64

Multiclass SVM: General case Size of the weights. Effectively, regularizer The score for the

Multiclass SVM: General case Size of the weights. Effectively, regularizer The score for the true label is higher than the score for any other label by 1 Total slack. Effectively, don’t allow too many examples to violate the margin constraint Slack variables. Not all examples need to satisfy the margin constraint. Slack variables can only be positive CS 6501 Lecture 3 65

Multiclass SVM: General case Size of the weights. Effectively, regularizer The score for the

Multiclass SVM: General case Size of the weights. Effectively, regularizer The score for the true label is higher than the score for any other label by 1 - » i Total slack. Effectively, don’t allow too many examples to violate the margin constraint Slack variables. Not all examples need to satisfy the margin constraint. Slack variables can only be positive CS 6501 Lecture 3 66

Recap: An alternative SVM formulation v Regularization term CS 6501 Lecture 3 Empirical loss

Recap: An alternative SVM formulation v Regularization term CS 6501 Lecture 3 Empirical loss 67

Rewrite it as unconstraint problem v CS 6501 Lecture 3 68

Rewrite it as unconstraint problem v CS 6501 Lecture 3 68

Multiclass SVM v Generalizes binary SVM algorithm v If we have only two classes,

Multiclass SVM v Generalizes binary SVM algorithm v If we have only two classes, this reduces to the binary (up to scale) v Comes with similar generalization guarantees as the binary SVM v Can be trained using different optimization methods v Stochastic sub-gradient descent can be generalized CS 6501 Lecture 3 69

Exercise! v Write down SGD for multiclass SVM v Write down multiclas SVM with

Exercise! v Write down SGD for multiclass SVM v Write down multiclas SVM with Kesler construction CS 6501 Lecture 3 70

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one

This Lecture v Multiclassification overview v Reducing multiclass to binary v One-against-all & One-vs-one v Error correcting codes v Training a single classifier v Multiclass Perceptron: Kesler’s construction v Multiclass SVMs: Crammer&Singer formulation v Multinomial logistic regression CS 6501 Lecture 3 71

Recall: (binary) logistic regression v CS 6501 Lecture 3 72

Recall: (binary) logistic regression v CS 6501 Lecture 3 72

(multi-class) log-linear model Partition function v This often called soft-max CS 6501 Lecture 3

(multi-class) log-linear model Partition function v This often called soft-max CS 6501 Lecture 3 73

Softmax v CS 6501 Lecture 3 74

Softmax v CS 6501 Lecture 3 74

Example S(dog)=. 5; s(cat)=1; s(mouse)=0. 3; s(duck)=-0. 2 1 0. 8 0. 6 0.

Example S(dog)=. 5; s(cat)=1; s(mouse)=0. 3; s(duck)=-0. 2 1 0. 8 0. 6 0. 4 0. 2 0 Dog -0. 2 Cat Mouse (hard) max (�� → 0) softmax (�� =0. 5) Duck -0. 4 score softmax CS 6501 Lecture 3 softmax (�� =2) 75

Log linear model v Linear function Except this term CS 6501 Lecture 3 76

Log linear model v Linear function Except this term CS 6501 Lecture 3 76

Maximum log-likelihood estimation v CS 6501 Lecture 3 77

Maximum log-likelihood estimation v CS 6501 Lecture 3 77

Maximum a posteriori v Can you use Kesler construction to rewrite this formulation? CS

Maximum a posteriori v Can you use Kesler construction to rewrite this formulation? CS 6501 Lecture 3 78

Comparisons v CS 6501 Lecture 3 79

Comparisons v CS 6501 Lecture 3 79