import torch

def mahalanobis_distance(x, y, cov): """ Compute Mahalanobis distance between two vectors x and y using the covariance matrix cov. """ diff = x - y inv_cov = torch.inverse(cov) md = torch.sqrt(torch.matmul(torch.matmul(diff, inv_cov), diff.T)) return md.item()

inputs_part = nei_inputsnei_index_t 0float hi_t = nei_inputsviewselfN selfN selfhidden_size permute1 0 2contiguousview-1 selfhidden_size # nn D写出计算两个向量之间马氏距离的代码

原文地址: https://www.cveoy.top/t/topic/cj7G 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录