Skip to content

Instantly share code, notes, and snippets.

@negedng
Created October 15, 2019 15:43
Show Gist options
  • Save negedng/89e9ef84926b7b1cb21992798c766462 to your computer and use it in GitHub Desktop.
Save negedng/89e9ef84926b7b1cb21992798c766462 to your computer and use it in GitHub Desktop.
Sentence dependent token embedding projection
def get_visual_embs(sentence):
"""Get BERT embedding for the sentence,
project it to a 2D subspace where [CLS] is (1,0) and [SEP] is (0,1)."""
embs = bert_embedding([sentence], filter_spec_tokens=False)
tokens = embs[0][0]
embV = embs[0][1]
W = np.array(embV)
B = np.array([embV[0], embV[-1]])
Bi = np.linalg.pinv(B.T)
Wp = np.matmul(Bi,W.T)
return Wp, tokens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment