pytorch cuda 欧式距离 矩阵运算

输入为tensor矩阵向量

欧式距离

def get_elu_dis(data):
    return torch.sqrt((-2*data.mm(data.t()))+torch.sum(torch.square(data),axis=1,keepdim=True)+torch.sum(torch.square(data.t()),axis=0,keepdim=True))

余玄距离

def get_cos_dis(sentences_vec):
    sentences_vec = sentences_vec / torch.norm(sentences_vec, 2, 1, keepdim=True)
    sentences_vec_mat = F.relu(1 - torch.mm(sentences_vec, sentences_vec.t()), inplace=True).cpu().numpy()
    return sentences_vec_mat

你可能感兴趣的:(pytorch cuda 欧式距离 矩阵运算)