[英]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.