使用自己的损失函数编译 Keras 模型时出错

Error during compiling a Keras model with own loss function

当我尝试使用 here 中的损失函数在 Keras 中编译模型时,出现错误

ValueError: Shape must be rank 2 but is rank 1 for 'loss/activation_10_loss/MatMul' (op: 'MatMul') with input shapes: [?], [?].

我已根据 的回答尝试修复此错误。

def get_loss_function(weights):
    def loss(y_pred, y_true):
        return (y_pred - y_true) * weights # or whatever your loss function   should be
    return loss
 model.compile(loss=get_loss_function(conv_weights), optimizer=SGD(lr=0.1))

重现问题的最简单方法:

from segmentation_models.metrics import iou_score
from segmentation_models import Unet
import keras

class Losses:

    def __init__(self):
        pass
    @staticmethod
    def IoULoss(targets, inputs, smooth=1e-6):
        logger=logging.getLogger("Losses.IoULoss")
        logger.setLevel(Debug_param.debug_scope())
        # flatten label and prediction tensors
        # logger.critical(("targets.shape",targets.get_shape().as_list(), "inputs.shape",inputs.shape))
        inputs = K.flatten(inputs)
        targets = K.flatten(targets)
        logger.critical(("flatten", "targets.shape", targets.shape, "inputs.shape", inputs.shape))

        intersection = K.sum(K.dot(targets, inputs))
        total = K.sum(targets) + K.sum(inputs)
        union = total - intersection

        IoU = (intersection + smooth) / (union + smooth)
        return 1 - IoU

model = Unet("resnet34", backend=None, classes=1, activation='softmax')
opt = keras.optimizers.Adam(lr=config.lr)
model.compile(loss=Losses.IoULoss, optimizer=opt,
                      metrics=[iou_score, "accuracy"])

如何编译带有自定义损失函数的模型或如何防止错误?

Python 版本 3.7.4,keras 2.3.0,TF 1.14,分段模型 0.2.1

当我重现您的错误时,我发现问题出在函数 K.dot() 上。看起来 Keras 期望该函数有两个 2 阶张量(即矩阵或二维数组)。您正在使用 K.flatten()inputstargets 变成一维张量(向量)。以下是如何从数据中生成二维张量的示例:

inputs = K.reshape(inputs, [1, -1]) # 1 row, as many columns as needed
targets = K.reshape(targets, [-1, 1]) # 1 column, as many rows as needed