Support Vector Machines Text classification Last time and

  • Slides: 53
Download presentation
Support Vector Machines 支持向量机

Support Vector Machines 支持向量机

Text classification: Last time and today n Last time: 3 algorithms for text classification

Text classification: Last time and today n Last time: 3 algorithms for text classification n K Nearest Neighbor classification n n Bayesian classification n n Simple, using historical data to judge the class of a new object. Decision Trees n n Simple, expensive at test time, high variance, non-linear Pick out hyperboxes; nonlinear; use just a few features Today n n n SVMs Some empirical evaluation and comparison Text-specific issues in classification

Linear classifiers: Which Hyperplane? n n n Lots of possible solutions for a, b,

Linear classifiers: Which Hyperplane? n n n Lots of possible solutions for a, b, c. Some methods find a separating hyperplane, but not the optimal one [according to some criterion of expected goodness] n E. g. , perceptron Support Vector Machine (SVM) finds an optimal solution. n Maximizes the distance between the hyperplane and the “difficult points” close to decision boundary n One intuition: if there are no points near the decision surface, then there are no very uncertain classification decisions This line represents the decision boundary: ax + by - c = 0

Another intuition (non-unique) n If you have to place a fat separator between classes,

Another intuition (non-unique) n If you have to place a fat separator between classes, you have less choices, and so the capacity of the model has been decreased

assuption n The training sample set n n n (x 1, y 1), …(xl,

assuption n The training sample set n n n (x 1, y 1), …(xl, yl) x Rn, y {-1, +1} WTX=0, x 超平面

Support Vector Machine (SVM) n SVMs maximize the margin around the separating hyperplane. n

Support Vector Machine (SVM) n SVMs maximize the margin around the separating hyperplane. n n Support vectors A. k. a. large margin classifiers The decision function is fully specified by a subset of training samples, the support vectors. Quadratic (2次)programming problem Seen by many as most successful current text classification method Maximize margin

Maximum Margin: Formalization n w: decision hyperplane normal (法线) xi: data point i yi:

Maximum Margin: Formalization n w: decision hyperplane normal (法线) xi: data point i yi: class of data point i (+1 or -1) (并不是 1/0) n Classifier is: n Functional margin of xi is: n n n f(xi) = sign(w. Txi + b) yi (w. Txi + b) But note that we can increase this margin simply by scaling w, b…. Functional margin of dataset is minimum functional margin for any point b is the distance from data set to the plan.

x r x’ = x – r (w/||w|| ) (1) X’ Where x’ is

x r x’ = x – r (w/||w|| ) (1) X’ Where x’ is the point in the plan Then WTx’+b = 0 (2) 带入 1到 2 w. T(x- r(w/||w||)+b = 0 w. Tx – r (w. Tw/||w||)+b = 0 r = y (w. Tx+b)/||w|| note

Geometric Margin n Distance from example to the separator is n Examples closest to

Geometric Margin n Distance from example to the separator is n Examples closest to the hyperplane are support vectors. n Margin ρ of the separator is the width of separation between support vectors of classes. ρ x r x′

Linear SVM Mathematically n Assume that all data is at least distance 1 from

Linear SVM Mathematically n Assume that all data is at least distance 1 from the hyperplane, then the following two constraints follow for a training set {(xi , yi)} w. Txi + b ≥ 1 if yi = 1 w. Txi + b ≤ -1 if yi = -1 n For support vectors, the inequality becomes an equality Then, since each example’s distance from the hyperplane is n The margin is: n

Bi 1 : WTx 1+b = 1; Bi 2: WTx 2 + b =

Bi 1 : WTx 1+b = 1; Bi 2: WTx 2 + b = -1; 2= WT (x 1 -x 2) since ||x 1 – x 2 || 2 = ) = 2/||W||

Linear Support Vector Machine (SVM) ρ Hyperplane w. T x + b = 0

Linear Support Vector Machine (SVM) ρ Hyperplane w. T x + b = 0 n w. T x a + b = 1 w. Txb + b = -1 Extra scale constraint: mini=1, …, n |w. Txi + b| = 1 n This implies: w. T(xa–xb) = 2 ρ = ||xa–xb||2 = 2/||w||2 n w. T x + b = 0

Linear SVMs Mathematically (cont. ) n Then we can formulate the quadratic optimization problem:

Linear SVMs Mathematically (cont. ) n Then we can formulate the quadratic optimization problem: Find w and b such that is maximized; and for all {(xi , yi)} w. Txi + b ≥ 1 if yi=1; w. Txi + b ≤ -1 if yi = -1 n A better formulation (min ||w|| = max 1/ ||w|| ): Find w and b such that Φ(w) =½ w. Tw is minimized; and for all {(xi , yi)}: yi (w. Txi + b) ≥ 1

Solving the Optimization Problem Find w and b such that Φ(w) =½ w. Tw

Solving the Optimization Problem Find w and b such that Φ(w) =½ w. Tw is minimized; and for all {(xi , yi)}: yi (w. Txi + b) ≥ 1 n n n 2次方程 This is now optimizing a quadratic function subject to linear constraints Quadratic optimization problems are a well-known class of mathematical programming problems, and many (rather intricate) algorithms exist for solving them The solution involves constructing a dual problem where a Lagrange multiplier αi is associated with every constraint in the primary problem: 求偏导数 Find α 1…αN such that Q(α) =Σαi - ½ΣΣαiαjyiyjxi. Txj is maximized and (1) Σαiyi = 0 (2) αi ≥ 0 for all αi

The Optimization Problem Solution n The solution has the form: w =Σαiyixi n n

The Optimization Problem Solution n The solution has the form: w =Σαiyixi n n b= yk- w. Txk for any xk such that αk 0 Each non-zero αi indicates that corresponding xi is a support vector. Then the classifying function will have the form: f(x) = Σαiyixi. Tx + b n n Notice that it relies on an inner product between the test point x and the support vectors xi – we will return to this later. Also keep in mind that solving the optimization problem involved computing the inner products xi. Txj between all pairs of training points.

Soft Margin Classification n n n If the training set is not linearly separable,

Soft Margin Classification n n n If the training set is not linearly separable, slack variables ξi can be added to allow misclassification of difficult or noisy examples. Allow some errors n Let some points be moved to where they belong, at a cost Still, try to minimize training set errors, and to place hyperplane “far” from each class (large margin) ξi ξj

Soft Margin Classification Mathematically n The old formulation: Find w and b such that

Soft Margin Classification Mathematically n The old formulation: Find w and b such that Φ(w) =½ w. Tw is minimized and for all {(xi yi (w. Txi + b) ≥ 1 n , yi)} The new formulation incorporating slack variables: Find w and b such that Φ(w) =½ w. Tw + CΣξi is minimized and for all {(xi yi (w. Txi + b) ≥ 1 - ξi and ξi ≥ 0 for all i n , yi)} Parameter C can be viewed as a way to control overfitting – a regularization term

Soft Margin Classification – Solution n The dual problem for soft margin classification: Find

Soft Margin Classification – Solution n The dual problem for soft margin classification: Find α 1…αN such that Q(α) =Σαi - ½ΣΣαiαjyiyjxi. Txj is maximized and (1) Σαiyi = 0 (2) 0 ≤ αi ≤ C for all αi n n n Neither slack variables ξi nor their Lagrange multipliers appear in the dual problem! Again, xi with non-zero αi will be support vectors. Solution to the dual problem is: w =Σαiyixi b= yk(1 - ξk) - w. Txk where k = argmax αk k But w not needed explicitly for classification! f(x) = Σαiyixi. Tx + b

Classification with SVMs n Given a new point (x 1, x 2), we can

Classification with SVMs n Given a new point (x 1, x 2), we can score its projection onto the hyperplane normal: n n n In 2 dims: score = w 1 x 1+w 2 x 2+b. I. e. , compute score: wx + b = Σαiyixi. Tx + b Set confidence threshold t. Score > t: yes Score < -t: no Else: don’t know 3 5 7

Linear SVMs: Summary n n The classifier is a separating hyperplane. Most “important” training

Linear SVMs: Summary n n The classifier is a separating hyperplane. Most “important” training points are support vectors; they define the hyperplane. Quadratic optimization algorithms can identify which training points xi are support vectors with non-zero Lagrangian multipliers αi. Both in the dual formulation of the problem and in the solution training points appear only inside inner products: Find α 1…αN such that Q(α) =Σαi - ½ΣΣαiαjyiyjxi. Txj is maximized and (1) Σαiyi = 0 (2) 0 ≤ αi ≤ C for all αi f(x) = Σαiyixi. Tx + b

Non-linear SVMs n Datasets that are linearly separable (with some noise) work out great:

Non-linear SVMs n Datasets that are linearly separable (with some noise) work out great: x 0 n But what are we going to do if the dataset is just too hard? n x 0 How about … mapping data to a higher-dimensional space: x 2 0 x

Non-linear SVMs: Feature spaces n General idea: the original feature space can always be

Non-linear SVMs: Feature spaces n General idea: the original feature space can always be mapped to some higher-dimensional feature space where the training set is separable: Φ: x → φ(x)

The “Kernel Trick” n n The linear classifier relies on an inner product between

The “Kernel Trick” n n The linear classifier relies on an inner product between vectors K(xi, xj)=xi. Txj If every datapoint is mapped into high-dimensional space via some transformation Φ: x → φ(x), the inner product becomes: K(xi, xj)= φ(xi) Tφ(xj) A kernel function is some function that corresponds to an inner product in some expanded feature space. Example: 2 -dimensional vectors x=[x 1 x 2]; let K(xi, xj)=(1 + xi. Txj)2, Need to show that K(xi, xj)= φ(xi) Tφ(xj): K(xi, xj)=(1 + xi. Txj)2, = 1+ xi 12 xj 12 + 2 xi 1 xj 1 xi 2 xj 2+ xi 22 xj 22 + 2 xi 1 xj 1 + 2 xi 2 xj 2= = [1 xi 12 √ 2 xi 1 xi 22 √ 2 xi 1 √ 2 xi 2]T [1 xj 12 √ 2 xj 1 xj 22 √ 2 xj 1 √ 2 xj 2] = φ(xi) Tφ(xj) where φ(x) = [1 x 12 √ 2 x 1 x 2 x 22 √ 2 x 1 √ 2 x 2]

Kernels n Why use kernels? n n n Make non-separable problem separable. Map data

Kernels n Why use kernels? n n n Make non-separable problem separable. Map data into better representational space Common kernels n n n Linear Polynomial K(x, z) = (1+x. Tz)d Radial basis function (infinite dimensional space)

Evaluation: Classic Reuters Data Set http: //www. daviddlewis. com/resources/testcollections/reuters 21578/ n Most (over)used data

Evaluation: Classic Reuters Data Set http: //www. daviddlewis. com/resources/testcollections/reuters 21578/ n Most (over)used data set 21578 documents 9603 training, 3299 test articles (Mod. Apte split) 118 categories n An article can be in more than one category n Learn 118 binary category distinctions Average document: about 90 types n Average number of classes assigned n n n 1. 24 for docs with at least one category Only about 10 out of 118 categories are large Common categories (#train, #test) • • • Earn (2877, 1087) Acquisitions (1650, 179) Money-fx (538, 179) Grain (433, 149) Crude (389, 189) • • • Trade (369, 119) Interest (347, 131) Ship (197, 89) Wheat (212, 71) Corn (182, 56)

Reuters Text Categorization data set (Reuters-21578) document <REUTERS TOPICS="YES" LEWISSPLIT="TRAIN" CGISPLIT="TRAINING-SET" OLDID="12981" NEWID="798"> <DATE>

Reuters Text Categorization data set (Reuters-21578) document <REUTERS TOPICS="YES" LEWISSPLIT="TRAIN" CGISPLIT="TRAINING-SET" OLDID="12981" NEWID="798"> <DATE> 2 -MAR-1987 16: 51: 43. 42</DATE> <TOPICS><D>livestock</D><D>hog</D></TOPICS> <TITLE>AMERICAN PORK CONGRESS KICKS OFF TOMORROW</TITLE> <DATELINE> CHICAGO, March 2 - </DATELINE><BODY>The American Pork Congress kicks off tomorrow, March 3, in Indianapolis with 160 of the nations pork producers from 44 member states determining industry positions on a number of issues, according to the National Pork Producers Council, NPPC. Delegates to the three day Congress will be considering 26 resolutions concerning various issues, including the future direction of farm policy and the tax law as it applies to the agriculture sector. The delegates will also debate whether to endorse concepts of a national PRV (pseudorabies virus) control and eradication program, the NPPC said. A large trade show, in conjunction with the congress, will feature the latest in technology in all areas of the industry, the NPPC added. Reuter &#3; </BODY></TEXT></REUTERS>

New Reuters: RCV 1: 810, 000 docs n Top topics in Reuters RCV 1

New Reuters: RCV 1: 810, 000 docs n Top topics in Reuters RCV 1

Per class evaluation measures n n n Recall: Fraction of docs in class i

Per class evaluation measures n n n Recall: Fraction of docs in class i classified correctly: Precision: Fraction of docs assigned class i that are actually about class i: “Correct rate”: (1 - error rate) Fraction of docs classified correctly:

Dumais et al. 1998: Reuters - Accuracy Recall: % labeled in category among those

Dumais et al. 1998: Reuters - Accuracy Recall: % labeled in category among those stories that are really in category Precision: % really in category among those stories labeled in category Break Even: (Recall + Precision) / 2

Reuters ROC - Category Grain Recall LSVM Decision Tree Naïve Bayes Find Similar Precision

Reuters ROC - Category Grain Recall LSVM Decision Tree Naïve Bayes Find Similar Precision Recall: % labeled in category among those stories that are really in category Precision: % really in category among those stories labeled in category

ROC for Category - Crude Recall LSVM Decision Tree Naïve Bayes Find Similar Precision

ROC for Category - Crude Recall LSVM Decision Tree Naïve Bayes Find Similar Precision

ROC for Category - Ship Recall LSVM Decision Tree Naïve Bayes Find Similar Precision

ROC for Category - Ship Recall LSVM Decision Tree Naïve Bayes Find Similar Precision

Results for Kernels (Joachims 1998)

Results for Kernels (Joachims 1998)

Lave is the average number of tokens per document Mave is the average vocabulary

Lave is the average number of tokens per document Mave is the average vocabulary (number of non-zero features) of a document. La = the numbers of tokens in the test document Ma = types in the test document.

Multiclass SVM n n SVMs are inherently two-classifiers Building |C| one-versus-rest classifiers, and choose

Multiclass SVM n n SVMs are inherently two-classifiers Building |C| one-versus-rest classifiers, and choose the class which classifier the test datum with the greatest margin.

Micro- vs. Macro-Averaging n n n If we have more than one class, how

Micro- vs. Macro-Averaging n n n If we have more than one class, how do we combine multiple performance measures into one quantity? Macroaveraging: Compute performance for each class, then average. Microaveraging: Collect decisions for all classes, compute contingency (相依表)table, evaluate.

微(单独-整体)宏 (整体计算) Micro- vs. Macro-Averaging: Example Class 1 Truth: yes Class 2 Truth: no

微(单独-整体)宏 (整体计算) Micro- vs. Macro-Averaging: Example Class 1 Truth: yes Class 2 Truth: no Micro. Av. Table Truth: yes Truth: no Truth: yes Truth: no Classifi 10 er: yes 10 Classifi er: yes 90 10 Classifie r: yes 100 20 Classifi 10 er: no 970 Classifi er: no 10 890 Classifie r: no 20 1860 n n n Macroaveraged precision: (0. 5 + 0. 9)/2 = 0. 7 Microaveraged precision: 100/120 =. 83 Why this difference?

Yang&Liu: SVM vs. Other Methods

Yang&Liu: SVM vs. Other Methods

Good practice department: Confusion matrix This (i, j) entry means 53 of the docs

Good practice department: Confusion matrix This (i, j) entry means 53 of the docs actually in class i were put in class j by the classifier. Actual Class assigned by classifier n 53 In a perfect classification, only the diagonal has nonzero entries

The Real World P. Jackson and I. Moulinier: Natural Language Processing for Online Applications

The Real World P. Jackson and I. Moulinier: Natural Language Processing for Online Applications n n “There is no question concerning the commercial value of being able to classify documents automatically by content. There are myriad (无数的) potential applications of such a capability for corporate Intranets, government departments, and Internet publishers” “Understanding the data is one of the keys to successful categorization, yet this is an area in which most categorization tool vendors are extremely weak. Many of the ‘one size fits all’ tools on the market have not been tested on a wide range of content types. ”

The Real World n Gee, I’m building a text classifier for real, now! What

The Real World n Gee, I’m building a text classifier for real, now! What should I do? n How much training data do you have? n n n None Very little Quite a lot A huge amount and its growing

Manually written rules n n No training data, adequate editorial staff? Never forget the

Manually written rules n n No training data, adequate editorial staff? Never forget the hand-written rules solution! n If (wheat or grain) and not (whole or bread) then n n In practice, rules get a lot bigger than this n n Can also be phrased using tf or tf. idf weights With careful crafting (human tuning on development data) performance is high: n n Categorize as grain Construe: 94% recall, 84% precision over 675 categories (Hayes and Weinstein 1990) Amount of work required is huge n Estimate 2 days per class … plus maintenance

Very little data? n If you’re just doing supervised classification, you should stick to

Very little data? n If you’re just doing supervised classification, you should stick to something high bias n n The interesting theoretical answer is to explore semi-supervised training methods: n n There are theoretical results that Naïve Bayes should do well in such circumstances (Ng and Jordan 2002 NIPS) Bootstrapping, EM over unlabeled documents, … The practical answer is to get more labeled data as soon as you can n How can you insert yourself into a process where humans will be willing to label data for you? ?

A reasonable amount of data? n n Perfect! We can use all our clever

A reasonable amount of data? n n Perfect! We can use all our clever classifiers Roll out the SVM! But if you are using an SVM/NB etc. , you should probably be prepared with the “hybrid” solution where there is a Boolean overlay n n Or else to user-interpretable Boolean-like models like decision trees Users like to hack, and management likes to be able to implement quick fixes immediately

A huge amount of data? n This is great in theory for doing accurate

A huge amount of data? n This is great in theory for doing accurate classification… But it could easily mean that expensive methods like SVMs (train time) or k. NN (test time) are quite impractical n Naïve Bayes can come back into its own again! n n Or other advanced methods with linear training/test complexity like regularized logistic regression (though much more expensive to train)

A huge amount of data? n With enough data the choice of classifier may

A huge amount of data? n With enough data the choice of classifier may not matter much, and the best choice may be unclear n n Data: Brill and Banko on context-sensitive spelling correction But the fact that you have to keep doubling your data to improve performance is a little unpleasant

How many categories? n A few (well separated ones)? n n Easy! A zillion

How many categories? n A few (well separated ones)? n n Easy! A zillion (巨量) closely related ones? n n Think: Yahoo! Directory, Library of Congress classification, legal applications Quickly gets difficult! n Classifier combination is always a useful technique n n Much literature on hierarchical classification n n Voting, bagging, or boosting multiple classifiers Mileage fairly unclear May need a hybrid automatic/manual solution

How can one tweak(微调) performance? n Aim to exploit any domain-specific useful features that

How can one tweak(微调) performance? n Aim to exploit any domain-specific useful features that give special meanings or that zone the data n n E. g. , an author byline (署名)or mail headers Aim to collapse things that would be treated as different but shouldn’t be. n E. g. , part numbers, chemical formulas

Text Summarization techniques in text classification n n Text Summarization: Process of extracting key

Text Summarization techniques in text classification n n Text Summarization: Process of extracting key pieces from text, normally by features on sentences reflecting position and content Much of this work can be used to suggest weightings for terms in text categorization n n See: Kolcz, Prabakarmurthi, and Kolita, CIKM 2001: Summarization as feature selection for text categorization Categorizing purely with title, Categorizing with first paragraph only Categorizing with paragraph with most keywords Categorizing with first and last paragraphs, etc.

Measuring Classification Figures of Merit n Not just accuracy; in the real world, there

Measuring Classification Figures of Merit n Not just accuracy; in the real world, there are economic measures: n Your choices are: n Do no classification n n Do it all manually n n n Has an easy to compute cost if doing it like that now Do it all with an automatic classifier n n That has a cost (hard to compute) Mistakes have a cost Do it with a combination of automatic classification and manual review of uncertain/difficult/“new” cases Commonly the last method is most cost efficient and is adopted

Summary n Support vector machines (SVM) n Choose hyperplane based on support vectors n

Summary n Support vector machines (SVM) n Choose hyperplane based on support vectors n n (Degree-1) SVMs are linear classifiers. Kernels: powerful and elegant way to define similarity metric Perhaps best performing text classifier n n But there are other methods that perform about as well as SVM, such as regularized logistic regression (Zhang & Oles 2001) Partly popular due to availability of SVMlight n n Support vector = “critical” point close to decision boundary SVMlight is accurate and fast – and free (for research) Now lots of software: libsvm, Tiny. SVM, …. Comparative evaluation of methods Real world: exploit domain specific structure!

Resources n n n n n A Tutorial on Support Vector Machines for Pattern

Resources n n n n n A Tutorial on Support Vector Machines for Pattern Recognition (1998) Christopher J. C. Burges S. T. Dumais, Using SVMs for text categorization, IEEE Intelligent Systems, 13(4), Jul/Aug 1998 S. T. Dumais, J. Platt, D. Heckerman and M. Sahami. 1998. Inductive learning algorithms and representations for text categorization. CIKM ’ 98, pp. 148 -155. A re-examination of text categorization methods (1999) Yiming Yang, Xin Liu 22 nd Annual International SIGIR Tong Zhang, Frank J. Oles: Text Categorization Based on Regularized Linear Classification Methods. Information Retrieval 4(1): 5 -31 (2001) Trevor Hastie, Robert Tibshirani and Jerome Friedman, "Elements of Statistical Learning: Data Mining, Inference and Prediction" Springer. Verlag, New York. ‘Classic’ Reuters data set: http: //www. daviddlewis. com /resources /testcollections/reuters 21578/ T. Joachims, Learning to Classify Text using Support Vector Machines. Kluwer, 2002. Fan Li, Yiming Yang: A Loss Function Analysis for Classification Methods in Text Categorization. ICML 2003: 472 -479.