RuntimeError: The backward pass for this operation requires the 'self' tensor to be strided, but a sparse tensor was given instead. Please either use a strided tensor or set requires_grad=False for 'self'

username: work
cooked: <p>Hi,<br>
when i run forward() as follows:</p>
<pre><code class="lang-auto"> def forward(self, input, adj):
        support = torch.mm(input, self.weight)
        output = torch.spmm(adj, support)
</code></pre>
<p>this<br>
<code>output = torch.spmm(adj, support)</code><br>
got an error<br>
<code>RuntimeError: The backward pass for this operation requires the 'self' tensor to be strided, but a sparse tensor was given instead. Please either use a strided tensor or set requires_grad=False for 'self'</code><br>
here <em>adj</em> is an output, a sparse tensor, of another layer which has <em>requires_grad=True</em>.<br>
how can i solve the problem?</p>

username: srishti-git1110
cooked: <p>Hi,<br>
Can you please brief in words what you are actually trying to do?</p>
<p>I tried reproducing your error and was able to do that with this piece of code :</p>
<pre><code class="lang-auto">input = torch.randn(3,3)
weight = torch.randn(3,3)
weight.requires_grad_()
i = [[0, 1, 1],
     [2, 0, 2]]
v =  [3.0, 4, 5]

adj = torch.sparse_coo_tensor(i, v, (2, 3), requires_grad=True) 
def forward(input, adj):
    support = torch.mm(input, weight)
    output = torch.smm(adj, support)
    
forward(input, adj)
</code></pre>
<p>and got the same error as you:</p>
<pre><code class="lang-auto">RuntimeError: The backward pass for this operation requires the 'self' tensor to be strided, but a sparse tensor was given instead. Please either use a strided tensor or set requires_grad=False for 'self'
</code></pre>
<p>Does this piece of code align with your use-case?</p>
<p>Also, from what I could understand from the error message, I set requires_grad=False for the sparse adj tensor, and the following code then ran fine:</p>
<pre><code class="lang-auto">input = torch.randn(3,3)
weight = torch.randn(3,3)
weight.requires_grad_()
i = [[0, 1, 1],
     [2, 0, 2]]
v =  [3.0, 4, 5]

adj = torch.sparse_coo_tensor(i, v, (2, 3))
def forward(input, adj):
    support = torch.mm(input, weight)
    output = torch.smm(adj, support)
    
forward(input, adj)
</code></pre>
<p>More importantly, I couldn’t find any official docs for <code>torch.spmm</code> that you are using.</p>
<p>I know <code>torch.smm</code> is used to perform a matrix multiplication of a sparse matrix with a dense matrix. So, I replaced <code>torch.spmm</code> in the above code to get this:</p>
<pre><code class="lang-auto">input = torch.randn(3,3)
weight = torch.randn(3,3)
weight.requires_grad_()
i = [[0, 1, 1],
     [2, 0, 2]]
v =  [3.0, 4, 5]
adj = torch.sparse_coo_tensor(i, v, (2, 3), requires_grad=True)
def forward(input, adj):
    support = torch.mm(input, weight)
    output = torch.smm(adj, support)
    
forward(input, adj)
</code></pre>
<p>And this code runs perfectly.</p>
<p>So, if you let me know what exactly you are trying to achieve with <code>torch.spmm</code>, I might be able to help you further.</p>

username: srishti-git1110
cooked: <p>Additionally, find the list of available functions for sparse tensors here:<br>
<a href="https://pytorch.org/docs/stable/sparse.html#supported-linear-algebra-operations" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/sparse.html#supported-linear-algebra-operations</a></p>

username: work
cooked: <p>I want to perform a matrix multiplication of a sparse tensor with a dense tensor, both having <em>requires_grad=True</em>, by <code>torch.spmm()</code>.</p>
<p>After reading your reply, i found a function <code>tensor.sparse.mm()</code> that performs well on your given link.</p>
<p>Finally, i figure it out like this:</p>
<pre><code class="lang-auto">  def forward(self, input, adj):
        support = torch.mm(input, self.weight)
        output = torch.sparse.mm(adj, support)
</code></pre>
<p>Thank you for helping me again. You are really nice!</p>