Conceptual question regarding gradient

username: meyro123
cooked: <p>Hi everyone,</p>
<p>I’m currently trying to train a physics informed NN and I have a (probably very) simple question regarding autograd.<br>
My training samples are points in space and time, i.e. a two-dimensional tensor and my question is as follows. When I compute the gradient wrt. the training data, i.e.</p>
<p>gradient = torch.autograd.grad(u,<br>
trainingData,<br>
create_graph=True,<br>
allow_unused=True)</p>
<p>and I want to obtain the time derivative (first component of training data) of u, does the time derivative coincide with this quantity:</p>
<p>dtU = gradient[0][0]?</p>
<p>And similarly, If I want to obtain the spatial derivative, would It be this quantity:</p>
<p>dxU = gradient[0][1]?</p>
<p>Thank you very much for your help!</p>
<p>Best<br>
Fabian</p>

username: KFrank
cooked: <p>Hi Fabian!</p>
<aside class="quote no-group" data-username="meyro123" data-post="1" data-topic="100674" data-full="true">
<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/3ab097/48.png" class="avatar"> meyro123:</div>
<blockquote>
<p>My training samples are points in space and time, i.e. a two-dimensional tensor</p>
</blockquote>
</aside>
<p>I don’t understand what you are saying here.</p>
<aside class="quote no-group">
<blockquote>
<p>gradient = torch.autograd.grad(u,<br>
trainingData,<br>
create_graph=True,<br>
allow_unused=True)</p>
<p>dtU = gradient[0][0]?</p>
<p>dxU = gradient[0][1]?</p>
</blockquote>
</aside>
<p>The following example script may answer part of your question:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
torch.__version__

def  f (x, y):
    return x * y

x = torch.Tensor ([2.0])
x.requires_grad = True
y = torch.Tensor ([3.0])
y.requires_grad = True
u = f (x, y)
gradient = torch.autograd.grad (u, (x, y), create_graph=True, allow_unused=True)
gradient[0]
gradient[1]
</code></pre>
<p>Here is its output:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.6.0'
&gt;&gt;&gt;
&gt;&gt;&gt; def  f (x, y):
...     return x * y
...
&gt;&gt;&gt; x = torch.Tensor ([2.0])
&gt;&gt;&gt; x.requires_grad = True
&gt;&gt;&gt; y = torch.Tensor ([3.0])
&gt;&gt;&gt; y.requires_grad = True
&gt;&gt;&gt; u = f (x, y)
&gt;&gt;&gt; gradient = torch.autograd.grad (u, (x, y), create_graph=True, allow_unused=True)
&gt;&gt;&gt; gradient[0]
tensor([3.], grad_fn=&lt;MulBackward0&gt;)
&gt;&gt;&gt; gradient[1]
tensor([2.], grad_fn=&lt;MulBackward0&gt;)
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

username: meyro123
cooked: <p>Hi Frank,</p>
<p>thank you very much for your explanation, it is indeed very helpful!</p>
<aside class="quote no-group quote-modified" data-username="KFrank" data-post="2" data-topic="100674">
<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/k/ecb155/48.png" class="avatar"> KFrank:</div>
<blockquote>
<aside class="quote no-group" data-username="meyro123" data-post="1" data-topic="100674">
<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/3ab097/48.png" class="avatar"> meyro123:</div>
<blockquote>
<p>My training samples are points in space and time, i.e. a two-dimensional tensor</p>
</blockquote>
</aside>
<p>I don’t understand what you are saying here.</p>
</blockquote>
</aside>
<p>Sorry, I was very unspecific. I wanted to say that the input for my NN is simply (t,x)\in \R^2.</p>
<p>Again, thank you very much.</p>
<p>Best<br>
Fabian</p>