Stack expects a non-empty Tensor List pytorch while using gradient clipping

kl_divergence: <p>I want to use gradient clipping</p>
<pre><code class="lang-auto">torch.nn.utils.clip_grad_norm(loss, model.parameters()
</code></pre>
<p>I’m getting the error <code>stack expects a non-empty Tensor List</code>. I read in the forums, <a class="mention" href="/u/tom">@tom</a> highlighted that gradients need to be computed first (<code>loss.backward()</code>) before <code>calling clip_grad_norm_</code> (not much info in the documentation). I’m updating the model’s parameter in a bit different manner.</p>
<pre><code class="lang-auto">grad = torch.autograd.grad(loss, model.parameters())
</code></pre>
<p>And then doing the updating manually.</p>
<p>How can gradient clipping be used in this case ?</p>

tom: <p>Personally, I’d call that variable <code>grads</code> rather than <code>grad</code>.</p>
<p>You can grab <code>clip_grad_norm_</code> from the source (link below) and then convert</p>
<pre><code class="lang-auto">for p in parameters:
   something with p.grad
</code></pre>
<p>into</p>
<pre><code class="lang-auto">for gr in grads:
  something with gr
</code></pre>
<p>Best regards</p>
<p>Thomas</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/bcb44796ba00f9ac5f22e33f1de3a24b277c4c3a/torch/nn/utils/clip_grad.py#L6" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/bcb44796ba00f9ac5f22e33f1de3a24b277c4c3a/torch/nn/utils/clip_grad.py#L6" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/bcb44796ba00f9ac5f22e33f1de3a24b277c4c3a/torch/nn/utils/clip_grad.py#L6</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="1" style="counter-reset: li-counter 0 ;">
<li>import warnings</li>
<li>import torch</li>
<li>from torch._six import inf</li>
<li>
</li><li>
</li><li class="selected">def clip_grad_norm_(parameters, max_norm, norm_type=2):</li>
<li>    r"""Clips gradient norm of an iterable of parameters.</li>
<li>
</li><li>    The norm is computed over all gradients together, as if they were</li>
<li>    concatenated into a single vector. Gradients are modified in-place.</li>
<li>
</li><li>    Arguments:</li>
<li>        parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a</li>
<li>            single Tensor that will have gradients normalized</li>
<li>        max_norm (float or int): max norm of the gradients</li>
<li>        norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


kl_divergence: <p>Thanks for replying but I’m still not sure clip norm in this case. Inside the function,  we don’t calculate <code>grads</code>, so do I compute that and then make modifications. I see that this clipping operation is applied on params, what changes do I make if I want an equivalent operation on <code>grads</code> then. And is this all really needed ? The only difference in how I update the model params.</p>