introduction of svmstruct hung-yi...

14
Introduction of SVM struct Hung-yi Lee

Upload: others

Post on 23-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Introduction of SVMstruct

Hung-yi Lee

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/

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

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(๐‘ฅ๐‘›, ๐‘ฆ๐‘›)

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)

svm_python_classify

Usage: ./svm_python_classify TestingData Model Result

For each test data:

๐‘ฆโ€ฒ โ† ๐ผ๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’ ๐‘ฅโ€ฒ, ๐‘คclassify_example (๐‘ฅ๐‘›) Inference

argmax๐‘ฆ

๐‘ค โˆ™ ๐œ™ ๐‘ฅ๐‘›, ๐‘ฆ

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 (๐‘ฅ๐‘›)

read_examples๐‘ฅ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

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

loss

โ€ข โˆ† ๐‘ฆ, ๐‘ฆ

Task dependent

[(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

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]

๐‘ค โˆ™ ๐œ™ ๐‘ฅ๐‘›, ๐‘ฆ

classify_example

For each test data:

๐‘ฆโ€ฒ โ† ๐ผ๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’ ๐‘ฅโ€ฒ, ๐‘คclassify_example (๐‘ฅ๐‘›) Inference

argmax๐‘ฆ

๐‘ค โˆ™ ๐œ™ ๐‘ฅ๐‘›, ๐‘ฆ

๐‘ฆ๐‘› = argmax๐‘ฆ

โˆ† ๐‘ฆ๐‘›, ๐‘ฆ + ๐‘ค โˆ™ ๐œ™ ๐‘ฅ๐‘›, ๐‘ฆ