我正在尝试使用 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/