我正在尝试使用自定义损失函数来防止误报。我发现了这篇帖子Custom loss function in Keras to penalize false negatives,与我的帖子非常相似,但是当我实现这些功能时,模型将向我返回下一个错误:
model.compile(loss = loss, optimizer = 'Adadelta', metrics = [auroc])
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\keras\engine\training.py", line 860, in compile
sample_weight, mask)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\keras\engine\training.py", line 460, in weighted
score_array = fn(y_true, y_pred)
File "EmbeddingConcatenate.py", line 268, in recall_spec_loss
return binary_recall_specificity(y_true, y_pred, recall_weight, spec_weight)
File "EmbeddingConcatenate.py", line 248, in binary_recall_specificity
TN = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 0)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py", line 644, in eval
return to_dense(x).eval(session=get_session())
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 648, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 4758, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
run_metadata_ptr)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1344, in _do_run
options, run_metadata)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1363, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'dense_1_target' with dtype float and shape [?,?]
[[Node: dense_1_target = Placeholder[dtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: dense_1_target/_29 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_4_dense_1_target", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'dense_1_target', defined at:
File "EmbeddingConcatenate.py", line 398, in <module>
model = generate_model()
File "EmbeddingConcatenate.py", line 296, in generate_model
model.compile(loss = loss, optimizer = 'Adadelta', metrics = [auroc])
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\keras\engine\training.py", line 755, in compile
dtype=K.dtype(self.outputs[i]))
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py", line 488, in placeholder
x = tf.placeholder(dtype, shape=shape, name=name)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1680, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 4105, in _placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 3160, in create_op
op_def=op_def)
File "C:\Users\X\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 1625, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_1_target' with dtype float and shape [?,?]
[[Node: dense_1_target = Placeholder[dtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: dense_1_target/_29 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_4_dense_1_target", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
关于如何解决它的任何想法?我还有其他选择可以防止误报吗?
修改
我留下了我在这里使用的代码(与另一篇文章完全相同):
import keras.backend as K
def binary_recall_specificity(y_true, y_pred, recall_weight, spec_weight):
TN = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 0)
TP = np.logical_and(K.eval(y_true) == 1, K.eval(y_pred) == 1)
FP = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 1)
FN = np.logical_and(K.eval(y_true) == 1, K.eval(y_pred) == 0)
# Converted as Keras Tensors
TN = K.sum(K.variable(TN))
FP = K.sum(K.variable(FP))
specificity = TN / (TN + FP + K.epsilon())
recall = TP / (TP + FN + K.epsilon())
return 1.0 - (recall_weight*recall + spec_weight*specificity)
# Our custom loss' wrapper
def custom_loss(recall_weight, spec_weight):
def recall_spec_loss(y_true, y_pred):
return binary_recall_specificity(y_true, y_pred, recall_weight, spec_weight)
# Returns the (y_true, y_pred) loss function
return recall_spec_loss
loss_custom = custom_loss(recall_weight = 0.9, spec_weight = 0.1)
model.compile(loss = loss_custom, optimizer = 'Adadelta', metrics = [auroc])