简体   繁体   English

在Keras中为模型编译指定多重损失函数

[英]Specify multiple loss function for model compilation in Keras

I want to specify 2 loss functions 1 for the object class which is cross-entropy and the other for the bounding box which is mean squared error. 我想为交叉熵的对象类指定2个损失函数,为边界均方误差的边界框指定2个损失函数。 how to specify in model.compile each output with the corresponding loss function? 如何指定在model.compile每个输出与相应的损失函数?

model = Sequential()

model.add(Dense(128, activation='relu'))
out_last_dense = model.add(Dense(128, activation='relu'))
object_type = model.add(Dense(1, activation='softmax'))(out_last_dense)
object_coordinates = model.add(Dense(4, activation='softmax'))(out_last_dense)

/// here is the problem i want to specify loss function for object type and coordinates
model.compile(loss= keras.losses.categorical_crossentropy,
   optimizer= 'sgd', metrics=['accuracy'])

First of all, you can't use Sequential API here since your model has two output layers (ie what you have written is all wrong and would raise error). 首先,由于模型有两个输出层,因此不能在这里使用Sequential API(即,您编写的内容都是错误的,并且会引发错误)。 Instead you must use Keras Functional API : 相反,您必须使用Keras Functional API

inp = Input(shape=...)
x = Dense(128, activation='relu')(inp)
x = Dense(128, activation='relu')(x)
object_type = Dense(1, activation='sigmoid', name='type')(x)
object_coordinates = Dense(4, activation='linear', name='coord')(x)

Now, you can specify a loss function (as well as metric) for each output layer based on their names given above and using a dictionary: 现在,您可以根据上面给出的名称并使用字典为每个输出层指定损失函数(以及度量):

model.compile(loss={'type': 'binary_crossentropy', 'coord': 'mse'}, 
              optimizer='sgd', metrics={'type': 'accuracy', 'coord': 'mae'})

Further, note that you are using softmax as the activation function and I have changed it to sigomid and linear above. 此外,请注意,您正在使用softmax作为激活函数,并且在上面将其更改为sigomidlinear That's because: 1) using softmax on a layer with one unit does not make sense (if there are more than 2 classes then you should use softmax), and 2) the other layer predicts coordinates and therefore using softmax is not suitable at all (unless the problem formulation let you do so). 这是因为:1)在具有一个单位的层上使用softmax没有意义(如果有两个以上的类,则应使用softmax),并且2)另一层预测坐标,因此使用softmax根本不适合(除非问题表述允许您这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM