Skip to content

Instantly share code, notes, and snippets.

@negedng
Created October 15, 2019 15:10
Show Gist options
  • Save negedng/0c1382d336ef9b4f1a757c9bddf4d2a0 to your computer and use it in GitHub Desktop.
Save negedng/0c1382d336ef9b4f1a757c9bddf4d2a0 to your computer and use it in GitHub Desktop.
Project word2vec vectors to 2D
# Word list
Wl = ['man', 'woman', 'rich', 'poor', 'queen',
'king', 'fisherman', 'teacher', 'actress', 'actor']
Wv = []
for i in range(len(Wl)):
# Embeddings
Wv.append(word2vec[Wl[i]])
# To-be basis
b1 = (Wv[1]-Wv[0])
b2 = (Wv[3]-Wv[2])
# Get pseudo-inverse matrix
W = np.array(Wv)
B = np.array([b1,b2])
Bi = np.linalg.pinv(B.T)
# Project all the words
Wp = np.matmul(Bi,W.T)
Wp = (Wp.T-[Wp[0,2],Wp[1,0]]).T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment