Second autograd.grad gives error- Tensors appears to not have been used in the graph

username: Salman_Abbasi
cooked: <p>Hello</p>
<p>I am using the following code. I have to use autograd.grad twice. The first time it runs.</p>
<p>The second time. it gives the error “One of the differentiated Tensors appears to not have been used in the graph”. I tried my best to understand what is going wrong here, but could not solve it.</p>
<p>Help is much appreciated. The code is shown below</p>
<p>thanks<br>
salman<br>
import torch</p>
<p>x=torch.randn(10,3).float()<br>
x.requires_grad=True</p>
<p>y=torch.sum(x,1)<br>
y2=torch.flatten(y)</p>
<p>L1=torch.nn.Linear(10, 50) # 3 bbing x, y(depth),and time<br>
L2=torch.nn.Linear(50, 50)<br>
L3=torch.nn.Linear(50, 10)</p>
<p><span class="hashtag">#Model</span> Output<br>
u=L1(y2)<br>
u=L2(u)<br>
u=L3(u)</p>
<p>u=u.reshape(u.shape,1)</p>
<h1>
<a name="the-first-autogradgrad-works-ok-as-expected-1" class="anchor" href="#the-first-autogradgrad-works-ok-as-expected-1"></a>the first autograd.grad works ok as expected</h1>
<p>u_x=torch.autograd.grad(u,x,torch.ones(u.shape),retain_graph=True,create_graph=True)[0]<br>
<span class="hashtag">#error</span> in the last line<br>
u_xx=torch.autograd.grad(u_x,x,torch.ones(u_x.shape),create_graph=True)[0]</p>

username: soulitzer
cooked: <p>Your function looks to be linear, and the second order grads of that are zero. The issue is that when that is the case, sometimes (most of the time?) autograd will disconnect the graph. Autograd unfortunately don’t distinguish between zero gradients and no gradients flowing (i.e, no graph).</p>

username: Salman_Abbasi
cooked: <p>Thanks soulitzer. I made it a second derivative function, and it worked</p>

username: Salman_Abbasi
cooked: <p>Can this  also be corrected. This is a simplified example of the Helmohltz equation,  shown here:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/omniscientoctopus/Physics-Informed-Neural-Networks/blob/main/PyTorch/Helmholtz/Helmholtz.ipynb">
  <header class="source">

      <a href="https://github.com/omniscientoctopus/Physics-Informed-Neural-Networks/blob/main/PyTorch/Helmholtz/Helmholtz.ipynb" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/omniscientoctopus/Physics-Informed-Neural-Networks/blob/main/PyTorch/Helmholtz/Helmholtz.ipynb" target="_blank" rel="noopener nofollow ugc">omniscientoctopus/Physics-Informed-Neural-Networks/blob/main/PyTorch/Helmholtz/Helmholtz.ipynb</a></h4>


      <pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Import Libraries"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "import torch.autograd as autograd         # computation graph\n",
    "from torch import Tensor                  # tensor node in the computation graph\n",
    "import torch.nn as nn                     # neural networks\n",
    "import torch.optim as optim               # optimizers e.g. gradient descent, ADAM, etc.\n",
    "import time\n",
</code></pre>



  This file has been truncated. <a href="https://github.com/omniscientoctopus/Physics-Informed-Neural-Networks/blob/main/PyTorch/Helmholtz/Helmholtz.ipynb" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>import torch<br>
import numpy as np</p>
<p>x=torch.randn(10,3).float()<br>
x.requires_grad=True</p>
<p>L1=torch.nn.Linear(3, 50)<br>
L2=torch.nn.Linear(50, 50)<br>
L3=torch.nn.Linear(50, 1)</p>
<p><span class="hashtag">#Model</span> Output<br>
u=L1(x)<br>
u=L2(u)<br>
u=L3(u)</p>
<p>u=u.reshape(u.shape,1)</p>
<p>u_x=torch.autograd.grad(u,x,torch.ones(u.shape),retain_graph=True,create_graph=True)[0]<br>
u_xx=torch.autograd.grad(u_x,x,torch.ones(u_x.shape),create_graph=True)[0]</p>

username: soulitzer
cooked: <p>I wasn’t sure which part of your code you were referring to in the link, but from the excerpt below that still looks linear?</p>

username: Salman_Abbasi
cooked: <p>Thanks soulitzer. I agree. and it makes complete sense. I tried using the second(later) code in tensor flow, and it is giving me second gradient.  The second code model is linear, but the inputs are not related to the output y. Moreover, i have seen it work(second code) in pytorch as well. I just could not resolve it after so many days.</p>
<p>This example is made  out of my work on solving 2d wave equation. The u would be the solution and the three input are its coordinates, x,y, and t(x,y location coordinates and time).</p>