PyTorch equivalent of K.clear_session()

I am training models iteratively and would like to make sure that the session is cleared and the computational graph does not start from the last model’s update. I am deleting the models with del model but is there an equivalent of K.clear_session() to ensure destroying the computational graph and clearing session?

Thanks in advance!

There shouldn’t be a need for something like K.clear_session() since the graph should be automatically freed as long as there are no more references to it. So if model is the last variable referencing your graph, something like del model should do the trick. You might not even need del if model goes out of scope or you rebind your model variable to a new object.

If you find your memory usage still going up, you can add a call to gc.collect() to manually trigger python’s garbage collector. This might be necessary if your code isn’t CPU memory intensive.