如何用角班轮计算损失
我正在使用Sklearn的KerasClassifier来包装我的KERAS型号,以执行K-折叠验证。
model = KerasClassifier(build_fn=create_model, epochs=20, batch_size=8, verbose = 1)
kfold = KFold(n_splits=10)
scoring = ['accuracy', 'precision', 'recall', 'f1']
results = cross_validate(estimator=model,
X=x_train,
y=y_train,
cv=kfold,
scoring=scoring,
return_train_score=True,
return_estimator=True)
然后,根据指标,我选择了函数返回的10个估计器之间的最佳模型:
best_model = results['estimators'][2] #for example the second model
现在,我想对 x_test 进行预测,并获得准确性和损失指标。我该怎么做?我尝试了 model.evaluate(x_test,y_test)
,但是模型是kerasclassifier,因此我会遇到错误。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要点是,您的 实例模拟标准 Scikit-Learn 分类器。换句话说,它是 scikit-learn beast ,而且它不提供方法
.evaluate()
。因此,您可能只会调用
best_model.score(x_test,y_test)
,它将自动返回准确性,因为标准的Sklearn分类器会返回。另一方面,您可以通过历史记录_
kerasclassifier
实例访问培训期间获得的损失值。这是一个示例:
最终观察到,如有疑问,您可以调用
dir(object)
获取指定对象的所有属性和方法的列表(dir(best_model)
在您的情况下)。Point is that your
KerasClassifier
instance mimics standard scikit-learn classifiers. In other terms, it is kind of a scikit-learn beast and, as is, it does not provide method.evaluate()
.Therefore, you might just call
best_model.score(X_test, y_test)
which will automatically return the accuracy as standard sklearn classifiers do. On the other hand, you can access the loss values obtained during training via thehistory_
attribute of yourKerasClassifier
instance.Here's an example:
Eventually observe that, when in doubt, you can call
dir(object)
to get the list of all properties and methods of the specified object (dir(best_model)
in your case).