I am trying to run the following code: for ie in range(100): energy = torch.tensor([.1 + .1 * ie], dtype = torch.float32, requires_grad = True) xe = .5 + 20 * (torch.log(energy) - log_01) / (log_10 - log_01) #(log_ parts are some constants) xe2 = xe * xe for it in range(100): theta = torch.tensor([it * theta_max / 99], dtype = torch.float32, requires_grad = True) #Some function of xe2 and theta, evaluating the either the values, first derivatives, or second derivatives using `torch.autograd.grad()` This code seem to work really slowly, and the cuda is not available on my computer, so I cannot use torch.cuda.clear_cache(). I have also tried using del theta, or del energy after their iteration over the value is done, but it doesn't seem it improves that much. Is there a way to delete the computational graphs after each iteration so that I save some memory. The code is extremely slow as for now. Just to give an idea, in the inner loop I have tried range 1, 2, and 3. The time it takes to compile is 1 min, 2 min 30 sec, 10 min respectively. Continue reading...