gpt4 book ai didi

deep-learning - keras:平滑 L1 损失

转载 作者:行者123 更新时间:2023-12-04 07:29:15 27 4
gpt4 key购买 nike

尝试在 keras 中自定义损失函数(平滑 L1 损失),如下所示

ValueError: Shape must be rank 0 but is rank 5 for 'cond/Switch' (op: 'Switch') with input shapes: [?,24,24,24,?], [?,24,24,24,?].


from keras import backend as K
import numpy as np


def smooth_L1_loss(y_true, y_pred):
THRESHOLD = K.variable(1.0)
mae = K.abs(y_true-y_pred)
flag = K.greater(mae, THRESHOLD)
loss = K.mean(K.switch(flag, (mae - 0.5), K.pow(mae, 2)), axis=-1)
return loss

最佳答案

我知道我参加聚会晚了两年,但是如果您使用 tensorflow 作为 keras 后端,您可以使用 tensorflow 的 Huber loss (本质上是一样的)像这样:

import tensorflow as tf


def smooth_L1_loss(y_true, y_pred):
return tf.losses.huber_loss(y_true, y_pred)

关于deep-learning - keras:平滑 L1 损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com