Skip to content

[MRG+2] Change ovr_decision_function scale - #10440

Merged
GaelVaroquaux merged 22 commits into
scikit-learn:masterfrom
Johayon:ovr_decision_function_sigmoid_scale
Feb 28, 2019
Merged

GaelVaroquaux merged 22 commits into
scikit-learn:masterfrom
Johayon:ovr_decision_function_sigmoid_scale

Conversation

@Johayon

@Johayon Johayon commented Jan 10, 2018 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #9174

What does this implement/fix? Explain your changes.

TODO;

  • add unit tests for _ovr_decision_function main properties

  • decision values near votes values (-.5,+.5) range

  • can resolve tie for a sample (information in decision_function rows values)

  • higher confidence means higher decision values (information in decision_function columns values)

  • subset invariance (not check for current implementation)

  • change scaling implementation in _ovr_decision_function

  • x/(2*(abs(x)+1))

@Johayon

Johayon commented Jan 10, 2018

Copy link
Copy Markdown
Contributor Author

@jnothman do you have any other unit test that I can add for the behaviour of _ovr_decision_function ?

Comment thread sklearn/utils/multiclass.py Outdated
max_abs_confidence = max(abs(max_confidences), abs(min_confidences))
scale = (0.5 - eps) / max_abs_confidence
return votes + sum_of_confidences * scale
sigmoid_confidences = ((np.exp(sum_of_confidences/10) - 1) /

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.

On what basis do you choose /10?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not sure myself. I am worried about the behaviour of the sigmoid if the sum_of_confidences values are high.

@Johayon Johayon Jan 10, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

maybe a simple function like

lambda x:  x/(2*(np.abs(x)+1))

will be better, because we don't have to worry about overflow like with the exponential.
The only thing we need to verify the conditions we want, is a strictly increasing function which goes from (-inf,+inf) to (-.5,+.5).

Comment thread sklearn/utils/tests/test_multiclass.py Outdated
# as confidence is higher on the 3rd sample for the
# two last classifier, dec value should also be higher
# for the third class
assert_true(dec_values[2, 2] > dec_values[3, 2])

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.

I'm not sure I get this... Try reword the comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the idea here is to check that the rank inside the columns are meaningful. A sample with higher confidence in the classification should have a higher decision value.

@Johayon Johayon changed the title [WIP] Change ovr_decision_function scale [MRG] Change ovr_decision_function scale Jan 11, 2018

@jnothman jnothman left a comment

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.

LGTM!

Comment thread sklearn/utils/multiclass.py Outdated
scale = (0.5 - eps) / max_abs_confidence
return votes + sum_of_confidences * scale
transformed_confidences = (sum_of_confidences /
((2 + eps) * (np.abs(sum_of_confidences) + 1)))

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.

Nice choice of function!

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.

Maybe comment on the purpose of eps here. Is it to ensure we never reach the limits?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, that's it.

@jnothman

Copy link
Copy Markdown
Member

Please add an entry to the change log at doc/whats_new/v0.20.rst under bug fixes. Also please mention the changed decision_function behaviour in the changed models section up top. Like the other entries in the changelog, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:

@jnothman jnothman changed the title [MRG] Change ovr_decision_function scale [MRG+1] Change ovr_decision_function scale Jan 15, 2018
Comment thread sklearn/utils/multiclass.py Outdated
if max_confidences == min_confidences:
return votes

# Scale the sum_of_confidences to (-0.5, 0.5) and add it with votes.

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.

scale -> monotonically transform

@albertcthomas albertcthomas left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few comments. Otherwise LGTM. Thanks @Johayon! One small remark/question but more for my own understanding than for this PR particularly: is it really necessary to add eps in the scaling? The fact that we use abs(x) + 1 should be sufficient to ensure that the returned values are in (-0.5, 0.5) for any x. I agree that the limit at + and - infinity would be respectively equal to -0.5 and 0.5 but are we going to observe this in practice?

Comment thread doc/whats_new/v0.20.rst Outdated

- Fixed a bug in :func:`utils.multiclass._ovr_decision_function` which would give
different results when applied to a subset due to local scaling method.
:issue:`10440` :user:`Jonathan Ohayon <Johayon>`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue by user

Comment thread sklearn/utils/tests/test_multiclass.py Outdated
assert_array_equal(np.argmax(dec_values, axis=1), expected_prediction)

# third and fourth sample have the same vote but third sample
# have higher confidence, this should reflect on the decision values

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

has a higher confidence

Comment thread sklearn/utils/tests/test_multiclass.py Outdated
assert_allclose(votes, dec_values, atol=0.5)

# check that the prediction are what we expect
# highest vote or highest confidence if there is a tie

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add a full stop after "if there is a tie" to indicate that the next line is a new sentence

@Johayon

Johayon commented Mar 12, 2018 •

Copy link
Copy Markdown
Contributor Author

@albertcthomas because of float imprecision, it is possible that the value returned is equal to the upper (or lower) bound. It seems that epsilon is not even enough, it should be 2+2*eps because 2+eps is still 2.0 as a floating point, I should have checked that.

import numpy as np
eps = np.finfo('float').eps

f = lambda x: x/((2)*(np.abs(x)+1))
print(f(1e16) < 0.5, f(-1e16) > -0.5)

f = lambda x: x/((2+eps)*(np.abs(x)+1))
print(f(1e16) < 0.5, f(-1e16) > -0.5)

f = lambda x: x/((2+2*eps)*(np.abs(x)+1))
print(f(1e16) < 0.5, f(-1e16) > -0.5)

I will add a test to check that this is ok, it seems to be missing.

@albertcthomas

Copy link
Copy Markdown
Contributor

Thanks for the clarification. I had just compared the first and second case of your snippet and observed that they seemed to be the same.

@albertcthomas

albertcthomas commented Mar 12, 2018 •

Copy link
Copy Markdown
Contributor

So in master the values of sum_of_confidences are scaled to (-0.5, 0.5) whatever the min and max values of sum_of_confidences. I guess this was ensuring a kind of continuity of the returned _ovr_decision_function. With this PR, and the suggested monotonically increasing transform it seems to me that we are no longer ensuring this continuity, but just preserving the rankings. In that case can we use 1/3 instead of 1/2?

@Johayon

Johayon commented Mar 12, 2018

Copy link
Copy Markdown
Contributor Author

@albertcthomas I don't follow what you mean by "continuity" here. But I think that you are correct and we should use 1/3 in order to escape from the float imprecision.

I changed one of the example in the test, to check for float imprecision, and this test will fail in both the master and the PR if we take 1/2 even if we add an epsilon.

@albertcthomas

Copy link
Copy Markdown
Contributor

Sorry I was wrong for the continuity... If we just want to preserve the rankings, 1/3, although arbitrary, should be OK. I wonder if there was a good reason to choose 0.5...

scale = (0.5 - eps) / max_abs_confidence
return votes + sum_of_confidences * scale
# Use 1/3 instead of 1/2 to ensure that we won't reach the limits
transformed_confidences = (sum_of_confidences /

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 this transformation have a name?

@amueller

Copy link
Copy Markdown
Member

looks good, I think. Does this transform have a name? It's a bit adhoc, I think.

@jnothman

jnothman commented May 23, 2018 via email

Copy link
Copy Markdown
Member

@amueller

Copy link
Copy Markdown
Member

fair. But why this and not sigmoid for example? But I guess an arbitrary choice is fine. Can we at least make sure the user guide and docstring are useful ;)

@jnothman

Copy link
Copy Markdown
Member

Just merge master to update the Circle config

@albertcthomas

Copy link
Copy Markdown
Contributor

@Johayon could you please rebase and fix the conflicts? I have good hopes that this PR could be merged this week.

@agramfort

Copy link
Copy Markdown
Member

@albertcthomas you should be able to push on this PR and do the rebase yourself.

@albertcthomas

Copy link
Copy Markdown
Contributor

@Johayon I don't have the rights to push commits to your PR. If you don't have time to do this, I can take over your PR (keeping all your commits) to finish it during the scikit-learn sprint currently happening in Paris.

@Johayon

Johayon commented Feb 27, 2019

Copy link
Copy Markdown
Contributor Author

Hi,
sorry about the delay, this should solve the conflicts in estimator checks.
Have a good sprint.

Comment thread doc/whats_new/v0.21.rst Outdated
....................
- |Fix| Fixed an issue in :func:`utils.multiclass._ovr_decision_function` which would give
different results when applied to a subset due to local scaling method.
:pr:`10440` by :user:`Jonathan Ohayon <Johayon>`

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.

you should document the public funcions/classes impacted by this PR not the private functions. These are not part of the documentation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, is it better like this ?

Comment thread doc/modules/svm.rst Outdated
``decision_function_shape`` option allows to monotically transform the results of the
"one-against-one" classifiers to a decision function of shape ``(n_samples,
n_classes)``::
n_classes)`` using the function :func:`_ovr_decision_function` :ref:`multiclass`::

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.

still refer to private function

Comment thread doc/whats_new/v0.21.rst Outdated
:func:`utils.multiclass._ovr_decision_function`. :pr:`10440` by :user:`Jonathan Ohayon <Johayon>`

:mod:`sklearn.multiclass`
....................

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.

not enough ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

should be fine this time.

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.

no. You should have as many "." as number of chars in the header title

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry, I did not know about that.

@albertcthomas

Copy link
Copy Markdown
Contributor

Thanks @Johayon! Maybe @jnothman would like to have a second look, or @amueller?

Comment thread doc/whats_new/v0.21.rst Outdated
..................

- |Fix| Fixed an issue in :func:`svm.SVC.decision_function` which would give
different results when applied to a subset due to local scaling method.

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.

I don't think this wording is especially clear, but I think the important thing to note is that results are changed only for decision_function_shape='ovr'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

maybe something like this :

Fixed an issue in :func:`svm.SVC.decision_function` which would give inconsistent 
results for a sample due to a scaling method depend on the dataset when
decision_function_shape='ovr'.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be better to put when decision_function_shape='ovr' at the top if this is the important information and use the problem described in the original issue, something like

Fixed an issue in :func:`svm.SVC.decision_function` when decision_function_shape='ovr'.
The decision_function value of a given sample was different depending on whether the
decision_function was evaluated on the sample alone or on a batch containing this same
sample due to the scaling used in decision_function.

@agramfort agramfort changed the title [MRG+1] Change ovr_decision_function scale [MRG+2] Change ovr_decision_function scale Feb 28, 2019

@GaelVaroquaux GaelVaroquaux left a comment

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.

LGTM. Merging. Thank you!

@GaelVaroquaux
GaelVaroquaux merged commit d045c1a into scikit-learn:master Feb 28, 2019
@Johayon

Johayon commented Feb 28, 2019

Copy link
Copy Markdown
Contributor Author

Thank you all.

@jnothman

jnothman commented Feb 28, 2019 via email

Copy link
Copy Markdown
Member

xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
* unit test for _ovr_decision_function

* sigmoid scale

* error on subset test

* clearer comment on test

* new scaling function forget sigmoid

* remove exception in subset check for SVC decision_function

* add comment and update whats_new

* update comment

* change test for float imprecision

* use 1/3 instead of 1/2

* merge conflict error

* flake8 correction and assert_true replacement

* better comment

* refer to public functions

* remove reference to private function

* rst dots

* change fix comments
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
* unit test for _ovr_decision_function

* sigmoid scale

* error on subset test

* clearer comment on test

* new scaling function forget sigmoid

* remove exception in subset check for SVC decision_function

* add comment and update whats_new

* update comment

* change test for float imprecision

* use 1/3 instead of 1/2

* merge conflict error

* flake8 correction and assert_true replacement

* better comment

* refer to public functions

* remove reference to private function

* rst dots

* change fix comments
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.

SVC and OneVsOneClassifier decision_function inconsistent on sub-sample

6 participants