Skip to content

Conversation

@vishwakftw
Copy link
Contributor

@vishwakftw vishwakftw commented Aug 8, 2018

  • Similar functionality as NumPy
  • Added doc string
  • Added tests

cc: @ssnl

Closes #10292

- Similar functionality as NumPy
- Added doc string
- Added tests
return V.mm(S_pseudoinv.diag().mm(U.t()));
}

static double _get_epsilon(const ScalarType& sc_type) {

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

"of floating types");

Tensor S = _matrix_rank_helper(self, symmetric);
return (S > tol).toType(kLong).sum().toCLong();

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@vishwakftw
Copy link
Contributor Author

@ssnl Is this good to go?

"of floating types");

Tensor S = _matrix_rank_helper(self, symmetric);
return (S > tol).toType(kLong).sum();

This comment was marked as off-topic.

This comment was marked as off-topic.

return V.mm(S_pseudoinv.diag().mm(U.t()));
}

static double _get_epsilon(const ScalarType& sc_type) {

This comment was marked as off-topic.

"of floating types");

Tensor S = _matrix_rank_helper(self, symmetric);
double tol = S.max().toCDouble() * std::max<double>(self.size(0), self.size(1)) *

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

return V.mm(S_pseudoinv.diag().mm(U.t()));
}

Tensor _matrix_rank_helper(const Tensor& self, bool symmetric) {

This comment was marked as off-topic.

"of floating types");

Tensor S = _matrix_rank_helper(self, symmetric);
Tensor tol = S.max() * _get_epsilon(self.type());

This comment was marked as off-topic.

This comment was marked as off-topic.

:attr:`tol` is the threshold below which the singular values (or the eigenvalues
when :attr:`symmetric` is ``True``) are considered to be 0. If :attr:`tol` is not
specified, :attr:`tol` is set to `S.max() * max(S.size()) * eps` where `S` is the

This comment was marked as off-topic.

>>> a = torch.eye(10)
>>> torch.matrix_rank(a)
10

This comment was marked as off-topic.

static inline Tensor _get_epsilon(const Type& type) {
switch (type.scalarType()) {
case at::ScalarType::Half:
return type.tensor({}).fill_(std::numeric_limits<at::Half>::epsilon());

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Contributor

@goldsborough goldsborough left a comment

Choose a reason for hiding this comment

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

Can be a future fix.


Tensor S = _matrix_rank_helper(self, symmetric);
Tensor tol = _get_epsilon(self.type()) * std::max(self.size(0), self.size(1));
tol.mul_(S.max());

This comment was marked as off-topic.

Copy link
Collaborator

@ssnl ssnl left a comment

Choose a reason for hiding this comment

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

Thanks!

:attr:`tol` is the threshold below which the singular values (or the eigenvalues
when :attr:`symmetric` is ``True``) are considered to be 0. If :attr:`tol` is not
specified, :attr:`tol` is set to ``S.max() * max(S.size()) * eps`` where `S` is the
singular values (or the eigenvalues when :attr:`symmetric` is ``True``), and `eps`

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

SsnL has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@vishwakftw
Copy link
Contributor Author

@ssnl is this good to go?

@vishwakftw
Copy link
Contributor Author

@ssnl ping :)

@ssnl
Copy link
Collaborator

ssnl commented Aug 15, 2018

I tried to get another person to look before merging but wasn't able to. Will get this going tomorrow.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

SsnL is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

self.assertEqual(X, Xhat, 1e-8, 'USV\' wrong')

@staticmethod
def _test_matrix_rank(self, conv_fn):

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

SsnL has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@vishwakftw
Copy link
Contributor Author

@ssnl is this good to go?

@ssnl
Copy link
Collaborator

ssnl commented Aug 17, 2018 via email

@ssnl
Copy link
Collaborator

ssnl commented Aug 19, 2018

@vishwakftw There is some really weird things going on. I tried but can't quite repro or figure out why. I will talk with people knowing more about those tests on Monday. Sorry about it.

@vishwakftw
Copy link
Contributor Author

Oh no worries. Please take your time. After all, it’s a weekend :)

@ssnl
Copy link
Collaborator

ssnl commented Aug 21, 2018

@vishwakftw the 3rd arg of np.linalg.matrix_rank is added in NumPy 0.14... Can you add a version check on the matrix_rank(aaT.cpu().numpy(), 0.01, True) test case? This is causing the internal test failure. Sorry about it.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

SsnL has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

facebook-github-bot pushed a commit that referenced this pull request Aug 22, 2018
Summary:
- Similar functionality as NumPy
- Added doc string
- Added tests

Differential Revision: D9240850

Pulled By: SsnL

fbshipit-source-id: 1d04cfadb076e99e03bdf699bc41b8fac06831bf
@vishwakftw vishwakftw closed this Aug 23, 2018
@vishwakftw vishwakftw deleted the matrix-rank branch August 23, 2018 01:59
PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
Summary:
- Similar functionality as NumPy
- Added doc string
- Added tests

Differential Revision: D9240850

Pulled By: SsnL

fbshipit-source-id: 1d04cfadb076e99e03bdf699bc41b8fac06831bf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature request] Matrix rank

6 participants