我们可以在同一层使用多个损失函数吗?

300*_*300 5 machine-learning deep-learning keras tensorflow loss-function

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

  • 二元交叉熵
  • 自定义损失函数

我们可以这样做吗? 在此输入图像描述

Mar*_*ani 3

是的,你可以......你只需在模型定义中重复模型输出两次。您还可以使用 loss_weights 参数以不同的方式合并损失(两个损失的默认值为 [1,1])。下面是虚拟回归问题的示例。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)
Run Code Online (Sandbox Code Playgroud)