【发布时间】:2017-11-20 09:31:58
【问题描述】:
我正在尝试编写自定义度量函数以在这样编写的编译步骤中设置:
self.model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=[self.dice_similarity_coefficient_metric,self.positive_predictive_value_metric,self.sensitivity_metric])
我是这样写的 Dice Similarity Coefficient、Positive Predictive Value 和 Similarity:
- 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(参见上面的回溯):您必须为占位符张量“dense_3_target”提供一个 dtype float 的值 [[节点:dense_3_target = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]
有人能解释一下问题出在哪里吗? 我哪里错了?
谢谢
【问题讨论】:
标签: customization keras conv-neural-network metrics loss-function