python - Tensorflow Keras 模型 : how to get the best score from a history object

标签 python tensorflow keras

我正在尝试使用 tensorflow keras 训练多个机器学习模型,我只是想知道有没有办法在训练完成后获得训练时取得的最佳分数。我在网上发现 .fit 函数返回一个历史对象,可以访问它以获得最好的分数,尽管从我试过的代码中它说“AttributeError:‘History’对象没有属性‘best_score’”,我找不到一个在线属性列表,所以这就是我在这里问的原因。

提前致谢。

History = model.fit(Xtrain, ytrain, epochs=1, validation_data=(Xtest, ytest), verbose=1)

print("Best: %f using %s" % (History.best_score, History.best_params_))

PS,我知道训练 1 个 epoch 不会有任何效果,我只是想测试代码

最佳答案

我假设您只想从历史对象中获得最好的分数。

hist = model.fit(...)

print(hist.history) # this will print a dictionary object, now you need to grab the metrics / score you're looking for

# if your score == 'acc', if not replace 'acc' with your metric

best_score = max(hist.history['acc'])

print(best_score)

如果您的指标是“准确性”,请改用它。

如果你想要最好的模型,你可以使用 ModelCheckPoint https://keras.io/callbacks/

关于python - Tensorflow Keras 模型 : how to get the best score from a history object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60896416/

相关文章:

python - 基于值列的下拉条形图(绘图)

python - 是否可以通过 TensorFlow 实现带有注意力的动态 RNN?

python - 使用 TensorFlow 和 tf.data.Dataset 从文件夹中采样图像序列

python - 如果我尝试拟合 pandas 数据框中的数据,如何将 input_shape 提供给卷积神经网络?

Keras - K.eval() 的逆

python - Django导入错误: Could not import settings

python - sklearn 错误 - 我已经填写了列的缺失值但仍然面临以下错误

python - 在 PyQt4 中向 MPL 图添加工具栏

python - tensorflow keras : Problem with loading the weights of the optimizer

python - 为什么在 Keras 中 Adam.iterations 总是设置为 0?