コード例 #1
0
 def get_label_mask(self, y_true):
     """获取batch内相同label样本"""
     label = K.cast(y_true, 'int32')
     label_2 = K.reshape(label, (1, -1))
     mask = K.equal(label_2, label)
     mask = K.cast(mask, K.floatx())
     mask = mask * (1 - K.eye(K.shape(y_true)[0]))  # 排除对角线,即 i == j
     return mask
コード例 #2
0
 def compute_loss_of_similarity(self, inputs):
     y_true = self.get_labels_of_similarity(inputs)  # 构建标签
     y_pred = K.l2_normalize(inputs, axis=1)  # 句向量归一化
     similarities = K.dot(y_pred, K.transpose(y_pred))  # 相似度矩阵
     similarities = similarities - K.eye(K.shape(y_pred)[0]) * 1e12  # 排除对角线
     similarities = similarities * self.scale  # scale
     loss = K.categorical_crossentropy(
         y_true, similarities, from_logits=True
     )
     return loss
 def compute_loss_of_similarity(self, inputs, mask=None):
     # _, _, _, y_pred, _ = inputs
     _, _, _, _, y_pred = inputs  # use last layer's logits
     y_true = self.get_labels_of_similarity(y_pred)  # 构建标签
     y_pred = K.l2_normalize(y_pred, axis=1)  # 句向量归一化
     similarities = K.dot(y_pred, K.transpose(y_pred))  # 相似度矩阵
     similarities = similarities - K.eye(K.shape(y_pred)[0]) * 1e12  # 排除对角线
     similarities = similarities * 20  # scale
     loss = K.categorical_crossentropy(
         y_true, similarities, from_logits=True
     )
     self.add_metric(loss, 'sim_loss')
     return loss
コード例 #4
0
    def compute_loss_of_scl(self, inputs, mask=None):
        y_pred, y_true = inputs
        label_mask = self.get_label_mask(y_true)
        y_pred = K.l2_normalize(y_pred, axis=1)  # 特征向量归一化
        similarities = K.dot(y_pred, K.transpose(y_pred))  # 相似矩阵
        similarities = similarities - K.eye(K.shape(y_pred)[0]) * 1e12  # 排除对角线,即 i == j

        similarities = similarities / self.T  # Temperature scale
        similarities = K.exp(similarities)  # exp

        sum_similarities = K.sum(similarities, axis=-1, keepdims=True)  # sum i != k
        scl = similarities / sum_similarities
        scl = K.log(scl + K.epsilon())  # sum log
        scl = -K.sum(scl * label_mask, axis=1, keepdims=True) / (K.sum(label_mask, axis=1, keepdims=True) + K.epsilon())
        return K.mean(scl)