Repository navigation
[MRG+2] Change ovr_decision_function scale - #10440
GaelVaroquaux merged 22 commits into
Conversation
|
@jnothman do you have any other unit test that I can add for the behaviour of _ovr_decision_function ? |
| 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) / |
There was a problem hiding this comment.
On what basis do you choose /10?
There was a problem hiding this comment.
not sure myself. I am worried about the behaviour of the sigmoid if the sum_of_confidences values are high.
There was a problem hiding this comment.
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).
| # 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]) |
There was a problem hiding this comment.
I'm not sure I get this... Try reword the comment?
There was a problem hiding this comment.
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.
| 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))) |
There was a problem hiding this comment.
Maybe comment on the purpose of eps here. Is it to ensure we never reach the limits?
|
Please add an entry to the change log at |
| if max_confidences == min_confidences: | ||
| return votes | ||
|
|
||
| # Scale the sum_of_confidences to (-0.5, 0.5) and add it with votes. |
There was a problem hiding this comment.
scale -> monotonically transform
There was a problem hiding this comment.
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?
|
|
||
| - 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>` |
| 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 |
There was a problem hiding this comment.
has a higher confidence
| 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 |
There was a problem hiding this comment.
add a full stop after "if there is a tie" to indicate that the next line is a new sentence
|
@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 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. |
|
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 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. |
|
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 / |
There was a problem hiding this comment.
Does this transformation have a name?
|
looks good, I think. Does this transform have a name? It's a bit adhoc, I think. |
|
What we already have is ad-hoc. Its purpose is only to make a
rank-consistent transformation of the OvO scores.
|
|
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 ;) |
|
Just merge master to update the Circle config |
|
@Johayon could you please rebase and fix the conflicts? I have good hopes that this PR could be merged this week. |
|
@albertcthomas you should be able to push on this PR and do the rebase yourself. |
|
@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. |
|
Hi, |
| .................... | ||
| - |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>` |
There was a problem hiding this comment.
you should document the public funcions/classes impacted by this PR not the private functions. These are not part of the documentation.
There was a problem hiding this comment.
ok, is it better like this ?
| ``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`:: |
There was a problem hiding this comment.
still refer to private function
| :func:`utils.multiclass._ovr_decision_function`. :pr:`10440` by :user:`Jonathan Ohayon <Johayon>` | ||
|
|
||
| :mod:`sklearn.multiclass` | ||
| .................... |
There was a problem hiding this comment.
should be fine this time.
There was a problem hiding this comment.
no. You should have as many "." as number of chars in the header title
There was a problem hiding this comment.
sorry, I did not know about that.
| .................. | ||
|
|
||
| - |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. |
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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'.
There was a problem hiding this comment.
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.
GaelVaroquaux
left a comment
There was a problem hiding this comment.
LGTM. Merging. Thank you!
|
Thank you all. |
|
Nice work, @Johayon. Thank you!
|
* 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
This reverts commit 75614ef.
This reverts commit 75614ef.
* 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
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))