在这种架构中可以使用多个损失函数吗?我有两种不同类型的损失函数,想在最后一层[输出]使用这些损失函数:
- 二元交叉熵
- 自定义损失函数
回答:
是的,你可以这样做…你只需要在模型定义中重复两次模型输出。你也可以通过使用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)