Skip to content

Correct internal dimension of Dask's SVD - #3517

Merged
jakirkham merged 2 commits into
dask:masterfrom
jakirkham:fix_svd_shape_mismatch
Jun 7, 2018
Merged

jakirkham merged 2 commits into
dask:masterfrom
jakirkham:fix_svd_shape_mismatch

Conversation

@jakirkham

@jakirkham jakirkham commented May 20, 2018 •

Copy link
Copy Markdown
Member

Previously the Dask Array SVD code had assumed the shared dimension would be equal to the column in the Dask Array matrix always. However the shared dimension in SVD is whichever is shortest (row or column) of the provided matrix. Computing the results actually provides NumPy Arrays of the expected shape. Meaning that the chunking is merely being misreported when constructing the Dask Arrays from the Dask graph. Hence this fixes how the shapes and chunks of the resulting Dask Arrays are determined.

  • Tests added / passed
  • Passes flake8 dask

Previously the Dask Array SVD code had assumed the shared dimension
would be equal to the column in the Dask Array matrix always. However
the shared dimension in SVD is whichever is shortest (row or column) of
the provided matrix. Computing the results actually provides NumPy
Arrays of the expected shape. Meaning that the chunking is merely being
misreported when constructing the Dask Arrays from the Dask graph. Hence
this fixes how the shapes and chunks of the resulting Dask Arrays are
determined.
@jakirkham

Copy link
Copy Markdown
Member Author

Discovered this bug on my flight. Haven't had time to add a test to it. That said, the bug is real.

Basically the shape in the Dask Array metadata does not match the final NumPy array shape computed. It also doesn't match how numpy.linalg.svd would have generated the array shapes either. This fixes both of those issues.

@mrocklin

Copy link
Copy Markdown
Member

Thanks for tracking this down. This looks like it could use a small test, otherwise I'm +1

@jakirkham
jakirkham force-pushed the fix_svd_shape_mismatch branch 2 times, most recently from 4686afa to 93ab56f Compare June 7, 2018 19:36
Make sure that Dask's `svd` returns arrays that would have the correct
shapes compared to similar results from NumPy.
@jakirkham
jakirkham force-pushed the fix_svd_shape_mismatch branch from 93ab56f to ff331c0 Compare June 7, 2018 19:41
@jakirkham

Copy link
Copy Markdown
Member Author

Alright, have added a test to ensure the Dask Arrays returned by svd report consistent metadata. Please let me know what you think.


for d_e, e in zip([d_u, d_s, d_vt], [u, s, vt]):
assert d_e.shape == e.shape
assert d_e.dtype == e.dtype

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.

Can we check for these by using assert_eq?

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.

We could, but that would be overkill as we already know the results are exactly equal.

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.

Ah I see, I mistakenly assumed that you were comparing to a computation with np.linalg.svd.

I guess I'll change my question then to "given that assert_eq makes this check internally, why didn't we catch this before? Would it make sense to roll these shapes/chunks examples into the parameters of an existing test?"

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.

So there is also an issue between NumPy and Dask implementations as well, but let's save that discussion for issue ( #3576 ). While we figure that out, it would be good if the Dask Arrays at least didn't misreport the data they hold.

Had looked into including this in another test initially, but the other tests seem to have some pretty niche concerns, which made it unnatural to add this to them and convoluted the otherwise simple logic included here. Plus we want to test several possible shapes, which may have slowed down tests that were comparing results. Given this, opted to breakout the test instead.

Since this test is pretty short, clean, and fast, not sure it is worth spending time trying to stuff it into another test that is running cross-purpose to this one.

@mrocklin

mrocklin commented Jun 7, 2018 via email

Copy link
Copy Markdown
Member

@jakirkham

Copy link
Copy Markdown
Member Author

Thanks Matt

@jakirkham
jakirkham merged commit 821f05e into dask:master Jun 7, 2018
@jakirkham
jakirkham deleted the fix_svd_shape_mismatch branch June 7, 2018 21:21
convexset added a commit to convexset/dask that referenced this pull request Jun 12, 2018
* master:
  [array.linalg.qr] added sfqr (short-and-fat) as a counterpart to tsqr… (dask#3575)
  Drop Dask Array's `learn` module (dask#3580)
  Adds `isneginf` and `isposinf` (dask#3581)
  Update sizeof definitions (dask#3582)
  BUG: do not raise IndexError for identity slice in array.vindex (dask#3559)
  Correct internal dimension of Dask's SVD (dask#3517)
  Fix check in configuration with env=None (dask#3562)
  Dataframe sample method docstring fix (dask#3566)
  Include "region" in key names for da.store (dask#3540)

# Conflicts:
#	dask/array/linalg.py
#	dask/array/tests/test_linalg.py
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.

2 participants