I'm training a LSTM based model in PyTorch 0.3.1.
My problem is that after increasing the learning rate I always get a RuntimeError
saying: element 0 of variables tuple is volatile
.
This does not happen at the beginning, but after some training, like in epoch 3, 4, 5 .. etc.
When looking after this error I found this Question on Stackoverflow suggesting to use zero_grad()
. But this was already in use when the error occurred.
So my questions are:
- What does it mean that an element of a variable is volatile?
- And what are possible causes for a variables element to get "volatile"?
- Is there a way to test which variable contains the volatile element, so that I can backtrace the problem?
Thanks a lot in advance for any help!
Here is the code of the training step I'm using:
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
(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.)
Traceback:
Traceback (most recent call last):
File "ent_lm.py", line 223, in <module>
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)