多输出的自定义损失函数会引发错误

时间:2020-03-14 11:10:05

标签: python keras

x = sliced_model.output
outputMap = Convolution2D(1, (1, 1), activation='sigmoid')(x)
x = Flatten()(outputMap)
binaryOutput = Dense(1, activation='sigmoid')(x)
ourModel = Model(inputs=sliced_model.input, outputs=[outputMap, 
binaryOutput])

def customloss(y_true, y_pred):
    map_true, binary_true = y_true
    map_pred, binary_pred = y_pred
    landa = 0.5
    binaryOutput_loss = k.losses.binary_crossentropy(binary_true, binary_pred) 
    binaryMap_loss = k.mean(keras.losses.binary_crossentropy(map_true, map_pred))
    return binaryOutput_loss * landa + (1-landa) * binaryMap_loss
ourModel.compile(loss=customloss, optimizer='adam', metrics=['accuracy'])

TypeError:仅在启用急切执行后,张量对象才可迭代。要遍历此张量,请使用tf.map_fn。

0 个答案:

没有答案