Error when computing gradients

edshkim98: <p>Hi, I am trying to use my customized embedding loss for learning.<br>
However, I have been getting the error below:</p>
<p><code>RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</code></p>
<p>Below is my code for the loss function:</p>
<pre><code class="lang-auto">def disc_loss(output,num, x=None,y=None):
    if pred == True:
        #pdist = nn.PairwiseDistance(p=2)
        #loss = pdist(x,y).sum()
        num = x.size(1)
        loss = torch.sqrt(torch.sum((x-y)**2))
    return loss
</code></pre>
<p>x and y in the function are the embedding that I got from intermediate conv layer.<br>
I keep getting the error message during .backward() function, but I am not sure what is wrong with my implementation. Can somebody help me with this?<br>
Thank you!</p>

edshkim98: <p>I think the problem is solved by using requires_grad = True for loss</p>

omarfoq: <p>Hello,</p>
<p>Seems that you want to Implement MSELoss? In that case use the standard MSELoss criterion.</p>

edshkim98: <p><a class="mention" href="/u/omarfoq">@omarfoq</a>  Hi, yea I forgot about MSELoss. However, I’ve just tried with MSELoss and it gives same error which seems very weird <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

omarfoq: <p>In that case, the problem is not related to this part. Can you please share how you feed X and Y to the loss function, or if possible share the part of code corresponding to your training loop?</p>

edshkim98: <p><a class="mention" href="/u/omarfoq">@omarfoq</a>  Thank you for the help!<br>
Here is a part of my code</p>
<pre><code class="lang-auto">                output,em1 = disc(real_image.float()) #Real point cloud data
                _,em2 = disc(inputs2.float()) #Fake or predicted point cloud data
            ls_fake = disc_loss(output, num=1,x=em1,y=em2,pred=True)
            ls_fake.backward()
</code></pre>
<p>Here, disc is a model that I am using and em1 and em2 are embedding or features from intermediate layer. So, I basically have to compute MSELoss based on the two em1 and em2. But, I get an error at ls_fake.backward()</p>

edshkim98: <p><a class="mention" href="/u/omarfoq">@omarfoq</a>  Let me know if you need more information!</p>

omarfoq: <p>Hello Seems that your computation graph gets disconnected when you get em1 and em2, can you show how you get them from <code>disc</code></p>

edshkim98: <p><a class="mention" href="/u/omarfoq">@omarfoq</a><br>
Below is a part of the code for my model, disc.</p>
<pre><code class="lang-auto">def forward(self, inputs):
................
..... More code
...............
        x3 = x2.max(dim=-1, keepdim=False)[0]
        em = x3
        
        x = F.leaky_relu(self.bn6(self.linear1(x3)), negative_slope=0.2) # (batch_size, emb_dims*2) -&gt; (batch_size, 512)
        #x = self.dp1(x)
        x = F.leaky_relu(self.bn7(self.linear2(x)), negative_slope=0.2) # (batch_size, 512) -&gt; (batch_size, 256)
        x = self.dp2(x)
        x = self.linear3(x)                                             # (batch_size, 256) -&gt; (batch_size, output_channels)
        
        return x,em
</code></pre>
<p>em is just the output striaght from the convolution and maxpooling.</p>

omarfoq: <p>Hello,</p>
<p>The problem is in</p>
<pre><code class="lang-auto">em = x2.max(dim=-1, keepdim=False)[0]
</code></pre>
<p>I think you can’t propagte this operation, have a look to this <a href="https://discuss.pytorch.org/t/confused-about-torch-max-and-gradient/14283">topic</a> and this <a href="https://discuss.pytorch.org/t/confused-about-torch-max-and-gradient/14283/7">post</a></p>

edshkim98: <p><a class="mention" href="/u/omarfoq">@omarfoq</a>  So does this mean I shoud use pytorch built in maxpooling function for this right?</p>

omarfoq: <p>That’s one possibility. I think a better implementation may take advantage of using hooks. I think you can keep the original and use <code>register_forward_hook</code> during the forward phase to get the embedding.</p>

edshkim98: <aside class="quote no-group" data-username="omarfoq" data-post="12" data-topic="116645">
<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/omarfoq/48/27473_2.png" class="avatar"> omarfoq:</div>
<blockquote>
<p>register_forward_hook</p>
</blockquote>
</aside>
<p><a class="mention" href="/u/omarfoq">@omarfoq</a>  Thank you very much for the help! Could you give me an example how I can use register_forward_hook for my case?</p>

omarfoq: <p>Hello,</p>
<pre><code class="lang-auto">x = x.to(device)

embeddings = {}
def hook_fn(model, input_, output):
    activation["embeddings"] = output.squeeze()

model.features.register_forward_hook(hook_fn)

</code></pre>
<p>In my case <code>features</code> is the name of the before last layer in the network.</p>