Top Banner
Introduction of SVM struct Hung-yi Lee
14

Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

May 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

Introduction of SVMstruct

Hung-yi Lee

Page 2: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

SVMstruct

β€’ API for implementing different kinds of structured learning tasks by structured SVMβ€’ C:

https://www.cs.cornell.edu/people/tj/svm_light/svm_struct.html

β€’ Python: http://tfinley.net/software/svmpython2/

β€’ MATLAB: http://www.robots.ox.ac.uk/~vedaldi/svmstruct.html

β€’ Structured SVM with Hidden Informationβ€’ http://www.cs.cornell.edu/~cnyu/latentssvm/

Page 3: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

SVMstruct

β€’ Python version in these slides

β€’ Download from http://tfinley.net/software/svmpython2/svm-python-v204.tgz

β€’ To build this, a simple make should work.

β€’ svm_python_learn: for learning a model and

β€’ svm_python_classify: for testing with a learned model

Page 4: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

svm_python_learn

π‘₯1, 𝑦1 , π‘₯2, 𝑦2 , β‹― , π‘₯𝑁, 𝑦𝑁 = read_examples(TrainingData)

init_model()

w ← 0, working set 𝔸1 ← 𝑛𝑒𝑙𝑙, 𝔸2 ← 𝑛𝑒𝑙𝑙,β‹― , 𝔸𝑁 ← 𝑛𝑒𝑙𝑙Repeat

𝑀 ←Solve a QP with Working Set 𝔸1, 𝔸2, β‹― , 𝔸𝑁

argmax𝑦

βˆ† 𝑦𝑛, 𝑦 + 𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦

𝑦𝑛 =

For each training data:

Until the stopping criterion fulfilled

Return w

Update working set 𝔸𝑛 ← 𝔸𝑛 βˆͺ 𝑦𝑛

What you have to implement by yourself

find_most_violated_constraint(π‘₯𝑛, 𝑦𝑛)

Page 5: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

svm_python_learn - QP

Find 𝑀, πœ€1β€¦πœ€π‘ minimizing 1

2𝑀 2 + πœ†

𝑛=1

𝑁

πœ€π‘›QP:

𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦𝑛 βˆ’ πœ™ π‘₯𝑛, 𝑦 β‰₯ βˆ† 𝑦𝑛, 𝑦 βˆ’πœ€π‘› , πœ€π‘› β‰₯ 0

For βˆ€π‘¦ ∈ 𝔸𝑛:

For βˆ€π‘›:

psi(π‘₯, 𝑦) loss( 𝑦𝑛, 𝑦)

Usage: ./svm_python_learn –c 1000 TrainingData Model

-e value (smaller, training time longer)

-k int (number of new constraints to accumulate before recomputing the QP)

Page 6: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

svm_python_classify

Usage: ./svm_python_classify TestingData Model Result

For each test data:

𝑦′ ← πΌπ‘›π‘“π‘’π‘Ÿπ‘’π‘›π‘π‘’ π‘₯β€², 𝑀classify_example (π‘₯𝑛) Inference

argmax𝑦

𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦

Page 7: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

svmstruct.py

β€’ Put your code in svmstruct.py

Using multi-class classification to show how to implement each function

http://tfinley.net/software/svmpython2/multiclass_code.html

read_examples(filename)

init_model()

find_most_violated_constraint(π‘₯𝑛, 𝑦𝑛)

psi(π‘₯, 𝑦) loss( 𝑦𝑛, 𝑦)

classify_example (π‘₯𝑛)

Page 8: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

read_examplesπ‘₯1, 𝑦1 , π‘₯2, 𝑦2 , β‹― , π‘₯𝑁, 𝑦𝑁 = read_examples(TrainingData)

Page 9: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

read_examples

β€’ Format of β€œexamples” (training data)

β€’ The whole β€œexamples” (training data) is a list

β€’ Each data is a tuple with two element

𝑒π‘₯π‘Žπ‘šπ‘π‘™π‘’π‘  = [ π‘₯1, 𝑦1 , … π‘₯𝑛, 𝑦𝑛 , … π‘₯𝑁, 𝑦𝑁 ]

Input object output object

a scalar

[(3,6.55),(7,-1.01),…(10,0.4)]

Sparse representation of a vector

Page 10: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

init_model

β€’ Tell SVMstruct the feature dimension of w

e.g. K classes, feature representation of x is M dimensions

sm.size_psi = K * M

feature dimension of w

Model

Page 11: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

loss

β€’ βˆ† 𝑦, 𝑦

Task dependent

Page 12: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

[(3+(k-1)*M,6.55),(7+(k-1)*M,-1.01),…(10+(k-1)*M,0.4)]

psi

0

0

π‘₯

β‹―β‹―

πœ™ π‘₯, 𝑦 =

𝐾 Γ— 𝑀 dims

K blocks

Each has M dimensions

[(3,6.55),(7,-1.01),…(10,0.4)]

vec = svmapi.Sparse( )

return vec

If x is class k, put π‘₯ at kth block

y = k (class k)

x:

0 for other dimensions

Page 13: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

find_most_violated_constraint 𝑦𝑛 = argmax

π‘¦βˆ† 𝑦𝑛, 𝑦 + 𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦

w = sm.w

vec = psi(x,y)

for k, v in vec: The value at k dimension is v.

score = 0

score = score + v * w[k]

𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦

Page 14: Introduction of SVMstruct Hung-yi Leespeech.ee.ntu.edu.tw/~tlkagk/courses/MLDS_2015_2/Lecture/Structu… · svm_python_learn 1, 1, 2, 2,β‹―, 𝑁, 𝑁 =read_examples(TrainingData)

classify_example

For each test data:

𝑦′ ← πΌπ‘›π‘“π‘’π‘Ÿπ‘’π‘›π‘π‘’ π‘₯β€², 𝑀classify_example (π‘₯𝑛) Inference

argmax𝑦

𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦

𝑦𝑛 = argmax𝑦

βˆ† 𝑦𝑛, 𝑦 + 𝑀 βˆ™ πœ™ π‘₯𝑛, 𝑦