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
评论列表
文章目录