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"]]
能有人照顾解释问题出在哪里? 我在哪里错了?
谢谢