Last active
February 13, 2020 11:17
-
-
Save lambdaofgod/fa88ec92deab4fe10fa28f664e550658 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import ot | |
from sklearn import metrics | |
roberta = torch.hub.load('pytorch/fairseq', 'roberta.large') | |
roberta.eval() # disable dropout (or leave in train mode to finetune) | |
def get_roberta_features(text): | |
return roberta.extract_features(roberta.encode(text)).detach().numpy()[0] | |
def get_optimal_transport_distance(v, w, sinkhorn_method='sinkhorn', entropy_regularization=0.05): | |
dists = metrics.pairwise.euclidean_distances(v, w) ** 2 | |
transport = ot.sinkhorn([], [], dists, reg=entropy_regularization, method=sinkhorn_method, numItermax=100) | |
cost = (dists * transport).sum() | |
return cost |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment