【发布时间】:2018-07-12 18:12:56
【问题描述】:
我需要在 Keras 中创建一个自定义损失函数,并根据条件返回两个不同的损失值的结果。我无法让 if 语句正常运行。
我需要做类似的事情:
def custom_loss(y_true, y_pred):
sees = tf.Session()
const = 2
if (sees.run(tf.keras.backend.less(y_pred, y_true))): #i.e. y_pred - y_true < 0
return const * mean_squared_error(y_true, y_pred)
else:
return mean_squared_error(y_true, y_pred)
尝试运行此程序时,我不断收到张量错误(见下文)。任何帮助/建议将不胜感激!
InvalidArgumentError: You must feed a value for placeholder tensor 'dense_63_target' with dtype float and shape [?,?]
[[Node: dense_63_target = Placeholder[dtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
【问题讨论】:
-
顺便说一句,我已经编辑了我的答案,以包含超出您要求的建议。
标签: python tensorflow machine-learning keras