Difference between "detach()" and "with torch.nograd()" in PyTorch? : <p>I know about two ways to exclude elements of a computation from the gradient calculation <code>backward</code></p>

<p><strong>Method 1:</strong> using <code>with torch.no_grad()</code></p>

<pre><code>with torch.no_grad():
    y = reward + gamma * torch.max(net.forward(x))
loss = criterion(net.forward(torch.from_numpy(o)), y)
loss.backward();
</code></pre>

<p><strong>Method 2:</strong> using <code>.detach()</code></p>

<pre><code>y = reward + gamma * torch.max(net.forward(x))
loss = criterion(net.forward(torch.from_numpy(o)), y.detach())
loss.backward();
</code></pre>

<p>Is there a difference between these two? Are there benefits/downsides to either?</p>
