title: PyTorch: RuntimeError: element 0 of variables tuple is volatile
body: <p>I'm training a LSTM based model in PyTorch 0.3.1.</p>
  
  <p>My problem is that after increasing the learning rate I always get a <code>RuntimeError</code> saying: <code>element 0 of variables tuple is volatile</code>.</p>
  
  <p>This does not happen at the beginning, but after some training, like in epoch 3, 4, 5 .. etc.</p>
  
  <p>When looking after this error I found <a href="https://stackoverflow.com/questions/46513276/pytorch-backprop-through-volatile-variable-error">this Question on Stackoverflow</a> suggesting to use <code>zero_grad()</code>. But this was already in use when the error occurred.</p>
  
  <p>So my questions are: </p>
  
  <ul>
  <li>What does it mean that an element of a variable is volatile? </li>
  <li>And what are possible causes for a variables element to get "volatile"?</li>
  <li>Is there a way to test which variable contains the volatile element, so that I can backtrace the problem?</li>
  </ul>
  
  <p>Thanks a lot in advance for any help!</p>
  
  <p>Here is the code of the training step I'm using:</p>
  
  <pre><code>for epoch in range(num_epochs):
      states = (Var(torch.zeros(num_layers, batch_size, hidden_size)), 
                Var(torch.zeros(num_layers, batch_size, hidden_size)))
      new_batch = True
      step = 0
      epoch_loss = []
      print('Epoch: ', epoch+1)
      for i in range(0, token_ids.size(1) - seq_length, seq_length):
          #print(i)
          input_sequence = Var(token_ids[:,i:i+seq_length])
          target_sequence = Var(token_ids[:,(i+1):(i+1)+seq_length])
          entity_target_sequence = Var(entety_targets[:,(i+1):(i+1)+seq_length]).contiguous()
  
          if int(input_sequence )== 0:
              states = (Var(torch.zeros(num_layers, batch_size, hidden_size)), 
                        Var(torch.zeros(num_layers, batch_size, hidden_size)))
              print('New Document')
          model.zero_grad()
          states = detach(states)
          out, states, z = model(input_sequence, states)
  
          if new_batch:
              loss = loss_func(out, target_sequence.view(-1)) + bce_loss(z, entity_target_sequence)
              new_batch = False
          else:
              loss += loss_func(out, target_sequence.view(-1)) + bce_loss(z, entity_target_sequence)
          if (i+1) % wbatch_size == 0:
              step += 1 // seq_length
              if step % 10 == 0:
                  epoch_loss.append((loss.data[0]/wbatch_size))
                  print ('Epoch [%d/%d], Step[%d/%d], Loss: %.3f' % (epoch+1, num_epochs, step, num_wbatches, (loss.data[0]/wbatch_size)))
                  sys.stdout.flush()
  
              loss.backward(retain_graph=True)
              torch.nn.utils.clip_grad_norm(model.parameters(), 0.5)
              optimizer.step()
              new_batch = True
  </code></pre>
  
  <p>(I left out the model itself to avoid a wall of code here and to keep this readable, but if this helps to solve the problem I can of course add the code.)</p>
  
  <p>Traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "ent_lm.py", line 223, in &lt;module&gt;
      loss.backward(retain_graph=True)
    File "/usr/local/lib/python3.6/site-packages/torch/autograd/variable.py", line 167, in backward
      torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
    File "/usr/local/lib/python3.6/site-packages/torch/autograd/__init__.py", line 99, in backward
      variables, grad_variables, retain_graph)
  </code></pre>