Unexpected shape when computing gradient of the output with respect to the input

malfonsoarquimea: <p>Hi there!<br>
I have a tensor of shape <code>(number_of rays, number_of_points_per_ray, 3),</code> let’s call it <code>input</code>. <code>input</code> is passed through a model and some processing (all of this is differentiable), let’s call this process <code>inference</code>. Finally,  we get <code>output = inference(input)</code>, which has a shape of <code>(number_of_rays, number_of_points_per_ray, 300)</code>, where each “ray” in the output only depends on the same ray of the input. e.g. <code>output[i]</code>  only depends on <code>input[i]</code>. This means that for each set of 3 elements on the input the output has 300 elements, so I would expect to get a gradient with the same shape as the output</p>
<p>As explained <a href="https://discuss.pytorch.org/t/need-help-computing-gradient-of-the-output-with-respect-to-the-input/150950/5">here</a>, I tried <code>grads=torch.autograd.grad (outputs = output, inputs = input, grad_outputs = None)</code></p>
<p>but the output I am getting is of shape <code>(number_of rays, number_of_points_per_ray, 3)</code> , which is the same as the input and not the same as the output.</p>
<p>Any clue on what may I be doing wrong?<br>
Thanks in advance</p>

eqy: <p>I’m not sure I understand the question, as <code>torch.autograd.grad</code> is meant to compute the gradient with respect to the inputs: <code>inputs (sequence of Tensor) – Inputs w.r.t. which the gradient will be returned (and not accumulated into .grad).</code> Therefore the shapes of the returned values should match that of the inputs.</p>
<p>Additionally, I’m not sure how you are able to run <code>torch.autograd.grad</code> without specifying an gradient for the output if it is not a scalar value, as I would assume it would have <code>requires_grad=True</code> if it is produced from an input that has <code>requires_grad=True</code>. Otherwise, you may already have the gradient of the outputs as that is what you are meant to pass to <code>torch.autograd.grad</code> to get the gradients of the inputs.</p>

malfonsoarquimea: <p>Probably I explained it poorly.<br>
I am using a nerf-like process (that contains a neural network and many other things) that takes an origin point and a direction as input. Let’s call this set of origin and direction “ray”. For each input ray it:</p>
<ol>
<li>casts a ray and divides it into the coordinates (XYZ) of a certain number of samples that go from the origin point and along the given direction.</li>
<li>computes a feature that represents the estimated density of the space at each sample</li>
<li>decodes this feature into the estimated density of the given points for each frame in the input sequence. My input sequence is 300 frames, and hence, for each input ray, the process returns 300 values for each sample in the ray</li>
</ol>
<p>Now I would like to compute the normal vector, which is usually computed in other nerf-like models by computing the gradient of the density (the output of the process) with respect to the xyz coordinates. In nerf-like models for static scenes (only one frame), each sample is used to predict only one density value, but in my case, each sample is used to predict 300 density values.</p>
<p>Did I explain myself better?<br>
Thanks in advance!</p>

malfonsoarquimea: <p>The original post is, in fact and as you pointed out, very poorly explained, so I am dropping this post and opening a new one where I will  explain it much better</p>