ValueError:Shape必须为秩2,但对于输入形状为[?],[?,10]的“loss/dense_2_loss/Npairloss/MatMul”(op:“MatMul”)而言,它为秩1

2024-09-30 22:13:01 发布

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

错误 ValueError: Shape must be rank 2 but is rank 1 for 'loss/dense_2_loss/Npairloss/MatMul' (op: 'MatMul') with input shapes: [?], [?,10]

def Npairloss(y_true,y_pred):
        l2_reg = 0.02
        anchors = y_pred[:,0]    # (n, embedding_size)
        positives = y_pred[:,1]  # (n, embedding_size)
        negatives = y_pred[:2]    # (n, n-1, embedding_size)
        sub_n_p =  negatives - positives
        trans_a= K.transpose(anchors)
        logit = K.dot(trans_a,sub_n_p)  # (n, 1, n-1)
        x = K.sum(K.exp(logit), 2)  # (n, 1)
        loss = K.mean(K.log(1 + x))
        l2_loss = K.sum(anchors ** 2 + positives ** 2) / anchors.shape[0]
        losses = loss + l2_reg * l2_loss
        return losses

我试图在MNIST上实现Keras学习中的N对丢失。这里的锚和正是来自同一个类,锚和负是来自不同的类。 我不知道怎么了。非常感谢你的帮助


Tags: transsizeembeddingregsumlogitrankanchors