繁体   English   中英

keras.backend 的 clear_session() 方法没有清理拟合数据

[英]The clear_session() method of keras.backend does not clean up the fitting data

我正在比较不同类型数据质量的拟合精度结果。 “好数据”是特征值中没有任何 NA 的数据。 “坏数据”是特征值中具有 NA 的数据。 应该通过一些值校正来修复“坏数据”。 作为值校正,它可能会用零或平均值替换 NA。

在我的代码中,我试图执行多个拟合程序。

查看简化的代码:

from keras import backend as K
...

xTrainGood = ... # the good version of the xTrain data 

xTrainBad = ... #  the bad version of the xTrain data

...

model = Sequential()

model.add(...)

...

historyGood = model.fit(..., xTrainGood, ...) # fitting the model with 
                                              # the original data without
                                              # NA, zeroes, or the feature mean values

查看拟合精度plot,基于historyGood数据:

在此处输入图像描述

之后,代码重置存储的 model 并使用“坏”数据重新训练 model:

K.clear_session()

historyBad = model.fit(..., xTrainBad, ...)

根据historyBad数据查看拟合过程结果:

在此处输入图像描述

可以注意到,初始精度> 0.7 ,这意味着 model “记住”了之前的拟合。

为了比较,这是“坏”数据的独立拟合结果:

在此处输入图像描述

如何将 model 重置为“初始”state?

K.clear_session()不足以重置状态并确保可重复性。 您还需要:

  • 设置(和重置)随机种子
  • 重置 TensorFlow 默认图
  • 删除以前的 model

完成以下各项的代码。

reset_seeds()
model = make_model() # example function to instantiate model
model.fit(x_good, y_good)

del model
K.clear_session()
tf.compat.v1.reset_default_graph()

reset_seeds()
model = make_model()
model.fit(x_bad, y_bad)

请注意,如果其他变量引用 model,您也应该del它们 - 例如model = make_model(); model2 = model model = make_model(); model2 = model --> del model, model2 - 否则它们可能会持续存在。 最后, tf随机种子不像randomnumpy那样容易重置,并且需要事先清除图形。


使用的功能/模块

 import tensorflow as tf import numpy as np import random import keras.backend as K def reset_seeds(): np.random.seed(1) random.seed(2) if tf.__version__[0] == '2': tf.random.set_seed(3) else: tf.set_random_seed(3) print("RANDOM SEEDS RESET")

您以错误的方式使用K.clear_session() ,要获得具有随机初始化权重的 model,您应该删除旧的 model(使用del关键字),然后继续创建一个新的 Z20F35E630DAF44DB8C4C3F6。

您可以在每个拟合过程之后使用K.clear_session()

实例化一个新的model object同名还不够?

model = make_model()

在单个文件中循环训练多个模型时,我遇到了类似的问题。 我在 Keras/TF(2.5 版)上尝试了很多东西,包括这个线程中的答案。 除了一件事之外没有任何帮助 - 使用子进程模块从另一个文件运行一个文件,这可以确保 kernel 每次都重新启动。

以最简单的方式,您可以将训练代码保存在单个文件中,并访问它以运行初始 model 或从不同文件重新运行后续 model。 要从另一个文件运行一个文件,只需在第二个文件中执行:

run_no = [0,1,2,3]
for i in range(len()):
    subprocess.run(["ipython", "your_main_file.ipynb", str(i)])    # for jupyter
    #subprocess.run(["python3", "your_main_file.py", str(i)])      # for python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM