1

我想編寫自定義指標函數編譯步驟設置這樣寫道:構建自定義指標與Keras損失函數,有錯誤

self.model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=[self.dice_similarity_coefficient_metric,self.positive_predictive_value_metric,self.sensitivity_metric]) 

我寫骰子相似係數,陽性預測值和相似性是這樣的:

  • FP =假陽性
  • TP =真陽性
  • FN =假陰性

def dice_similarity_coefficient_metric(self, y_true, y_pred): 
     y_true = np.array(K.eval(y_true)) 
     y_pred = np.array(K.eval(y_pred)) 
     FP = np.sum(y_pred & np.logical_not(y_true)).astype(float) 
     TP = np.sum(y_true & y_pred).astype(float) 
     FN = np.sum(np.logical_not(y_pred) & 
     np.logical_not(y_true)).astype(float) 
     return K.variable(np.array((2 * TP)/(FP + (2 * TP) + FN + 
     K.epsilon()))) 

def positive_predictive_value_metric(self, y_true, y_pred): 
     y_true = np.array(K.eval(y_true)) 
     y_pred = np.array(K.eval(y_pred)) 
     FP = np.sum(y_pred & np.logical_not(y_true)).astype(float) 
     TP = np.sum(y_true & y_pred).astype(float) 
     return K.variable(np.array(TP/(FP + TP + K.epsilon()))) 

def sensitivity_metric(self, y_true, y_pred): 
     y_true = np.array(K.eval(y_true)) 
     y_pred = np.array(K.eval(y_pred)) 
     TP = np.sum(y_true & y_pred).astype(float) 
     FN = np.sum(np.logical_not(y_pred) & 
     np.logical_not(y_true)).astype(float) 
     return K.variable(np.array(TP/(TP + FN + K.epsilon()))) 

當我運行的代碼,我有以下錯誤:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_3_target' with dtype float [[Node: dense_3_target = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

能有人照顧解釋問題出在哪裏? 我在哪裏錯了?

謝謝

回答

0

也許,這是更好地定義使用後端功能指標。例如:

y_true = np.array([[1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 1.0]], dtype=np.float32) 
y_pred = np.array([[0.3, 0.99, 0.99, 0.1], [0.6, 0.99, 0.99, 0.1], [0.1, 0.99, 0.99, 0.1]], dtype=np.float32) 
n_fn = np.sum((y_true - y_pred) > 0.5) 
Y_true = K.placeholder((None, 4), dtype=K.floatx()) 
Y_pred = K.placeholder((None, 4), dtype=K.floatx()) 
n_fn = false_negatives(Y_true, Y_pred).eval(inputs_to_values={Y_true: y_true, Y_pred: y_pred}) 

HTH

def false_negatives(Y_true, Y_pred): 
    return K.sum(K.round(K.clip(Y_true - Y_pred, 0, 1))) 

它可以在用5 FN的示例性數據來檢查