ops.py 文件源码

python
阅读 59 收藏 0 点赞 0 评论 0

项目:repeval_rivercorners 作者: jabalazs 项目源码 文件源码
def columnwise_cosine_similarity(matrix1, matrix2):
    """Return the columnwise cosine similarity from matrix1 and matrix2.
    Expect tesor of dimension (batch_size, seq_len, hidden).
    Return tensor of size (batch_size, seq_len) containing the cosine
    similarities."""

    assert matrix1.size() == matrix2.size(), 'matrix sizes do not match'

    # -> (batch_size, seq_len, 1)
    n_m1 = torch.norm(matrix1, 2, 2)
    n_m2 = torch.norm(matrix2, 2, 2)

    # -> (batch_size, seq_len, 1)
    col_norm = torch.mul(n_m1, n_m2)

    # -> (batch_size, seq_len, hidden)
    colprod = torch.mul(matrix1, matrix2)
    # -> (batch_size, seq_len, 1)
    colsum = torch.sum(colprod, 2)

    # -> (batch_size, seq_len, 1)
    cosine_sim = torch.div(colsum, col_norm)

    # -> (batch_size, seq_len)
    cosine_sim = cosine_sim.squeeze()

    return cosine_sim
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号