ValueError:在Keras中使用自定义度量时未知的度量函数

2024-05-19 12:52:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我做错什么了?根据凯拉斯的文件,我看不出我做错了什么。

编辑:

这是完整的回溯:

 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)

Tags: inpytruepackageslinelibrarysiteprecision