0

当我试图在 Keras 中编译一个带有损失函数的模型时我遇到了一个错误

ValueError:形状必须为 2 级,但对于 'loss/activation_10_loss/MatMul'(操作:'MatMul')为 1 级,输入形状为:[?]、[?]。

我已尝试按照答案修复此错误。

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

4

1 回答 1

1

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

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

于 2019-10-03T17:05:10.510 回答