-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Inconsistent behavior of dot products with sparse matrices #881
Comments
I noticed what appears to me to be another inconsistency. import numpy as np
import scipy.sparse
import aesara.sparse
import aesara.tensor as at
from aesara.tensor.random import RandomStream A sparse matrix multiplied by a matrixLet us define p = 5
srng = RandomStream(seed=12345)
X = scipy.sparse.random(p, p, density=.33, format="csr")
X_csr = aesara.sparse.as_sparse(X)
y = np.ones((p, p))
y_tt = at.as_tensor(y) We saw in the previous message that the at.dot(X_csr, y_tt).eval()
"""
| 1.33790965 | 1.33790965 | 1.33790965 | 1.33790965 | 1.33790965 |
| 0.47078111 | 0.47078111 | 0.47078111 | 0.47078111 | 0.47078111 |
| 0.98612278 | 0.98612278 | 0.98612278 | 0.98612278 | 0.98612278 |
| 0.72805556 | 0.72805556 | 0.72805556 | 0.72805556 | 0.72805556 |
| 0.22092613 | 0.22092613 | 0.22092613 | 0.22092613 | 0.22092613 |
""" But that this does not with the X_csr @ y_tt
# TypeError: The dense dot product is only supported for dense types A sparse matrix multiplied by a vectorNow let y_vec = np.ones(p)
y_vec_tt = at.as_tensor(y_vec) This does not work: at.dot(X_csr, y_vec_tt).eval()
# TypeError: The dense dot product is only supported for dense types But this does: at.dot(X_csr, at.shape_padright(y_vec_tt)).eval()
"""
| 1.33790965 |
| 0.47078111 |
| 0.98612278 |
| 0.72805556 |
| 0.22092613 |
""" For reference, let us make sure that the dot product does work with a dense tensor matrix and a tensor vector: X_tt = at.as_tensor(np.random.random((p,p)))
at.dot(X_tt, y_vec_tt).eval()
# | 1.35070533 | 2.56274912 | 1.70920864 | 2.87279029 | 1.94839187 | A possible explanation
Maybe someone who knows the internals better can correct me, but there seems to be an assumption throughout the library that we should read |
The reason is that
Yes, this is one of the compromises we made along the way toward combining/merging the tensor types and interfaces. The reason we chose to make all original/base tensor types dense is that it required fewer changes and wouldn't break dependent libraries. Here's a statement to that effect in the relevant PR. In summary, we need a follow-up PR that introduces something like an |
Here's an issue for those type changes: #886. |
This issue was discovered while testing
horsehoe_nbinom_gibbs
inaemcmcm
(code) with a sparse matrix input for the variableX
. Let's consider the product of a sparse tensor with a dense tensor:aesara.tensor.dot
is configured to work with bothTensorType
andSparseType
. Indeed:Using the
dot
method of the sparse tensor also works:The operator
@
, however, raises aTypeError
:For some reason that is not yet clear to me, the
__dot__
method of the_tensortype_operators_class
is called:instead of the
_sparse_py_operators
.The text was updated successfully, but these errors were encountered: