RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
I met the above error when I simply changed the last MLP network to the tcnn-NGP network.
I printed the batchidx before backward and found the error occurred at loss.backward() at the second batch. However, the error disappeared when using MLP.
batch_idx 0
batch_idx 1
Traceback (most recent call last):
File "train.py", line 32, in <module>
main()
File "train.py", line 25, in main
trainer.train(epoch=epoch,
File "core/train/trainers/projectx/trainer.py", line 193, in train
train_loss.backward()
File "/root/miniconda3/envs/ngp/lib/python3.8/site-packages/torch/_tensor.py", line 488, in backward
torch.autograd.backward(
File "/root/miniconda3/envs/ngp/lib/python3.8/site-packages/torch/autograd/__init__.py", line 197, in backward
Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
I guess it's because some tensors in tcnn still need to calculate gradients when calling backward for the second time. However, when I tried to print the attributes that require gradients, I found that there were no tensors that required gradients except for two networks.
for name, param in model.named_parameters():
if param.requires_grad:
print(name, param.shape)
RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
I met the above error when I simply changed the last MLP network to the tcnn-NGP network. I printed the batchidx before backward and found the error occurred at loss.backward() at the second batch. However, the error disappeared when using MLP.
I guess it's because some tensors in tcnn still need to calculate gradients when calling backward for the second time. However, when I tried to print the attributes that require gradients, I found that there were no tensors that required gradients except for two networks.
The following is my code.