简体   繁体   English

在多次损失 keras 上用单个 output 训练 model

[英]Training a model with single output on multiple losses keras

I am building an image segmentation model using keras and I want to train my model on multiple loss functions.我正在使用 keras 构建图像分割 model,我想在多个损失函数上训练我的 model。 I have seen this link but I am looking for a simpler and straight-forward solutions for this situation as my loss functions are quite complex.我看过这个链接,但我正在为这种情况寻找更简单直接的解决方案,因为我的损失函数非常复杂。 Can someone tell me how to build a model with single output with multiple losses in keras.有人能告诉我如何用单个 output 和 keras 中的多个损失构建一个 model。

You can use multiple losses with one output using weighted loss, which is a sum of your losses multiplied by weight.您可以使用加权损失对一个 output 使用多个损失,这是您的损失乘以权重的总和。 Create your custom loss which will return a sum of other losses with coefficients and pass it to model.compile .创建您的自定义损失,它将返回带有系数的其他损失的总和并将其传递给model.compile There is an example here .这里有一个例子。

This is just an example from here .这只是此处的示例。 You could play around with it.你可以玩弄它。

def custom_losses(y_true, y_pred):
    alpha = 0.6
    squared_difference = tf.square(y_true - y_pred)
    Huber = tf.keras.losses.huber(y_true, y_pred)
    return tf.reduce_mean(squared_difference, axis=-1) + (alpha*Huber)

model.compile(optimizer='adam', loss=custom_losses,metrics=['MeanSquaredError'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在训练期间变换 keras model output 并使用多个损失 - Transforming keras model output during training and use multiple losses Tensorflow 训练 - 打印一个 output 的多个损失 - Tensorflow training - print multiple losses for one output 使用多个输入训练 Keras 模型 - Training Keras model with multiple inputs 在 Keras 中输出由 add_loss 添加的多个损失 - Output multiple losses added by add_loss in Keras 训练多输出Keras模型 - training a multi-output keras model TF.Keras 中的多个 output model 单丢失 - Single loss with Multiple output model in TF.Keras 如何使用 Keras 构建具有多个输入和单个 output 的 model - How to build a model having multiple inputs and a single output using Keras 训练 Keras 模型会产生多个优化器错误 - Training a Keras model yields multiple optimizer errors Pytorch DL model,在训练期间随着收敛损失正常更新,但所有数据的 OUTPUT 值相同(回归) - Pytorch DL model, updates normally with converging losses during training, but SAME OUTPUT values for all data (regression) Training Multiple Input and Output Keras Model (ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list)) - Training Multiple Input and Output Keras Model (ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM