Delete entry from sparse tensor

Hello, I would like to be able to add and delete elements in an
pytorch sparse tensor

While I did not find a way to add an element by assigning it s[0,1] = 3, I discovered that you can just add it by an addition:

i = [[0, 1],[2, 0]]
v = [3, 4]
a1 = torch.sparse_coo_tensor(i, v, (5, 5))
i = [[3],[3]]
v = [10]
a2 = torch.sparse_coo_tensor(i, v, (5, 5))
assigned_tensor = (a1 + a2).coalesce()
print("assigned tensor ",assigned_tensor)

I did not find a way to delete an element from a sparse tensor either, and if I try to subtract an element in the same way like

i = [[0, 1],[2, 0]]
v = [3, 4]
a1 = torch.sparse_coo_tensor(i, v, (5, 5))
i = [[0],[2]]
v = [3]
a2 = torch.sparse_coo_tensor(i, v, (5, 5))
added_tensor = (a1 - a2).coalesce()
print("deleted tensor ",added_tensor)

the element does not vanish but the value becomes zero.

How do I delete an entry from the sparse tensor?
(Noticing this discussion about deleting 0 values in a sparse tensor).

1 Like