Created
October 15, 2019 15:43
-
-
Save negedng/89e9ef84926b7b1cb21992798c766462 to your computer and use it in GitHub Desktop.
Sentence dependent token embedding projection
This file contains 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
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