How to use torch.autograd.backward when variables are non-scalar

matliu: <p>hi, I am a newer of Pytorch. In recently, I encountered a problem about the function “torch.autograd.backward”. I have read the doc about it, while I cannot know  what it means for one parameter. The doc write “The graph is differentiated using the chain rule. <strong>If any of variables are non-scalar</strong> (i.e. their data has more than one element) and require gradient, the function additionaly requires specifying grad_variables.” Could someone give me a simple example about the variable are non-scalar and how to give the grad_variables?</p>
<p>Thank you very much for your attention!</p>

smth: <pre><code class="lang-auto">In [12]: x = Variable(torch.randn(10), requires_grad=True)

In [13]: y = x ** 2

In [14]: grad = torch.randn(10)

In [15]: torch.autograd.backward([y], [grad])
</code></pre>

matliu: <p>Dear smth, Thank you very much!</p>
<p>From what I understand, I think torch.autograd.backward([y], [grad]) is that we apply function grad to y. I do not know if it is right?</p>
<p>To understand the function deeply, I construct two examples to show what I want to do.</p>
<p>Firstly, I want to compute the derivation of y for all(any) elements in x. But I could not get it except I change <code>y = x**2</code> to <code>y = sum(x**2)</code>. Do I have another way to get it?</p>
<pre><code class="lang-python">import torch
from torch.autograd import Variable
import numpy as np

x = Variable(torch.arange(1,4), requires_grad=True)
y = x**2
 # torch.autograd.backward(x, y)  # ?? it is wrong
y.backward()
print(x.grad)
</code></pre>
<p>Furthermore, if I want to compute the derivation for <code>x = 4</code>? How can I write the code next?</p>
<p>Secondly, I want to compute the derivation of y and z for all(any) elements in x.  Can I write the code into a group by  <code>torch.autograd.backward([y], [grad])</code> ?</p>
<pre><code class="lang-python">import torch
from torch.autograd import Variable
import numpy as np

x = Variable(torch.arange(1,4), requires_grad=True)
y = sum(x**2)
z = sum(x*3)

y.backward()
print(x.grad)

 # how to set x.grad = 0??
z.backward()
print(x.grad)
</code></pre>

smth: <blockquote>
<p>From what I understand, I think torch.autograd.backward([y], [grad]) is that we apply function grad to y. I do not know if it is right?</p>
</blockquote>
<p>No. This will compute the gradient of all leaf nodes in the graph that <code>y</code> was created by. Leaf nodes are user-created Variables. In this case the only leaf node is <code>x</code>.</p>
<pre><code class="lang-auto">y = x ** 2
</code></pre>
<p>Since <code>y</code> is not a scalar here, we are giving the gradient wrt each output component y[0], y[1], y[2] etc. in the <code>grad</code> part of <code>torch.autograd.backward</code>. Hope that made it clearer.</p>

matliu: <p>Dear smth, thank you very much for your help! And I understood your idea! I  also find another related problem and wish it is helpful for others. Thanks!</p>
<aside class="quote quote-modified" data-post="1" data-topic="1059">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/8c91f0/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/clarification-using-backward-on-non-scalars/1059">Clarification - Using backward() on non-scalars</a> 
  </div>
  <blockquote>
    Hey, 
Sorry if this is obvious, but I find the description of the torch.autograd.backward(variables, grad_variables, retain_variables=False) function quite confusing. 
I’m working on a project where I have a vector of variables that I would like to differentiate to find the Jacobian. When it comes to implementing this, I’m not sure what form grad_variables should be or what a ‘sequence of Tensor’ is. I’ve tried many things, but all throw an error. 
Would anyone be able to point me in the directi…
  </blockquote>
</aside>


singhvishal0209: <aside class="quote no-group quote-modified" data-username="smth" data-post="4" data-topic="4191" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/48/13_2.png" class="avatar"> smth:</div>
<blockquote>
<pre><code class="lang-auto">Since `y` is not a scalar here, we are giving the gradient wrt each output component y[0], y[1], y[2] etc. in the `grad` part of `torch.autograd.backward`. I hope that made it clearer.
</code></pre>
</blockquote>
</aside>
<p>How do you initialize the [grad] vector? can we randomly initialize it?</p>

ptrblck: <p>You might initialize it randomly or with any other gradient value you would like to pass.<br>
However, the default value would be <code>torch.ones</code>.</p>

singhvishal0209: <p>Thanks  <a href="https://discuss.pytorch.org/u/ptrblck">ptrblck</a></p>