Skip to content

RuntimeError: derivative for to_sparse is not implemented #18111

@yougoforward

Description

@yougoforward

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=)

Metadata

Metadata

Assignees

Labels

high prioritymodule: bootcampWe plan to do a full writeup on the issue, and then get someone to do it for onboardingmodule: derivativesRelated to derivatives of operatorsmodule: sparseRelated to torch.sparsesmallWe think this is a small issue to fix. Consider knocking off high priority small issuestriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions