多输出的自定义丢失函数抛出错误

2024-05-29 06:05:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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:Tensor对象仅在启用“急切执行”时才可用。要在这个张量上迭代,请使用tf.map\u fn


Tags: truemapmodelactivationbinarylandalossesloss

热门问题