# Sparse Tensors Odd Behavior v0.2

The indices don’t align with the values. In the example given on the website, by this logic, seems like it’s indexing the columns first and that the indices are in reverse order to their values. Seems like `[0, 1]` corresponds to 4 and `[2, 0]` corresponds to 3.

``````i = torch.LongTensor([[0, 1], [2, 0]])
v = torch.FloatTensor([3, 4])
torch.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense()
0  0  3
4  0  0
[torch.FloatTensor of size 2x3]
``````

If I change the indices to `[0, 1]` and `[0, 1]`, I get

``````i = torch.LongTensor([[0, 1], [0,1]])
v = torch.FloatTensor([3, 4])
torch.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense()

3  0  0
0  4  0
[torch.FloatTensor of size 2x3]
``````

…this seems like unexpected behavior.
Also if I try - should be intuitive - putting a value in position `[1, 1]`

``````i = torch.LongTensor([[0, 1], [2, 0], [1,1]])
v = torch.FloatTensor([3, 4, 5])
torch.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 2: number of dimensions must be nDimI + nDimV at /pytorch/torch/lib/THS/generic/THSTensor.c:169
``````

Pytorch writes the indices in `dimensions x points`, you apparently expect it the other way round. So if you write `i.t ()` when constructing the sparse tensor, it should work.

Best regards

Thomas

1 Like

For anyone else who also needs help with this, notice the transpose (`i.t()`) in the last line and that you need to adjust the coordinates from the original example.

``````i = torch.LongTensor([[0, 1], [1, 0]])
v = torch.FloatTensor([3, 4])
torch.sparse.FloatTensor(i.t(), v, torch.Size([2,3])).to_dense()

0  3  0
4  0  0
[torch.FloatTensor of size 2x3]
``````

Ok that worked. Thanks!

Sorry for the bump. I’m hitting this problem again. But I’m running into this issue again. I don’t understand what’s going wrong. The dimensions appear correct.

``````>>> import torch
>>> cd = [[1, 0]]
>>> gd = [0.39613232016563416]
>>> i = torch.LongTensor(cd)
>>> v = torch.FloatTensor(gd)
>>> p = torch.rand(2)
>>> i

1  0
[torch.LongTensor of size 1x2]

>>> v

0.3961
[torch.FloatTensor of size 1]

>>> p

0.4678
0.0996
[torch.FloatTensor of size 2]

>>> grads = torch.sparse.FloatTensor(i.t(), v, torch.Size(list(p.size()))).to_dense()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 2: number of dimensions must be nDimI + nDimV at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/THS/generic/THSTensor.c:169
``````

By your v and i, you are constructing a 2d tensor with one nonzero entry at 1,0.
p.size() is  and this is one-dimensional. If you pass a size object with two elements as last argument, it’ll work.

Best regards

Thomas

1 Like