同时为不同的输出使用不同的损耗函数Keras?

Kem*_*ici 5 machine-learning computer-vision neural-network keras

我正在尝试制作一个分别输出深度图和语义分段数据的网络。

为了训练网络,我想对分类分支使用分类交叉熵,并对输出深度图的分支使用均方误差。

我在功能API的Keras文档中找不到有关为每个分支实施两个损失函数的任何信息。

我是否可以在训练期间同时使用这些损失函数,还是对我分别训练不同的分支更好?

Yu-*_*ang 9

从以下文档Model.compile

loss:字符串(目标函数的名称)或目标函数。见 损失。如果模型具有多个输出,则可以通过传递字典或损失列表来对每个输出使用不同的损失。然后,将由模型最小化的损失值将是所有单个损失的总和。

If your output is named, you can use a dictionary mapping the names to the corresponding losses:

x = Input((10,))
out1 = Dense(10, activation='softmax', name='segmentation')(x)
out2 = Dense(10, name='depth')(x)
model = Model(x, [out1, out2])
model.compile(loss={'segmentation': 'categorical_crossentropy', 'depth': 'mse'},
              optimizer='adam')
Run Code Online (Sandbox Code Playgroud)

Otherwise, use a list of losses (in the same order as the corresponding model outputs).

x = Input((10,))
out1 = Dense(10, activation='softmax')(x)
out2 = Dense(10)(x)
model = Model(x, [out1, out2])
model.compile(loss=['categorical_crossentropy', 'mse'], optimizer='adam')
Run Code Online (Sandbox Code Playgroud)