Skip to content

[MRG] FIX: fix lsml inversion #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

wdevazelhes
Copy link
Member

Fixes #202

Copy link
Contributor

@perimosocordiae perimosocordiae left a comment

Choose a reason for hiding this comment

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

Looks good. It's possible that this will mask some issues from users that provide a garbage matrix as the prior, but that's probably okay.

@@ -79,9 +79,12 @@ def test_singular_covariance_does_not_diverge(self):
# when this covariance has null eigenvalues (See
# https://github.com/metric-learn/metric-learn/issues/202)
# TODO: remove when # 195 is merged
X, Y = make_classification(n_redundant=2, random_state=42)
rng = np.random.RandomState(42)
Copy link
Member Author

Choose a reason for hiding this comment

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

somehow the covariance matrix in the previous example was not exactly singular so I built the example by hand, and it is indeed truly singular and exhibited another place where we should use the pseudo inverse

Copy link
Member

@bellet bellet left a comment

Choose a reason for hiding this comment

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

I am not sure that this is a good fix, because logdet divergence is finite only if the matrix is (strictly) positive definite. Hence working the whole time with singular matrices and pseudo-inverses looks like deviating quite a bit from the algorithm.

I think a better fix may be simply to return an error when the prior is not positive definite. When this happens with the covariance, the identity can be used (this could be done in #180)

@wdevazelhes
Copy link
Member Author

wdevazelhes commented May 14, 2019

I am not sure that this is a good fix, because logdet divergence is finite only if the matrix is (strictly) positive definite. Hence working the whole time with singular matrices and pseudo-inverses looks like deviating quite a bit from the algorithm.

I think a better fix may be simply to return an error when the prior is not positive definite. When this happens with the covariance, the identity can be used (this could be done in #180)

That's right, I didn't think of that...
I noticed that there is the same problem for SDML, and I'm thinking about this case: say the user provides a prior that is not PSD. But the emp_cov matrix could be PSD no? In this case should we still forbid an indefinite prior ? I guess so because it does not makes sense given what is a prior ? (it should be a mahalanobis metric)

EDIT: I got confused with PD and PSD: I guess this was the question
I noticed that there is the same problem for SDML, and I'm thinking about this case: say the user provides a prior that is not PD. But the emp_cov matrix could be PD no? In this case should we still forbid an indefinite prior ?
And after looking at SDML, I just realized that we need the inverse of the prior anyway which can be done only if it is definite, so the answer is we should also forbid the prior to be indefinite there

@wdevazelhes
Copy link
Member Author

Since in the end we will use init='identity' for the examples, I guess we don't need to merge this PR quicker, and the work will be done in PR #195 , so I'm closing the PR

@bellet
Copy link
Member

bellet commented May 14, 2019

I am not sure that this is a good fix, because logdet divergence is finite only if the matrix is (strictly) positive definite. Hence working the whole time with singular matrices and pseudo-inverses looks like deviating quite a bit from the algorithm.

I think a better fix may be simply to return an error when the prior is not positive definite. When this happens with the covariance, the identity can be used (this could be done in #180)

Let me correct my own claim. While the LogDet divergence is indeed finite only for pairs of PD matrices, in LSML and other algorithms using LogDet (ITML, SDML) having a PSD (not strictly PD) prior is probably fine in most cases because the logdet term on the prior is ignored since it is constant. So this PR could be merged if you use pseudo-inverse only for the prior (but not for the gradient!).

Yet, I think it is probably safer to rule out non-PD priors for these algorithms (see my other comment in #195)

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.

LSML fails on some classical inputs
3 participants