我们可以在同一层使用多个损失函数吗? [英] Can we use multiple loss functions in same layer?

查看:132
本文介绍了我们可以在同一层使用多个损失函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在这种体系结构中使用多损失函数吗?
我有两种不同类型的损失函数,想在最后一层使用它[输出]
损失函数:

Can we use mulitple loss function in this architecture: I have two different type of loss functions and want to use it on last layer [Output] loss functions :


  • binary_crossentropy

  • 自定义损失函数

我们能做到吗?

Can we do that?

推荐答案

是的,您可以...您只需必须在模型定义中重复两次模型输出。您也可以使用loss_weights参数以不同的方式合并您的损失(两个损失的默认值为[1,1])。下面是一个虚拟回归问题的示例。 https://colab.research.google.com/drive/ 1SVHC6RuHgNNe5Qj6IOtmBD5geAJ-G9-v?usp = sharing

yes you can... you simply have to repeat 2 times the model output in the model definition. you can also merge your loss in a different way using loss_weights params (default is [1,1] for two losses). Below an example in a dummy regression problem. https://colab.research.google.com/drive/1SVHC6RuHgNNe5Qj6IOtmBD5geAJ-G9-v?usp=sharing

def rmse(y_true, y_pred):

    error = y_true-y_pred

    return K.sqrt(K.mean(K.square(error)))


X1 = np.random.uniform(0,1, (1000,10))
X2 = np.random.uniform(0,1, (1000,10))
y = np.random.uniform(0,1, 1000)

inp1 = Input((10,))
inp2 = Input((10,))
x = Concatenate()([inp1,inp2])
x = Dense(32, activation='relu')(x)
out = Dense(1)(x)

m = Model([inp1,inp2], [out,out])
m.compile(loss=[rmse,'mse'], optimizer='adam') # , loss_weights=[0.3, 0.7]
history = m.fit([X1,X2], [y,y], epochs=10, verbose=2)

这篇关于我们可以在同一层使用多个损失函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆