Skip to content

MRG Svm coef sign in liblinear and libsvm, fixing libsvm coef_ - #546

Merged
amueller merged 14 commits into
scikit-learn:masterfrom
amueller:svm_coef_sign
Jan 28, 2012
Merged

amueller merged 14 commits into
scikit-learn:masterfrom
amueller:svm_coef_sign

Conversation

@amueller

@amueller amueller commented Jan 9, 2012

Copy link
Copy Markdown
Member

The issue was that the signs of the decision function are flipped for the two class case in liblinear and libsvm.
To put it otherwise, some wrapping code sorts classes in ascending order while the libsvm code expects the first class in a two class setting to be '+1' and the second one '-1'.

I "fixed" this by swapping the roles of +1 and -1 in liblinear.
I felt this is the cleanest solution. I am a bit weary of touching the liblinear code since that could complicate upgrades.

What do you think?

The main reason that I did not do this in cython is the following:
The Cython wrappers get their coefs for prediction from the Python class.
If I want to switch the sign there, I'll have to switch it back when doing predictions.
So I have to switch it after fit, before calling predict or decision function and then switch again on the value of the decision function.
This seemed ugly and confusing to me.

There is also a second thing this pr fixes (yeah I know, shouldn't do that):
the primal "coef_" of libSVM are now calculated correctly in the one-vs-one case.

@fabianp

fabianp commented Jan 10, 2012

Copy link
Copy Markdown
Member

Thanks Andreas, you are doing an amazing job moving forward these issues.

@amueller

Copy link
Copy Markdown
Member Author

thanks :) comments?

@fabianp

fabianp commented Jan 10, 2012

Copy link
Copy Markdown
Member

I think you did the right thing but I'd like to test it and think a bit more about the implications before merging.

@amueller

Copy link
Copy Markdown
Member Author

Sure. The tests should all run fine. I'll add the libsvm stuff to the pull request tonight. Then you can test them together.

Comment thread sklearn/svm/base.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not look pep8 to me :)

What about:

                sv1 = self.support_vectors_[
                    sv_locs[class1]:sv_locs[class1 + 1], :]

Or better this code should be factorized in a private python function that would both reduce the complexity of the method and increase its readability and respect pep8 by removing 2 indent levels.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops... I'm sorry. Factorization seems like a good idea.

@amueller

Copy link
Copy Markdown
Member Author

@mblondel : I added an example but I am not very happy with how it looks.
I made the description a bit more understandable, though - at least that's what I hope ;)
If you look at the old coef_, the first column would correspond to the first column of the example table.
At first, it looks like the "class 0 vs rest" classifier. It has all the support vectors from classes 1 and 2 that are
involved in this classifier. But it does not have the support vectors corresponding to :math:\alpha_{0,2}.
That seems quite weird to me.

If there is no obvious way to actually use the old coef_, I think breaking the API is ok.

@fabianp

fabianp commented Jan 16, 2012

Copy link
Copy Markdown
Member

Please add a (very small) description of the changes on the first lines of linear.cpp, something like:

Modified 2012:

    invert sign of classes to match scikit-learn API (amueller)

that really helps when applying upstream changes

@amueller

Copy link
Copy Markdown
Member Author

Will do!

@ogrisel

ogrisel commented Jan 16, 2012

Copy link
Copy Markdown
Member

Maybe you could even put a link to this pull request #546.

@amueller

Copy link
Copy Markdown
Member Author

REBASE :)
merge?

@amueller

Copy link
Copy Markdown
Member Author

Merged master.
@larsmans what do you think about the coef for sparse svms? Should it be a sparse matrix or should I convert it to dense?

@larsmans

Copy link
Copy Markdown
Member

I'd like to see some timings for both, e.g. from the document classification example (maybe with predict called on one sample at a time?).

@amueller

Copy link
Copy Markdown
Member Author

As I said before, predict doesn't use coef_. So I'm not sure what you want to benchmark. I could write a python version of predict and benchmark that... though I would think that this should rather been done in another pull request.

[edit] Forgot what I said. Writing a python predict function for SVC using coef_ doesn't make sense, as it would only work with
kernel="linear".[/edit]

@larsmans

Copy link
Copy Markdown
Member

Sorry, my bad, should have remembered that. We might want to let this depend on penalty; with L1 penalty, a sparse matrix would seem in order, but with L2, it's bound to be pretty dense (100% in the 20 newsgroups example).

@amueller

Copy link
Copy Markdown
Member Author

I'm not sure what the L1 penalty means (can you give a reference?) and whether it will result in sparse primal solutions.
Any way, the matrix is "naturally" represented as a sparse matrix, as it is a product of two sparse matrices.
I could just always convert it to be dense, though there might be cases where this is not appropriate.

Don't you think it is ok to let the user worry whether they want to convert the matrix to a dense representation or not?

@amueller

Copy link
Copy Markdown
Member Author

As far as I can see, there is no l1 penalization implemented in LibSVM, btw.

While this might not be clear from the title of the pr cough, the change to "coef_" only applies to LibSVM, not LibLinear,
because LibSVM uses one-vs-one training.

@larsmans

Copy link
Copy Markdown
Member

The Liblinear paper describes L1-regularized SVMs. When you run the document classification example, you'll get output like

L2 penalty
________________________________________________________________________________
Training: 
LinearSVC(C=1000, dual=False, fit_intercept=True, intercept_scaling=1,
     loss=l2, multi_class=False, penalty=l2, scale_C=False, tol=0.001)
train time: 0.395s
test time:  0.008s
f1-score:   0.895
dimensionality: 32395
density: 1.000000

and

L1 penalty
________________________________________________________________________________
Training: 
LinearSVC(C=1000, dual=False, fit_intercept=True, intercept_scaling=1,
     loss=l2, multi_class=False, penalty=l1, scale_C=False, tol=0.001)
train time: 6.005s
test time:  0.008s
f1-score:   0.868
dimensionality: 32395
density: 0.018429

where the last figure indicates the fraction of non-zero elements in coef_. Obviously, the L2-penalized LinearSVC creates a dense coef_, while the L1-penalized one created a sparse coef_, and I believe that's the main reason for choosing the L1 penalty in the first place.

@amueller

Copy link
Copy Markdown
Member Author

As I said in my edit above, what I did only changed things in libsvm.
It is definitely worth hinking about how to store primal coef for linearSVC, but that has nothing to do with this PR ;)

@larsmans

Copy link
Copy Markdown
Member

Ok. I thought you were working on Liblinear, I hadn't actually looked at the changes yet.

@amueller

Copy link
Copy Markdown
Member Author

Sorry for mixing something unrelated in the pull-request...

@mblondel

Copy link
Copy Markdown
Member

@larsmans what do you think about the coef for sparse svms? Should it be a sparse matrix or should I convert it to dense?

There is no gain to expect by storing the dual coefficients in a scipy
sparse matrix. On the contrary, this representation is inefficient. Of
course the support vectors, them, do need to be stored in a sparse
matrix. Note that even the dense SVC has sparse dual coefficients: the
support vectors are stored in a numpy array and the corresponding dual
coefficients are stored in parallel (so we know without ambiguity to
which support vector each dual coefficient corresponds). Other
training instances do not need to be stored as their dual coef is
zero.

@amueller: I think you should focus on merging your PR in its current
state. The above issue can be addressed when we tackle issue #388.

@amueller

Copy link
Copy Markdown
Member Author

@mblondel ok :) Any suggestions? I think it should be ready for merge.

@ogrisel

ogrisel commented Jan 28, 2012

Copy link
Copy Markdown
Member

+1 for merging.

@mblondel

Copy link
Copy Markdown
Member

+1 for merging.

+1

amueller added a commit that referenced this pull request Jan 28, 2012
MRG Svm coef sign in liblinear and libsvm, fixing libsvm coef_
@amueller
amueller merged commit a6fdecd into scikit-learn:master Jan 28, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants