Python 如何清除使用 Keras 和 Tensorflow(作为后端)创建的模型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52133347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:02:32  来源:igfitidea点击:

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

pythontensorflowkerasjupyter-notebook

提问by Ravonrip

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 forloop 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?

在 Jupyter Notebook 中使用 Keras 训练神经网络时遇到问题。我创建了一个带有多个隐藏层的序列模型。在训练模型并保存结果后,我想删除这个模型并在同一个会话中创建一个新模型,因为我有一个for循环来检查不同参数的结果。但据我所知,当我改变参数时,当我循环时,我只是向模型添加层(即使我network = Sequential()在循环内部再次初始化它)。所以我的问题是,我怎样才能完全清除以前的模型,或者我怎样才能在同一个会话中初始化一个全新的模型?

回答by g-eoj

keras.backend.clear_session()should clear the previous model. From https://keras.io/backend/:

keras.backend.clear_session()应该清除以前的模型。从https://keras.io/backend/

Destroys the current TF graph and creates a new one. Useful to avoid clutter from old models / layers.

销毁当前的 TF 图并创建一个新的图。有助于避免旧模型/层的混乱。