Repository navigation
Open
Labels
Description
Ran into an issue where NumPy's svd and Dask's svd would provide different shaped arrays. An example of this behavior is included below with details about the environment used to reproduce it.
Example:
In [1]: import numpy as np
In [2]: import dask.array as da
In [3]: m, n = 20, 10
In [4]: a = np.random.rand(m, n)
In [5]: d_a = da.from_array(a, chunks=(3, n), name='A')
In [6]: d_u, d_s, d_vt = da.linalg.svd(d_a)
In [7]: u, s, vt = np.linalg.svd(a)
In [8]: assert d_u.shape == u.shape
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-8-dffa2c855847> in <module>()
----> 1 assert d_u.shape == u.shape
AssertionError:
In [9]: d_u.shape
Out[9]: (20, 10)
In [10]: u.shape
Out[10]: (20, 20)Environment:
name: test
channels:
- conda-forge
- defaults
dependencies:
- appnope=0.1.0=py36_0
- backcall=0.1.0=py_0
- blas=1.1=openblas
- bokeh=0.12.16=py36_0
- ca-certificates=2018.4.16=0
- certifi=2018.4.16=py36_0
- click=6.7=py_1
- cloudpickle=0.5.3=py_0
- cycler=0.10.0=py36_0
- cytoolz=0.9.0.1=py36_0
- dask=0.17.5=py_0
- dask-core=0.17.5=py_0
- decorator=4.3.0=py_0
- distributed=1.21.8=py36_0
- freetype=2.8.1=0
- graphviz=2.38.0=7
- heapdict=1.0.0=py36_0
- ipython=6.4.0=py36_0
- ipython_genutils=0.2.0=py36_0
- jedi=0.12.0=py36_0
- jinja2=2.10=py36_0
- jpeg=9b=2
- kiwisolver=1.0.1=py36_1
- libgfortran=3.0.0=0
- libpng=1.6.34=0
- libtiff=4.0.9=0
- locket=0.2.0=py36_1
- markupsafe=1.0=py36_0
- matplotlib=2.2.2=py36_1
- msgpack-python=0.5.6=py36h2d50403_2
- ncurses=5.9=10
- numpy=1.14.3=py36_blas_openblas_200
- openblas=0.2.20=8
- openssl=1.0.2o=0
- packaging=17.1=py_0
- pandas=0.23.0=py36_1
- parso=0.2.1=py_0
- partd=0.3.8=py36_0
- pexpect=4.6.0=py36_0
- pickleshare=0.7.4=py36_0
- prompt_toolkit=1.0.15=py36_0
- psutil=5.4.5=py36_0
- ptyprocess=0.5.2=py36_0
- pygments=2.2.0=py36_0
- pyparsing=2.2.0=py36_0
- python=3.6.5=1
- python-dateutil=2.7.3=py_0
- python-graphviz=0.8.3=py36_0
- pytz=2018.4=py_0
- pyyaml=3.12=py36_1
- readline=7.0=0
- setuptools=39.2.0=py36_0
- simplegeneric=0.8.1=py36_0
- six=1.11.0=py36_1
- sortedcontainers=2.0.2=py36_0
- sqlite=3.20.1=2
- tblib=1.3.2=py36_0
- tk=8.6.7=0
- toolz=0.9.0=py_0
- tornado=5.0.2=py36_0
- traitlets=4.3.2=py36_0
- wcwidth=0.1.7=py36_0
- xz=5.2.3=0
- yaml=0.1.7=0
- zict=0.1.3=py_0
- zlib=1.2.11=h470a237_3
Activity
dhirschfeld commentedon Jun 8, 2018
It appears the shapes are the same if
full_matrices=Falseis passed tonp.linalg.svd.It also appears that without
full_matrices=Falseyou can't directly reconstruct the original arrayafrom the numpy results:jakirkham commentedon Jun 8, 2018
Right, we should rename this to
da.linalg.svdlacksfull_matricesargument.[-]Inconsistent shapes between NumPy's and Dask's svd[/-][+]Supporting `full_matrices` argument with Dask's `svd`[/+]jakirkham commentedon Jun 10, 2018
As to the other point you raised, @dhirschfeld, the Dask Arrays were misreporting the actual shape of the results. This was fixed with PR ( #3517 ), which should show up in the next release.
mrocklin commentedon Oct 24, 2018
@jakirkham is this resolved?
eric-czech commentedon Sep 2, 2020
Ditto that @jakirkham -- I was interested in helping pick this up after running into dask/dask-ml#732 (comment). Is there anything big left to be done/tested?
eric-czech commentedon Sep 9, 2020
I found a little bit more on what the state of this seems to be. It looks like for an (
M,N) matrix you get thefull_matrices=Falsebehavior whenM>Nand thefull_matrices=Truebehavior whenN>M:If it was clear how to get the
full_matrices=Truebehavior for all inputs from tsqr, I think it would be fairly straightforward to fully support the argument. Without that though, it would at least be nice if support forfull_matrices=Falsewas consistent. This could be one way to do that:throw an errortrim off the extra vectors returned by tsqrHow important is
full_matrices=True? I came here looking for consistentfull_matrices=Falsebehavior and I'll happily work on that, but I'm not sure what practical use cases there are for that being true other than the case @dhirschfeld mentioned in complete reconstruction of an input array.chkothe commentedon Feb 24, 2021
If I understood this right, the above merge achieves behavior that's consistent with
np.linalg.svd(X, full_matrices=False). However, one cannot currently invokeda.linalg.svd(X, full_matrices=False)since there's no such argument -- i.e. even though you have the right implementation, the API is not yet consistent.I guess the simplest fix would be to add that argument without a default value and require that
full_matricesbe passed in asFalse(this would flag users' existing code that may have relied on dask's legacy behavior).Plus, if there's interest in back compatibility, one could allow using this with something like
full_matrices='legacy', which would invoke dask's old behavior (that some users might depend on?). And if you want to avoid breaking any existing code, one could default tofull_matrices=None, which would do the same as'legacy'but should probably emit a deprecation warning that suggests to use either'legacy'orFalseexplicitly, and that at some future point the default behavior might flip to match numpy's API (i.e., default tofull_matrices=True).