错误-AttributeError:模块“ keras”没有属性“ sum”

时间:2020-07-21 10:58:37

标签: python keras

我正在使用以下辅助函数来计算Precision,Recall和F1-Score:

def recall_m(y_true, y_pred):
    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_m(y_true, y_pred):
    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 f1_m(y_true, y_pred):
    precision = precision_m(y_true, y_pred)
    recall = recall_m(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

我已经尝试了Keras的以下进口:

import keras as K
import keras

但是我得到了错误:

在f1_m中的

(y_true,y_pred) 12 13 def f1_m(y_true,y_pred): ---> 14精度= precision_m(y_true,y_pred) 15召回率= callback_m(y_true,y_pred) 16 return 2 *((precision * recall)/(precision + recall + K.epsilon()))

在precision_m(y_true,y_pred)中 6 7 def precision_m(y_true,y_pred): ----> 8个true_positives = K.sum(K.round(K.clip(y_true * y_pred,0,1))) 9个预测值= K.sum(K.round(K.clip(y_pred,0,1))) 10精度= true_positives /(predicted_positives + K.epsilon())

AttributeError:模块“ keras”没有属性“ sum”

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的可能方法:

导入以下内容:

import tensorflow.keras.backend as K

代替这些:

import keras as K
import keras