Can I find grad on the output of a nn wrt input?

Shresth_Somya: <pre><code class="lang-auto">class fun():
    def __init__(self, x):
        self.x = torch.tensor(x, requires_grad=True).double()
        self.loss_history = []
        self.model = nn.Sequential(
            nn.Linear(2,10),
            nn.ReLU(),
            nn.Linear(10,10),
            nn.ReLU(),
            nn.Linear(10,1)).double()

    def cost_function(self):
        output = self.model(self.x)
        temp = output*100
        dx = torch.autograd.grad(temp, self.x[:,0], torch.ones(self.x.size()[0], 1, device=device), create_graph=True, retain_graph=True, allow_unused=True)[0]
        loss = torch.sum(dx**2)
        
    def closure(self):
        self.optimizer.zero_grad()
        loss = self.cost_function()
        self.loss_history.append(loss)
        loss.backward(retain_graph=True)
        return loss

    # Training function
    def train(self, epochs, opt_func=torch.optim.Adam):
        torch.autograd.set_detect_anomaly(True)
        self.optimizer = opt_func(self.model.parameters())

        for epoch in range(epochs):
            self.optimizer.step(self.closure)

fun_model = fun(position_61x61)

fun_model.train(1000)
</code></pre>
<p>I am getting the following error</p>
<p><strong>TypeError</strong> : unsupported operand type(s) for ** or pow(): ‘NoneType’ and ‘int’</p>

swap: <p>Your dx is None. Could you print dx? If its None, storing dx as “Variable” might help!</p>

AlphaBetaGamma96: <p>As <a class="mention" href="/u/swap">@swap</a> said, your error emerges from the fact that <code>dx</code> is of type <code>None</code> and <code>None ** 2</code> is undefined. The use of <code>Variable()</code> is deprecated and shouldn’t be used, also when you define <code>self.x</code> as <code>torch.tensor(x, requires_grad=True).double()</code> you’re breaking your computation graph. Simply define it is as,</p>
<pre><code class="lang-auto">self.x = x.double().requries_grad=True
</code></pre>
<p>If you’re trying to get the gradient of the output w.r.t the inputs you can just do <code>dx = torch.autograd.grad(output, self.x, torch.ones_like(self.x), ...)</code> and redefine the loss to be <code>torch.sum( (100*dx)**2 )</code>. You can then use <code>torch.einsum</code> to format the output into the shape you want.</p>
<p>Also, your <code>cost_function</code> doesn’t return the loss so that’ll be a future error as well. Add <code>return loss</code>, and be careful with <code>retain_graph=True</code> as that might result in a memory leak if used improperly.</p>
<p>Finally, do you need to have everything within a class? Because it might be significantly easier to just write it as a script!</p>

Shresth_Somya: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="3" data-topic="154669">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p><code>x.double().requries_grad=True</code></p>
</blockquote>
</aside>
<p>x is an numpy object and need to be converted to tensor.<br>
so I tried this<br>
<code>self.x = torch.from_numpy(x).double()         self.x.requires_grad=True</code><br>
Again the grad returns None type. Can you explain why I am getting none as output for grad or a way to counter this problem. As far as I know that the output is an function of input, therefore we can find grad of output wrt to input. Also is reshaping an in place operation?</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="3" data-topic="154669">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p>Also, your <code>cost_function</code> doesn’t return the loss so that’ll be a future error as well. Add <code>return loss</code>, and be careful with <code>retain_graph=True</code> as that might result in a memory leak if used improperly.</p>
</blockquote>
</aside>
<p>Did you correct this mistake? The <code>cost_function</code> does return anything and hence will default to <code>None</code>.</p>

Shresth_Somya: <p>I corrected the cost_function but the error is same.</p>

AlphaBetaGamma96: <p>Can you share an updated version of your code? So I know what you’ve changed?</p>

Shresth_Somya: <pre><code class="lang-auto">class fun():
    def __init__(self, x):
        self.x = torch.from_numpy(x).double()
        self.x.requires_grad=True
        self.loss_history = []
        self.model = nn.Sequential(
            nn.Linear(2,10),
            nn.ReLU(),
            nn.Linear(10,10),
            nn.ReLU(),
            nn.Linear(10,1)).double()

    def cost_function(self):
        output = self.model(self.x)
        temp = output*100
        dx = torch.autograd.grad(temp, self.x[:,0], torch.ones_like(self.x[:,1].unsqueeze(-1)), create_graph=True, retain_graph=True, allow_unused=True)[0]
        loss = torch.sum(dx**2)
        return loss
        
    def closure(self):
        self.optimizer.zero_grad()
        loss = self.cost_function()
        self.loss_history.append(loss)
        loss.backward(retain_graph=True)
        return loss

    # Training function
    def train(self, epochs, opt_func=torch.optim.Adam):
        torch.autograd.set_detect_anomaly(True)
        self.optimizer = opt_func(self.model.parameters())

        for epoch in range(epochs):
            self.optimizer.step(self.closure)
            self.epoch_end(epoch, epochs, print_every=100)
    
    def epoch_end(self, epoch, epochs, print_every=100):
        if epoch == 0 or epoch == (epochs - 1) or epoch % print_every == 0 or print_every == 'all':
            print("Epoch [{}/{}], train_loss: {:.4f}".format(epoch, epochs, self.loss_history[epoch]))

</code></pre>
<p>this is the changed code.</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="Shresth_Somya" data-post="8" data-topic="154669">
<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/shresth_somya/48/50721_2.png" class="avatar"> Shresth_Somya:</div>
<blockquote>
<pre><code class="lang-auto">        dx = torch.autograd.grad(temp, self.x[:,0], torch.ones_like(self.x[:,1].unsqueeze(-1)), create_graph=True, retain_graph=True, allow_unused=True)[0]
</code></pre>
</blockquote>
</aside>
<p>Can you replace the line above with this below, and print out the shape of it?</p>
<pre><code class="lang-auto">dx = torch.autograd.grad(temp, self.x, torch.ones_like(temp), create_graph=True, retain_graph=True)[0]
print(dx.shape)
</code></pre>

Shresth_Somya: <p>The dx is no more none.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/8/a89f7bfc26d584d14a087bb544e22028ef60c5b6.png" data-download-href="https://discuss.pytorch.org/uploads/default/a89f7bfc26d584d14a087bb544e22028ef60c5b6" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/8/a89f7bfc26d584d14a087bb544e22028ef60c5b6.png" alt="image" data-base62-sha1="o3HUkGxdALBgL7Ru6olzS5pHvYq" width="690" height="318" data-dominant-color="262627"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">787×363 13.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
But I want to do calculations for only one column doing for both unnecessary increases complexity. Can you suggest something. Can you explain why I was getting none earlier.</p>

AlphaBetaGamma96: <p>If I had to guess, you can’t used <code>torch.autograd.grad</code> with respect to a part of an input like <code>x[:,0]</code>. <code>x[:,0]</code> isn’t part of the graph, but <code>x</code> is.</p>
<p>(Only guess so if someone does know, please do correct it)</p>