Linear Models for Classification: Discriminative Learning (Perceptron, SVMs, MaxEnt)

Similar documents
More Smoothing, Tuning, and Evaluation

Machine Learning for NLP

Logistic Regression & Neural Networks

Introduction to Logistic Regression and Support Vector Machine

Logistic Regression. Machine Learning Fall 2018

Machine Learning for NLP

Statistical Data Mining and Machine Learning Hilary Term 2016

Logistic Regression. COMP 527 Danushka Bollegala

Applied Machine Learning Lecture 5: Linear classifiers, continued. Richard Johansson

Logistic Regression Introduction to Machine Learning. Matt Gormley Lecture 9 Sep. 26, 2018

Logistic Regression Introduction to Machine Learning. Matt Gormley Lecture 8 Feb. 12, 2018

Support vector machines Lecture 4

Lecture 13: Discriminative Sequence Models (MEMM and Struct. Perceptron)

Natural Language Processing. Classification. Features. Some Definitions. Classification. Feature Vectors. Classification I. Dan Klein UC Berkeley

Machine Learning for Structured Prediction

Machine Learning and Data Mining. Linear classification. Kalev Kask

Discriminative Models

Support Vector Machines: Training with Stochastic Gradient Descent. Machine Learning Fall 2017

Ad Placement Strategies

Natural Language Processing (CSEP 517): Text Classification

Gaussian and Linear Discriminant Analysis; Multiclass Classification

Log-Linear Models, MEMMs, and CRFs

Case Study 1: Estimating Click Probabilities. Kakade Announcements: Project Proposals: due this Friday!

Classification Logistic Regression

Discriminative Models

Logistic Regression Logistic

Classification CE-717: Machine Learning Sharif University of Technology. M. Soleymani Fall 2012

Midterm Review CS 6375: Machine Learning. Vibhav Gogate The University of Texas at Dallas

Introduction to Machine Learning

Overfitting, Bias / Variance Analysis

Max Margin-Classifier

Machine Learning (CSE 446): Neural Networks

UNIVERSITY of PENNSYLVANIA CIS 520: Machine Learning Final, Fall 2013

DATA MINING AND MACHINE LEARNING

Generative Learning. INFO-4604, Applied Machine Learning University of Colorado Boulder. November 29, 2018 Prof. Michael Paul

Midterm Review CS 7301: Advanced Machine Learning. Vibhav Gogate The University of Texas at Dallas

Kernelized Perceptron Support Vector Machines

Mark your answers ON THE EXAM ITSELF. If you are not sure of your answer you may wish to provide a brief explanation.

Classification: Naïve Bayes. Nathan Schneider (slides adapted from Chris Dyer, Noah Smith, et al.) ENLP 19 September 2016

Classification objectives COMS 4771

Logistic Regression Review Fall 2012 Recitation. September 25, 2012 TA: Selen Uguroglu

MIRA, SVM, k-nn. Lirong Xia

CS 188: Artificial Intelligence Fall 2008

General Naïve Bayes. CS 188: Artificial Intelligence Fall Example: Overfitting. Example: OCR. Example: Spam Filtering. Example: Spam Filtering

CPSC 340: Machine Learning and Data Mining. MLE and MAP Fall 2017

Machine Learning Practice Page 2 of 2 10/28/13

Warm up: risk prediction with logistic regression

CS221 / Autumn 2017 / Liang & Ermon. Lecture 2: Machine learning I

Warm up. Regrade requests submitted directly in Gradescope, do not instructors.

CPSC 340: Machine Learning and Data Mining

Neural Networks and Deep Learning

Logistic Regression. Robot Image Credit: Viktoriya Sukhanova 123RF.com

Support Vector Machines. Machine Learning Fall 2017

NLP Programming Tutorial 6 - Advanced Discriminative Learning

Machine learning comes from Bayesian decision theory in statistics. There we want to minimize the expected value of the loss function.

Linear & nonlinear classifiers

Logistic Regression. William Cohen

Announcements - Homework

Neural Networks. David Rosenberg. July 26, New York University. David Rosenberg (New York University) DS-GA 1003 July 26, / 35

ECE521 Lecture7. Logistic Regression

Support Vector Machines

Lecture 2 - Learning Binary & Multi-class Classifiers from Labelled Training Data

Linear and Logistic Regression. Dr. Xiaowei Huang

CSC321 Lecture 18: Learning Probabilistic Models

Logistic Regression: Online, Lazy, Kernelized, Sequential, etc.

Statistical NLP Spring A Discriminative Approach

Lecture 9: Large Margin Classifiers. Linear Support Vector Machines

10-701/ Machine Learning - Midterm Exam, Fall 2010

Machine Learning (CS 567) Lecture 3

UNIVERSITY of PENNSYLVANIA CIS 520: Machine Learning Final, Fall 2014

Introduction to Machine Learning

Stochastic Gradient Descent

Introduction to Gaussian Process

COMS 4771 Introduction to Machine Learning. James McInerney Adapted from slides by Nakul Verma

Probabilistic classification CE-717: Machine Learning Sharif University of Technology. M. Soleymani Fall 2016

CS 188: Artificial Intelligence. Outline

INTRODUCTION TO DATA SCIENCE

Maximum Entropy Klassifikator; Klassifikation mit Scikit-Learn

PAC-learning, VC Dimension and Margin-based Bounds

Algorithms for NLP. Classification II. Taylor Berg-Kirkpatrick CMU Slides: Dan Klein UC Berkeley

Is the test error unbiased for these programs?

NONLINEAR CLASSIFICATION AND REGRESSION. J. Elder CSE 4404/5327 Introduction to Machine Learning and Pattern Recognition

Ad Placement Strategies

Machine Learning and Computational Statistics, Spring 2017 Homework 2: Lasso Regression

Machine Learning Linear Classification. Prof. Matteo Matteucci

Introduction to Logistic Regression

18.9 SUPPORT VECTOR MACHINES

EE 511 Online Learning Perceptron

Classification Based on Probability

From perceptrons to word embeddings. Simon Šuster University of Groningen

CSC 411 Lecture 17: Support Vector Machine

Deep Learning for Computer Vision

Machine Learning CSE546 Carlos Guestrin University of Washington. October 7, Efficiency: If size(w) = 100B, each prediction is expensive:

Naïve Bayes Introduction to Machine Learning. Matt Gormley Lecture 18 Oct. 31, 2018

Evaluation. Andrea Passerini Machine Learning. Evaluation

Classification: The rest of the story

Performance Evaluation

1 Machine Learning Concepts (16 points)

Tufts COMP 135: Introduction to Machine Learning

Introduction to Machine Learning Midterm, Tues April 8

Transcription:

Linear Models for Classification: Discriminative Learning (Perceptron, SVMs, MaxEnt) Nathan Schneider (some slides borrowed from Chris Dyer) ENLP 12 February 2018 23

Outline Words, probabilities Features, weights Geometric view: decision boundary Perceptron previous lecture this lecture Generative vs. Discriminative More discriminative models: Logistic regression/maxent; SVM Loss functions, optimization Regularization; sparsity 24

X Y Perceptron Learner w 0 for i = 1 I: for t = 1 T: select (x, y) t # run current classifier ŷ arg max y w y ᵀ Φ(x) w if ŷ y then # mistake w y w y + Φ(x) w ŷ w ŷ Φ(x) return w L (assumes all classes have the same percepts) 25

X Y Perceptron Learner w 0 for i = 1 I: for t = 1 T: select (x, y) t # run current classifier ŷ C x decoding is a subroutine of learning w if ŷ y then # mistake w y w y + Φ(x) w ŷ w ŷ Φ(x) return w L (assumes all classes have the same percepts) 26

X Y Perceptron Learner w 0 for i = 1 I: for t = 1 T: select (x, y) t for binary classification single weight vector such that >0 + class, <0 class # run current classifier ŷ sign(wᵀ Φ(x)) w if ŷ y then # mistake w w + sign(y) Φ(x) return w L (assumes all classes have the same percepts) 27

X Y Perceptron Learner w 0 for i = 1 I: for t = 1 T: select (x, y) t # run current classifier ŷ arg max y wᵀ Φ(x, y ) if ŷ y then # mistake if different classes have different percepts w w + Φ(x, y) Φ(x, ŷ) return w L w 28

work through example on the board x1 = I thought it was great x2 = not so great x3 = good but not great y1 = + y2 = y3 = + 29

Perceptron Learner The perceptron doesn t estimate probabilities. It just adjusts weights up or down until they classify the training data correctly. No assumptions of feature independence necessary! Better accuracy than NB The perceptron is an example of an online learning algorithm because it potentially updates its parameters (weights) with each training datapoint. Classification, a.k.a. decoding, is called with the latest weight vector. Mistakes lead to weight updates. One hyperparameter: I, the number of iterations (passes through the training data). Often desirable to make several passes over the training data. The number can be tuned. Under certain assumptions, it can be proven that the learner will converge. 30

Perceptron: Avoiding overfitting Like any learning algorithm, the perceptron risks overfitting the training data. Two main techniques to improve generalization: Averaging: Keep a copy of each weight vector as it changes, then average all of them to produce the final weight vector. Daumé chapter has a trick to make this efficient with large numbers of features. Early stopping: Tune I by checking held-out accuracy on dev data (or cross-val on train data) after each iteration. If accuracy has ceased to improve, stop training and use the model from iteration I 1. 31

Generative vs. Discriminative Naïve Bayes allows us to classify via the joint probability of x and y: p(y x) p(y) Π w x p(w y) = p(y) p(x y) (per the independence assumptions of the model) = p(y, x) (chain rule) This means the model accounts for BOTH x and y. From the joint distribution p(x,y) it is possible to compute p(x) as well as p(y), p(x y), and p(y x). NB is called a generative model because it assigns probability to linguistic objects (x). It could be used to generate likely language corresponding to some y. (Subject to its naïve modeling assumptions!) (Not to be confused with the generative school of linguistics.) Some other linear models, including the perceptron, are discriminative: they are trained directly to classify given x, and cannot be used to estimate the probability of x or generate x y. 32

Many possible decision boundaries? Which one is best?? x y C 33

Max-Margin Methods (e.g., SVM) { margin Choose decision boundary that is halfway between nearest positive and negative examples x y C 34

Max-Margin Methods Support Vector Machine (SVM): most popular max-margin variant Closely related to the perceptron; can be optimized (learned) with a slight tweak to the perceptron algorithm. Like perceptron, discriminative, nonprobabilistic. 35

Maximum Entropy (MaxEnt) a.k.a. (Multinomial) Logistic Regression What if we want a discriminative classifier with probabilities? E.g., need confidence of prediction, or want the full distribution over possible classes Wrap the linear score computation (wᵀ Φ(x, y )) in the softmax function: score can be negative; exp(score) is always positive log p(y x) = log exp(wᵀ Φ(x, y)) = wᵀ Φ(x, y) log Σ y exp(wᵀ Φ(x, y )) log Σ y exp(wᵀ Φ(x, y )) Denominator = normalization (makes probabilities sum to 1). Binary case: Sum over all classes same for all numerators can be ignored at classification time. log p(y=1 x) = log exp(wᵀ Φ(x, y=1)) log exp(wᵀ Φ(x, y=1)) + exp(wᵀ Φ(x, y=0)) = log exp(wᵀ Φ(x, y=1)) (fixing wᵀ Φ(x, y=0) = 0) log exp(wᵀ Φ(x, y=1)) + 1 MaxEnt classifiers are a special case of MaxEnt a.k.a. log-linear models. Why the term Maximum Entropy? See Smith Linguistic Structure Prediction, appendix C. 36

Objectives For all linear models, the classification rule or decoding objective is: y arg max y wᵀ Φ(x, y ) Objective function = function for which we want to find the optimum (in this case, the max) There is also a learning objective for which we want to find the optimal parameters. Mathematically, NB, MaxEnt, SVM, and perceptron all optimize different learning objectives. When the learning objective is formulated as a minimization problem, it s called a loss function. A loss function scores the badness of the training data under any possible set of parameters. Learning = choosing the parameters that minimize the badness. 37

Objectives Naïve Bayes learning objective: joint data likelihood p* arg max p L joint (D; p) = arg max p Σ (x, y) D log p(x,y) = arg max p Σ (x, y) D log (p(y)p(x y)) It can be shown that relative frequency estimation (i.e., count and divide, no smoothing) is indeed the maximum likelihood estimate MaxEnt learning objective: conditional log likelihood p* arg max p L cond (D; p) = arg max p Σ (x, y) D log p(y x) w arg max w Σ (x, y) D wᵀ Φ(x, y) log Σ y exp(wᵀ Φ(x, y )) [2 slides ago] This has no closed-form solution. Hence, we need an optimization algorithm to try different weight vectors and choose the best one. With thousands or millions of parameters not uncommon in NLP it may also overfit. 38

Objectives Visualizing different loss functions for binary classification worse 0 1 loss (error) loss 0 1 2 3 4 5 better log loss (MaxEnt) 4 2 0 2 4 score wᵀ Φ(x, y ) hinge loss (perceptron) figure from Noah Smith 39

Objectives Why not just penalize error directly if that s how we re going to evaluate our classifier (accuracy)? Error is difficult to optimize! Log loss and hinge loss are easier. Why? Because they re differentiable. Can use stochastic (sub)gradient descent (SGD) and other gradient-based optimization algorithms (L-BFGS, AdaGrad, ). There are freely available software packages that implement these algorithms. With supervised learning, these loss functions are convex: local optimum = global optimum (so in principle the initialization of weights doesn t matter). The perceptron algorithm can be understood as a special case of subgradient descent on the hinge loss! N.B. I haven t explained the math for the hinge loss (perceptron) or the SVM. Or the derivation of gradients. See further reading links if you re interested. 40

A likelihood surface Visualizes the likelihood objective (vertical axis) as a function of 2 parameters. Likelihood = maximization problem. Flip upside down for the loss. Gradient-based optimizers choose a point on the surface, look at its curvature, and then successively move to better points. figure from Chris Manning 41

Regularization Better MaxEnt learning objective: regularized conditional log likelihood w* arg max w λr(w) + Σ (x, y) D wᵀ Φ(x, y) log Σ y exp(wᵀ Φ(x, y )) To avoid overfitting, the regularization term ( regularizer ) λr(w) penalizes complex models (i.e., parameter vectors with many large weights). Close relationship to Bayesian prior (a priori notion of what a good model looks like if there is not much training data). Note that the regularizer is a function of the weights only (not the data)! In NLP, most popular values of R(w) are the 1 norm ( Lasso ) and the 2 norm ( ridge ): 2 = w 2 = (Σ i w i ²) 1/2 encourages most weights to be small in magnitude = w 1 1 = Σ i w i encourages most weights to be 0 λ determines the tradeoff between regularization and data-fitting. Can be tuned on dev data. SVM objective also incorporates a regularization term. Perceptron does not (hence, averaging and early stopping). 42

Sparsity 1 regularization is a way to promote model sparsity: many weights are pushed to 0. A vector is sparse if (# nonzero parameters) (total # parameters). Intuition: if we define very general feature templates e.g. one feature per word in the vocabulary we expect that most features should not matter for a particular classification task. In NLP, we typically have sparsity in our feature vectors as well. E.g., in WSD, all words in the training data but not in context of a particular token being classified are effectively 0-valued features. Exception: dense word representations popular in recent neural network models (we ll get to this later in the course). Sometimes the word sparsity or sparseness just means not very much data. 43

Summary: Linear Models Classifier: y arg max y wᵀ Φ(x, y ) kind of model loss function learning algorithm avoiding overfitting Naïve Bayes Probabilistic, generative Likelihood Closed-form estimation Smoothing Logistic regression (MaxEnt) Probabilistic, discriminative Conditional likelihood Optimization Regularization penalty Perceptron Non-probabilistic, discriminative Hinge Optimization Averaging; Early stopping SVM (linear kernel) Non-probabilistic, discriminative Max-margin Optimization Regularization penalty 44

Take-home points Feature-based linear classifiers are important to NLP. You define the features, an algorithm chooses the weights. Some classifiers then exponentiate and normalize to give probabilities. More features more flexibility, also more risk of overfitting. Because we work with large vocabularies, not uncommon to have millions of features. Learning objective/loss functions formalize training as choosing parameters to optimize a function. Some model both the language and the class (generative); some directly model the class conditioned on the language (discriminative). In general: Generative training is cheaper, but lower accuracy. Discriminative higher accuracy with sufficient training data and computation (optimization). Some models, like naïve Bayes, have a closed-form solution for parameters. Learning is cheap! Other models require fancier optimization algorithms that may iterate multiple times over the data, adjusting parameters until convergence (or some other stopping criterion). The advantage: fewer modeling assumptions. Weights can be interdependent. 45

Which classifier to use? Fast and simple: naïve Bayes Very accurate, still simple: perceptron Very accurate, probabilistic, more complicated to implement: MaxEnt Potentially best accuracy, more complicated to implement: SVM All of these: watch out for overfitting! Check the web for free and fast implementations, e.g. SVM light 46

Further Reading: Basics & Examples Manning: features in linear classifiers http://www.stanford.edu/class/cs224n/handouts/maxenttutorial-16x9- FeatureClassifiers.pdf Goldwater: naïve Bayes & MaxEnt examples http://www.inf.ed.ac.uk/teaching/courses/fnlp/lectures/07_slides.pdf O Connor: MaxEnt incl. step-by-step examples, comparison to naïve Bayes http://people.cs.umass.edu/~brenocon/inlp2015/04-logreg.pdf Daumé: The Perceptron (A Course in Machine Learning, ch. 3) http://www.ciml.info/dl/v0_8/ciml-v0_8-ch03.pdf Neubig: The Perceptron Algorithm http://www.phontron.com/slides/nlp-programming-en-05-perceptron.pdf 47

Further Reading: Advanced Neubig: Advanced Discriminative Learning MaxEnt w/ derivatives, SGD, SVMs, regularization http://www.phontron.com/slides/nlp-programming-en-06- discriminative.pdf Manning: generative vs. discriminative, MaxEnt likelihood function and derivatives http://www.stanford.edu/class/cs224n/handouts/maxenttutorial-16x9- MEMMs-Smoothing.pdf, slides 3 20 Daumé: linear models http://www.ciml.info/dl/v0_8/ciml-v0_8-ch06.pdf Smith: A variety of loss functions for text classification http://courses.cs.washington.edu/courses/cse517/16wi/slides/tc-introslides.pdf & http://courses.cs.washington.edu/courses/cse517/16wi/ slides/tc-advanced-slides.pdf 48

Evaluating Multiclass Classifiers and Retrieval Algorithms 49

Accuracy Assume we are disambiguating word senses such that every token has 1 gold sense label. The classifier predicts 1 label for each token in the test set. Thus, every test set token has a predicted label (pred) and a gold label (gold). The accuracy of our classifier is just the % of tokens for which the predicted label matched the gold label: # pred=gold /# tokens 50

Precision and Recall To measure the classifier with respect to a certain label y, and there are >2, we distinguish precision and recall: precision = proportion of times the label was predicted and that prediction matched the gold: # pred=gold=y /# pred=y recall = proportion of times the label was in the gold standard and was recovered correctly by the classifier: # pred=gold=y /# gold=y The harmonic mean of precision and recall, called F 1 - score, balances between the two. F 1 = 2*precision*recall / (precision + recall) 51

Evaluating Retrieval Systems Precision/Recall/F-score are also useful for evaluating retrieval systems. E.g., consider a system which takes a word as input and is supposed to retrieve all rhymes. Now, for a single input (the query), there are often many correct outputs. Precision tells us whether most of the given outputs were valid rhymes; recall tells us whether most of the valid rhymes in the gold standard were recovered. 52

Rhymes for hinge Gold System klinge minge vinje binge cringe fringe hinge impinge infringe syringe tinge twinge unhinge ainge 53

Rhymes for hinge Gold System klinge minge vinje binge cringe fringe hinge impinge infringe syringe tinge twinge unhinge False Positive (Type I error) ainge 54

Rhymes for hinge False Negative (Type II error) Gold System False Positive (Type I error) klinge minge vinje binge cringe fringe hinge impinge infringe syringe tinge twinge unhinge ainge 55

Rhymes for hinge False Negative (Type II error) klinge minge vinje Gold binge cringe fringe hinge impinge infringe syringe tinge twinge unhinge System False Positive (Type I error) ainge Sys=Y Sys=N Gold=Y 10 3 Gold=N 1 (large) Correctly predicted = True Positive All other words = True Negative 56

Precision & Recall False Negative (Type II error) klinge minge vinje Gold binge cringe fringe hinge impinge infringe syringe tinge twinge unhinge System False Positive (Type I error) ainge Sys=Y Sys=N Gold=Y 10 3 Gold=N 1 (large) Precision = TP/(TP+FP) = 10/11 = 91% Recall = TP/(TP+FN) = 10/13 = 77% F1 = 2 P R/(P+R) = 83% Correctly predicted = True Positive All other words = True Negative 57