Skip to content

Instantly share code, notes, and snippets.

@austin362667
Last active May 11, 2023 12:51
Show Gist options
  • Save austin362667/d7bb2782cd019188c6eb8350efd83b0a to your computer and use it in GitHub Desktop.
Save austin362667/d7bb2782cd019188c6eb8350efd83b0a to your computer and use it in GitHub Desktop.
def sim(ta, tb):
a_t = enc.encode(ta)
b_t = enc.encode(tb)
a_te = np.array(model.transformer.wte.weight[a_t].tolist())
b_te = np.array(model.transformer.wte.weight[b_t].tolist())
a_pe = np.array(model.transformer.wpe.weight[[i for i in range(len(a_t))]].tolist())
b_pe = np.array(model.transformer.wpe.weight[[i for i in range(len(b_t))]].tolist())
x = np.add.reduce(a_te+a_pe)
y = np.add.reduce(b_te+b_pe)
similarity = 1 - spatial.distance.cosine(x, y)
return similarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment