sparse
sparse copied to clipboard
Dot product between ones and COO tensor
Currently, sparse.ones returns a sparse tensor with a fill value of 1 which seems incompatible with the default value (0), at least when taking a dot product.
Minimal code to reproduce the issue:
import sparse
t1 = sparse.ones(5)
t2 = sparse.COO([1, 2], [1, 1], shape=(5, ))
sparse.dot(t1, t2)
Output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-d149cc87b95e> in <module>
2 t1 = sparse.ones(5)
3 t2 = sparse.COO([1, 2], [1, 1], shape=(5, ))
----> 4 sparse.dot(t1, t2)
~/anaconda3/lib/python3.7/site-packages/sparse/coo/common.py in dot(a, b)
256 COO.dot : Equivalent function for COO objects.
257 """
--> 258 check_zero_fill_value(a, b)
259 if not hasattr(a, 'ndim') or not hasattr(b, 'ndim'):
260 raise TypeError(
~/anaconda3/lib/python3.7/site-packages/sparse/utils.py in check_zero_fill_value(*args)
291 not equivalent(arg.fill_value, _zero_of_dtype(arg.dtype))):
292 raise ValueError('This operation requires zero fill values, '
--> 293 'but argument {:d} had a fill value of {!s}.'.format(i, arg.fill_value))
294
295
ValueError: This operation requires zero fill values, but argument 0 had a fill value of 1.0.
Yes, this is a missing feature. In your example, the statement can be easily written as a sum, is there a reason for not doing so?
Yes, indeed, this one can be done with a sum, I just wrote this to have a minimal reproducible example. The issue I had was with general tensors, not necessarily vectors. While there are ways around it I figured it might be worth opening an issue.