I have a problem when training a neural net with Keras in Jupyter Notebook. I created a sequential model with several hidden layers. After training the model and saving the results, I want to delete this model and create a new model in the same session, as I have a for
loop that checks the results for different parameters. But as I understand the errors I get, when changing the parameters, when I loop over, I am just adding layers to the model (even though I initialise it again with network = Sequential()
inside the loop). So my question is, how can I completely clear the previous model or how can I initialise a completely new model in the same session?
Currently only TensorFlow backend supports proper cleaning up of the session. This can be done by calling K. clear_session() . This will remove EVERYTHING from memory (models, optimizer objects and anything that has tensors internally).
Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited.
The reason why Keras uses Tensorflow as it's backend is because it is an abstraction layer. It is the easiest way to get started with AI and machine learning because all of the core algorithms are implemented in tensorflow and keras allows you to just call the classes/functions without adding any additional code.
TensorFlow is an open-sourced end-to-end platform, a library for multiple machine learning tasks, while Keras is a high-level neural network library that runs on top of TensorFlow. Both provide high-level APIs used for easily building and training models, but Keras is more user-friendly because it's built-in Python.
keras.backend.clear_session()
should clear the previous model. From https://keras.io/backend/:
Destroys the current TF graph and creates a new one. Useful to avoid clutter from old models / layers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With