如何清除使用 Keras 和 Tensorflow(作为后端)创建的模型? [英] How can I clear a model created with Keras and Tensorflow(as backend)?

查看:51
本文介绍了如何清除使用 Keras 和 Tensorflow(作为后端)创建的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

keras.backend.clear_session() 应该清除之前的模型.来自 https://keras.io/backend/:

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

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

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

这篇关于如何清除使用 Keras 和 Tensorflow(作为后端)创建的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆