Rub*_*mov 4 python keras tensorflow keras-2 tf.keras
我正在比较不同类型数据质量的拟合精度结果。“好数据”是特征值中没有任何 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
Run Code Online (Sandbox Code Playgroud)
根据historyGood数据查看拟合精度图:
之后,代码重置存储的模型并使用“坏”数据重新训练模型:
K.clear_session()
historyBad = model.fit(..., xTrainBad, ...)
Run Code Online (Sandbox Code Playgroud)
根据historyBad数据查看拟合过程结果:
可以注意到,初始精度> 0.7,这意味着模型“记住”了之前的拟合。
为了比较,这是“坏”数据的独立拟合结果:
如何将模型重置为“初始”状态?
K.clear_session()不足以重置状态并确保可重复性。您还需要:
代码完成下面的每一个。
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)
Run Code Online (Sandbox Code Playgroud)
请注意,如果其他变量引用模型,您del也应该使用它们 - 例如model = make_model(); model2 = model--> del model, model2- 否则它们可能会持续存在。最后,tf随机种子不像random's 或numpy's那样容易重置,并且需要事先清除图形。
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")
Run Code Online (Sandbox Code Playgroud)