Why empty_like cause error?

username: How_Range
cooked: <p>If use empty_like , this error occur:<br>
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</p>
<pre><code class="lang-auto">class net(nn.Module):
    def __init__(self, num_class=10):
        super(net, self).__init__()
        self.fc1 = nn.Linear(8, 4)
        self.fc2 = nn.Linear(4, num_class)

        #freeze fc2 parameter
        for param in self.fc2.parameters():
            param.requires_grad = False
        print(self.parameters())

    def forward(self, x):
        # use pred to decide whether pred or fc1_output is the input of fc2
        # both pred and fc1_output have shape (1,4)
        pred = rand(1)
        fc1_output = self.fc1(x)
        # if use this , will error
        real_src = torch.empty_like(fc1_output)
        # correct
        real_src = fc1_output
        for i, p in enumerate(pred):
            if p &gt;= 0.5:
                real_src[i] = fc1_output[i]
            else:
                real_src[i] = pred[i]
        return self.fc2(real_src)

        # return self.fc2(self.fc1(x))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bc4d84e3d192bc71f7df5d23f5ee90fe988e0758.png" data-download-href="https://discuss.pytorch.org/uploads/default/bc4d84e3d192bc71f7df5d23f5ee90fe988e0758" title="3fa4e9a9e9562accd34c13fa4aed0cc"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc4d84e3d192bc71f7df5d23f5ee90fe988e0758_2_690x432.png" alt="3fa4e9a9e9562accd34c13fa4aed0cc" data-base62-sha1="qRNQB4eLE8WfuCMcDtUbMzbxqWs" width="690" height="432" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc4d84e3d192bc71f7df5d23f5ee90fe988e0758_2_690x432.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc4d84e3d192bc71f7df5d23f5ee90fe988e0758_2_1035x648.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc4d84e3d192bc71f7df5d23f5ee90fe988e0758_2_1380x864.png 2x" data-dominant-color="FCFBFB"><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">3fa4e9a9e9562accd34c13fa4aed0cc</span><span class="informations">3058×1916 244 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></p>

username: soulitzer
cooked: <p>Creating a new tensor breaks the autograd graph. The same will happen if you use ones_like, rand_like, randn_like.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; a = torch.tensor(1., requires_grad=True)
&gt;&gt;&gt; a.requires_grad
True
&gt;&gt;&gt; a.clone()  # This is OK
tensor(1., grad_fn=&lt;CloneBackward0&gt;)
&gt;&gt;&gt; torch.empty_like(a)
tensor(7.0065e-45)
&gt;&gt;&gt; b = torch.empty_like(a, requires_grad=True)
&gt;&gt;&gt; b.backward()
&gt;&gt;&gt; a.grad is None.  # even if you specify requires_grad, there's no gradient (it would be 0 anyway)
True
</code></pre>

username: How_Range
cooked: <aside class="quote no-group" data-username="soulitzer" data-post="2" data-topic="176049">
<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/s/839c29/48.png" class="avatar"> soulitzer:</div>
<blockquote>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; a = torch.tensor(1., requires_grad=True)
&gt;&gt;&gt; a.requires_grad
True
&gt;&gt;&gt; a.clone()  # This is OK
tensor(1., grad_fn=&lt;CloneBackward0&gt;)
&gt;&gt;&gt; torch.empty_like(a)
tensor(7.0065e-45)
&gt;&gt;&gt; b = torch.empty_like(a, requires_grad=True)
&gt;&gt;&gt; b.backward()
&gt;&gt;&gt; a.grad is None.  # even if you specify requires_grad, there's no gradient (it would be 0 anyway)
True
</code></pre>
</blockquote>
</aside>
<p>Thanks for your reply! But if I unfreeze the parameter of fc2 , even when I use empty_like , the program can work . Still don’t know why</p>

username: soulitzer
cooked: <aside class="quote no-group" data-username="How_Range" data-post="3" data-topic="176049">
<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/how_range/48/58858_2.png" class="avatar"> How_Range:</div>
<blockquote>
<p>But if I unfreeze the parameter of fc2 , even when I use empty_like , the program can work</p>
</blockquote>
</aside>
<p>Yeah the rest of your model (fc2 and beyond) would get trained, but the gradients would not flow back to fc1.</p>