python - 使用手动指标使用 hyperas 进行 Keras 超参数调整
问题描述
我正在使用hyperas 文档示例来调整网络参数,但基于 f1 分数而不是准确性。
我正在为 f1 分数使用以下实现:
from keras import backend as K
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
在以下代码行中更新 compile 函数的 metric 参数:
model.compile(loss='categorical_crossentropy', metrics=['accuracy'],
optimizer={{choice(['rmsprop', 'adam', 'sgd'])}})
至
model.compile(loss='categorical_crossentropy', metrics=[f1],
optimizer={{choice(['rmsprop', 'adam', 'sgd'])}})
上述指标在不使用 hyperas 的情况下完美运行,而当我尝试在调整过程中使用它时,我收到以下错误:
Traceback (most recent call last):
File "D:/path/test.py", line 96, in <module>
trials=Trials())
File "C:\Python35\lib\site-packages\hyperas\optim.py", line 67, in minimize
verbose=verbose)
File "C:\Python35\lib\site-packages\hyperas\optim.py", line 133, in base_minimizer
return_argmin=True),
File "C:\Python35\lib\site-packages\hyperopt\fmin.py", line 367, in fmin
return_argmin=return_argmin,
File "C:\Python35\lib\site-packages\hyperopt\base.py", line 635, in fmin
return_argmin=return_argmin)
File "C:\Python35\lib\site-packages\hyperopt\fmin.py", line 385, in fmin
rval.exhaust()
File "C:\Python35\lib\site-packages\hyperopt\fmin.py", line 244, in exhaust
self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
File "C:\Python35\lib\site-packages\hyperopt\fmin.py", line 218, in run
self.serial_evaluate()
File "C:\Python35\lib\site-packages\hyperopt\fmin.py", line 137, in serial_evaluate
result = self.domain.evaluate(spec, ctrl)
File "C:\Python35\lib\site-packages\hyperopt\base.py", line 840, in evaluate
rval = self.fn(pyll_rval)
File "D:\path\temp_model.py", line 86, in keras_fmin_fnct
NameError: name 'f1' is not defined
解决方案
如果您遵循链接到的代码示例,那么您不会让 hyperas 了解自定义 f1 函数。包作者也提供了一个例子来做到这一点。
简而言之,您需要在调用中添加一个额外的functions
参数optim.minimize()
。就像是
best_run, best_model = optim.minimize(model=model,
data=data,
functions=[f1],
algo=tpe.suggest,
max_evals=5,
trials=Trials())
实际上我今天刚刚实现了它,所以我相信你也可以让它工作:)
推荐阅读
- javascript - 具有不同点形状的两层
- git - 如果我们从本地推送到远程提交,它是分支提交 X 和功能提交 Y 的合并,但远程提交 Y 不存在,它会工作吗?
- java - 如何对两个数组进行排序但保持相应的顺序?
- javascript - Vue.js - 无法触发 DOM 更新
- pandas - 如何在 Pandas 的 DataFrame 中创建一个新的条件计数列
- reactjs - React Auth0 关键依赖:require 函数的使用方式是无法静态提取依赖
- python - 关于强度的 Python 图形库
- javascript - 如何在反应中使用带有 async/await 语法的 Axios?
- javascript - Array.fill 和 [...] 之间的行为差异
- git - Git:自从搬家后,您如何恢复已删除的文件?