-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Description
When using to_sparse().requires_grad_(True) to tensor with .requires_grad_(False), it behaves just like the official example "https://pytorch.org/docs/stable/sparse.html?highlight=sparse"
. But when I use it in a network (in training phase) to tensor with .requires_grad_(True), it report "RuntimeError: derivative for to_sparse is not implemented" in backward. When I use torch.sparse.Floattensor to create sparse tensor, there is no problem. For my case, I already have a matrix with many zero elements, then is the to_sparse() operation can be used in network or I can just use torch.sparse.Floattensor to create sparse tensor for training?
a simple example :
a = torch.randn(2, 3).requires_grad_(False).to_sparse().requires_grad_(True)
a
tensor(indices=tensor([[0, 0, 0, 1, 1, 1],
[0, 1, 2, 0, 1, 2]]),
values=tensor([-1.6929, -0.6308, 1.0252, 1.2527, 0.9184, 1.7817]),
size=(2, 3), nnz=6, layout=torch.sparse_coo, requires_grad=True)
a = torch.randn(2, 3).requires_grad_(True).to_sparse().requires_grad_(True)
a
tensor(indices=tensor([[0, 0, 0, 1, 1, 1],
[0, 1, 2, 0, 1, 2]]),
values=tensor([ 0.3564, 1.5703, 1.2693, -0.4577, 0.7191, -1.0451]),
size=(2, 3), nnz=6, layout=torch.sparse_coo, grad_fn=)