如果Keras结果无法再现,那么比较模型和选择超参数的最佳实践是什么?

如果Keras结果无法再现,那么比较模型和选择超参数的最佳实践是什么?,第1张

如果Keras结果无法再现,那么比较模型和选择超参数的最佳实践是什么?

该问题似乎在Tensorflow 2.0中得以解决(至少在简单模型上如此)!这是一个似乎可以产生可重复结果的代码段。

import os####*IMPORANT*: Have to do this line *before* importing tensorflowos.environ['PYTHONHASHSEED']=str(1)import tensorflow as tfimport tensorflow.keras as kerasimport tensorflow.keras.layers import randomimport pandas as pdimport numpy as npdef reset_random_seeds():   os.environ['PYTHONHASHSEED']=str(1)   tf.random.set_seed(1)   np.random.seed(1)   random.seed(1)#make some random datareset_random_seeds()NUM_ROWS = 1000NUM_FEATURES = 10random_data = np.random.normal(size=(NUM_ROWS, NUM_FEATURES))df = pd.Dataframe(data=random_data, columns=['x_' + str(ii) for ii in range(NUM_FEATURES)])y = df.sum(axis=1) + np.random.normal(size=(NUM_ROWS))def run(x, y):    reset_random_seeds()    model = keras.Sequential([ keras.layers.Dense(40, input_dim=df.shape[1], activation='relu'), keras.layers.Dense(20, activation='relu'), keras.layers.Dense(10, activation='relu'), keras.layers.Dense(1, activation='linear')        ])    NUM_EPOCHS = 500    model.compile(optimizer='adam', loss='mean_squared_error')    model.fit(x, y, epochs=NUM_EPOCHS, verbose=0)    predictions = model.predict(x).flatten()    loss = model.evaluate(x,  y) #This prints out the loss by side-effect#With Tensorflow 2.0 this is now reproducible! run(df, y)run(df, y)run(df, y)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5645480.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存