in src/run_paraphrase.py [0:0]
def compute_similarity(f1, f2, method):
"""Compute similarity between two contextualized representations.
NOTE: Other methods besides bertscore aren't currently used but we did try
them out.
Args:
f1: Tensor of shape (L_1, d)
f2: Tensor of shape (L_2, d)
method: Name of method
Returns:
Similarity score
"""
if method == 'mean-cosine':
r1 = torch.mean(f1, dim=0) # d
r2 = torch.mean(f2, dim=0) # d
return r1.dot(r2) / torch.sqrt(r1.dot(r1) * r2.dot(r2))
elif method == 'bertscore' or method == 'bertscore-minmax':
r1 = (f1.T / torch.norm(f1, dim=1)).T # L_1, d
r2 = (f2.T / torch.norm(f2, dim=1)).T # L_2, d
dots = torch.matmul(r1, r2.T) # L_1, L_2
if method == 'bertscore':
s1 = torch.mean(torch.max(dots, dim=0)[0])
s2 = torch.mean(torch.max(dots, dim=1)[0])
elif method == 'bertscore-minmax':
s1 = torch.min(torch.max(dots, dim=0)[0])
s2 = torch.min(torch.max(dots, dim=1)[0])
return 2 * s1 * s2 / (s1 + s2)
raise NotImplementedError