Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear a model created with Keras and Tensorflow(as backend)?

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?

like image 306
Ravonrip Avatar asked Sep 02 '18 01:09

Ravonrip


People also ask

How do I clear a TensorFlow model?

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).

What does Keras backend clear session do?

Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited.

Is Keras the backend of TensorFlow?

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.

What are TensorFlow and Keras used for?

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.


1 Answers

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.

like image 175
g-eoj Avatar answered Sep 19 '22 04:09

g-eoj