Introduction of SVM struct 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
β’ 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
[(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]
π€ β π π₯π, π¦