ValueError: Shape должен иметь ранг 2, но это ранг 1 для 'loss / dens_2_loss / Npairloss / MatMul' (op: 'MatMul') с входными формами: [?], [?, 10] - PullRequest
0 голосов
/ 27 апреля 2020

Ошибка 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

Я пытаюсь реализовать потерю N-пары в обучении Keras в MNIST. Здесь якорь и положительный от одного класса, якорь и отрицательный от другого класса. Я не знаю, что не так. Большое спасибо за вашу помощь.

...