scikit-tensor
scikit-tensor copied to clipboard
Item access in sptensor always returns zero
When I try to access to a non-zero item in an sptensor like x[a, b, c], I always get zero.
Accessing after calling toarray() method works fine. I think this may be a bug.
I can't reproduce the behaviour you are describing. Can you provide a test case for the bug? The following test seems to work correctly:
def test_getitem():
subs = (
array([0, 1, 0, 5, 7, 8]),
array([2, 0, 4, 5, 3, 9]),
array([0, 1, 2, 2, 1, 0])
)
vals = array([1, 2, 3, 4, 5, 6])
S = sptensor(subs, vals, shape=[10, 10, 3])
assert_equal(0, S[1, 1, 1])
assert_equal(0, S[1, 2, 3])
assert_equal(1, S[0, 2, 0])
assert_equal(2, S[1, 0, 1])
assert_equal(3, S[0, 4, 2])
assert_equal(4, S[5, 5, 2])
assert_equal(5, S[7, 3, 1])
assert_equal(6, S[8, 9, 0])
Hmm your example is working indeed.
But I found that if I use python array instead of numpy array, it won't work. Since it works correctly with ndarray, the bug may be resolved but I think it may be helpful to add a notice for this to docstrings.
Try it like this:
def test_getitem():
subs = (
[0, 1, 0, 5, 7, 8],
[2, 0, 4, 5, 3, 9],
[0, 1, 2, 2, 1, 0]
)
vals = [1, 2, 3, 4, 5, 6]
S = sptensor(subs, vals, shape=[10, 10, 3])
assert_equal(0, S[1, 1, 1])
assert_equal(0, S[1, 2, 3])
assert_equal(1, S[0, 2, 0])
assert_equal(2, S[1, 0, 1])
assert_equal(3, S[0, 4, 2])
assert_equal(4, S[5, 5, 2])
assert_equal(5, S[7, 3, 1])
assert_equal(6, S[8, 9, 0])
@mnick do you think adding support for lists as indices and values for sparse tensors is worthwhile? Basically something like
if type(subs) != np.ndarray:
subs = np.array(subs)
and the same for vals. Or would it be better to raise an error if the type is not ndarray?
For now I would prefer the later until the API / functionality matured.