gpt4 book ai didi

python - 值错误: Unknown metric function when using custom metric in Keras

转载 作者:行者123 更新时间:2023-11-30 08:23:23 26 4
gpt4 key购买 nike

Keras 2.x 取消了我需要使用的一堆有用指标,因此我将旧的metrics.py 文件中的函数复制到我的代码中,然后按如下方式包含它们。

def precision(y_true, y_pred): #taken from old keras source code
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
def recall(y_true, y_pred): #taken from old keras source code
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

...

model.compile(loss='categorical_crossentropy', optimizer='adam', 
metrics=['accuracy', precision, recall])

这会导致

ValueError: Unknown metric function:precision

我做错了什么?根据 Keras 文档,我看不出我做错了什么。

编辑:

这是完整的回溯:

 Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Python/2.7/site-packages/keras/models.py", line 274, in
load_model
sample_weight_mode=sample_weight_mode)
File "/Library/Python/2.7/site-packages/keras/models.py", line 824, in
compile
**kwargs)
File "/Library/Python/2.7/site-packages/keras/engine/training.py", line
934, in compile
handle_metrics(output_metrics)
File "/Library/Python/2.7/site-packages/keras/engine/training.py", line
901, in handle_metrics
metric_fn = metrics_module.get(metric)
File "/Library/Python/2.7/site-packages/keras/metrics.py", line 75, in get
return deserialize(str(identifier))
File "/Library/Python/2.7/site-packages/keras/metrics.py", line 67, in
deserialize
printable_module_name='metric function')
File "/Library/Python/2.7/site-packages/keras/utils/generic_utils.py",
line 164, in deserialize_keras_object
':' + function_name)
ValueError: Unknown metric function:precision
<FATAL> : Failed to load Keras model from file:
model.h5
***> abort program execution
Traceback (most recent call last):
File "classification.py", line 84, in <module>
'H:!V:FilenameModel=model.h5:NumEpochs=20:BatchSize=32')
#:VarTransform=D,G
TypeError: none of the 3 overloaded methods succeeded. Full details:
TMVA::MethodBase* TMVA::Factory::BookMethod(TMVA::DataLoader* loader,
TString theMethodName, TString methodTitle, TString theOption = "") =>
could not convert argument 2
TMVA::MethodBase* TMVA::Factory::BookMethod(TMVA::DataLoader* loader,
TMVA::Types::EMVA theMethod, TString methodTitle, TString theOption = "") =>
FATAL error (C++ exception of type runtime_error)
TMVA::MethodBase* TMVA::Factory::BookMethod(TMVA::DataLoader*,
TMVA::Types::EMVA, TString, TString, TMVA::Types::EMVA, TString) =>
takes at least 6 arguments (4 given)

最佳答案

从回溯看来,当您尝试加载保存的模型时,似乎出现了问题:

 Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Python/2.7/site-packages/keras/models.py", line 274, in
load_model
sample_weight_mode=sample_weight_mode)
...
ValueError: Unknown metric function:precision
<FATAL> : Failed to load Keras model from file:
model.h5

看看这个问题:https://github.com/keras-team/keras/issues/10104

加载模型时您需要添加自定义对象。例如:

dependencies = {
'auc_roc': auc_roc
}

model = keras.models.load_model(self.output_directory + 'best_model.hdf5', custom_objects=dependencies)

关于python - 值错误: Unknown metric function when using custom metric in Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51700351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com