python - 如何从历史中访问指标
问题描述
对于回归问题,我想比较一些指标,但我只能accuracy
从历史中得到,这对回归目的毫无意义。我怎样才能获得其他指标等mean_squared_error
?
create_model(...)
input_layer = ...
output_laye = ...
model = Model(input_layer, output_layer)
model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
return model
model = KerasRegressor(build_fn=create_model, verbose=0)
batch_size = [1, 2]
epochs = [1, 2]
optimizer = ['Adam', 'sgd']
param_grid = dict(batch_size=batch_size
, optimizer = optimizer
)
grid_obj = RandomizedSearchCV(estimator=model
, param_grid=hypparas
, n_jobs=1
, cv = 3
, scoring = ['explained_variance', 'neg_mean_squared_error', 'r2']
, refit = 'neg_mean_squared_error'
, return_train_score=True
, verbose = 2
)
grid_result = grid_obj.fit(X_train1, y_train1)
X_train1, X_val1, y_train1, y_val1 = train_test_split(X_train1, y_train1, test_size=0.2, shuffle=False)
grid_best = grid_result.best_estimator_
history = grid_best.fit(X_train1, y_train1
, validation_data=(X_val1, y_val1)
)
print(history.history.keys())
> dict_keys(['val_loss', 'val_accuracy', 'loss', 'accuracy'])
我已经看到https://stackoverflow.com/a/50137577/6761328得到例如
history.history['accuracy']
哪个有效,但我无法访问mean_squared_error
或其他:
history.history['neg_mean_squared_error']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-473-eb96973bf014> in <module>
----> 1 history.history['neg_mean_squared_error']
KeyError: 'neg_mean_squared_error'
这个问题最后是关于如何比较不同指标的后续问题?因为我认为这个问题是另一个问题的答案。
解决方案
In stand-alone Keras (not sure for the scikit-learn wrapper), history.history['loss']
(or val_loss
respectively for the validation set) would do the job.
Here, 'loss'
and 'val_loss'
are keys; give
print(history.history.keys())
to see what keys are available in your case, and you will find among them the required ones for the loss (might even be the same, i.e. 'loss'
and 'val_loss'
).
As a side note, you should remove completely metrics=['accuracy']
from your model compilation - as you correctly point out, accuracy is meaningless in regression settings (you might want to check What function defines accuracy in Keras when the loss is mean squared error (MSE)?).
推荐阅读
- python - 为什么 pathlib.Path(__file__).parent.parent 对我的工作目录敏感?
- javascript - 未捕获的 RangeError:在 jquery ajax 调用中超出了最大调用堆栈大小
- angular - Swagger CodeGen,使代理删除某些元素并被简化
- oracle - ORA-00904: "S"."AIR_TIME": 标识符无效
- conv-neural-network - 我可以在任何任务中使用 ROI Pooling 从整个图像的特征图中提取区域特征吗?
- javascript - 自动完成无法通过 https laravel php
- angular - Angular 11 Firebase 服务器端渲染无限加载/挂起
- google-translate - 谷歌翻译与谷歌云翻译
- python - Keras 中的 1D CNN:如果过滤器的数量和 kernel_size 太低,它会在序列中间停止卷积吗?
- php - Codeigniter 4 在最新的 PHP 8 中不起作用