numpy 使用距离矩阵自定义损失函数

qyswt5oh  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(187)

对于我的模型的损失函数,我想比较预测结果与真实值之间的距离,以及预测值与参考数据集中其他最近邻居之间的距离。我可以用np数组很好地计算这个值,但是TF不喜欢我在Tensor上使用这个。

def nearest_other_neighbour(y_true, y_pred):
    losses=[]
    
    res_df = pd.DataFrame(cdist(keras.backend.array(y_pred), keras.backend.array(y_true)))
    for i in range(len(res_df.columns)) :
        mycol = list(res_df[i])
        dist_ref_self = mycol.pop(i)
        dist_ref_min_other = min(mycol)
        losses.append(dist_ref_self/dist_ref_min_other)
    
    return losses

我找到了this post,它允许我只使用Tensor计算距离矩阵,但之后我仍然需要迭代距离矩阵。
我如何仅使用Tensor来执行这些计算?或者有没有一种方法可以将符号Tensor转换为损失函数中的np数组?

2ekbmq32

2ekbmq321#

如果以后有人找这个:我和特遣部队一起完成了这一切

# def create_custom_loss(constant=1.0):
def create_nearest_other_neighbour(mybatchsize):
# def custom_loss(y_true, y_pred):
    def nearest_other_neighbour(B, A):
        na = tf.reduce_sum(tf.square(A), 1)
        nb = tf.reduce_sum(tf.square(B), 1)

        # na as a row and nb as a column vectors
        na = tf.reshape(na, [-1, 1])
        nb = tf.reshape(nb, [1, -1])

        # return pairwise euclidean difference matrix
        dists = tf.sqrt(na - 2*tf.matmul(A, B, False, True) + nb)

        # extract diag of dist matrix (distance from predicted self to real self), and replace with dummy vals that won't interfere with minimums
        d = tf.linalg.tensor_diag_part(dists)

        newdiag = tf.repeat(tf.reduce_max(dists), mybatchsize)
        dists_nodiag = tf.linalg.set_diag(dists, newdiag)

        # get minimum of each column of new matrix, then divide diagonal by these mins
        # (done in return for fewer variables)
        # min_percol = tf.math.reduce_min(dists_nodiag, 1)
        # dist_ratio = tf.math.divide(d, min_percol)
        # return dist_ratio

        return tf.math.divide(d, tf.math.reduce_min(dists_nodiag, 1))
    
    return nearest_other_neighbour

这需要使用datasets来管理输入:train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).batch(mybatch, drop_remainder=True)

相关问题