Suppose I have a 3x3 adjacency matrix A in sparse format such that
indices = torch.tensor([[1, 0, 1, 2, 0, 2], [0, 1, 2, 1, 2, 0]])
values = torch.tensor([1., 1., 1., 1., 1., 1.])
A = torch.sparse_coo_tensor(indices, values, size=(3, 3))
In dense format this would look like
A.to_dense()
tensor([[0., 1., 1.],
[1., 0., 1.],
[1., 1., 0.]])
I want the graph to be dynamic i.e. new edges can be created. Is there any way to add an edge to a sparse matrix without recreating the sparse matrix from an extended indices and values list?
I have tried e.g.
A[0,0] = 1
but this only works if first converting to dense and then inserting:
B = A.to_dense().clone()
B[0,0] = 5
print(B)
tensor([[5., 1., 1.],
[1., 0., 1.],
[1., 1., 0.]])
I figure that the latter option may be computationally heavy, so what is the best way to do this?
Thanks in advance for any help on this matter
Simon