Skip to content

Commit 53f039f

Browse files
add visualize embeddings
1 parent 13aab2a commit 53f039f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

rnn_class/visualize_embeddings.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from sklearn.manifold import TSNE
5+
from sklearn.decomposition import PCA
6+
from sklearn.decomposition import TruncatedSVD
7+
8+
def main(we_file='word_embeddings.npy', w2i_file='wikipedia_word2idx.json', Model=PCA):
9+
We = np.load(we_file)
10+
V, D = We.shape
11+
with open(w2i_file) as f:
12+
word2idx = json.load(f)
13+
idx2word = {v:k for k,v in word2idx.iteritems()}
14+
15+
model = Model()
16+
Z = model.fit_transform(We)
17+
plt.scatter(Z[:,0], Z[:,1])
18+
for i in xrange(V):
19+
plt.annotate(s=idx2word[i], xy=(Z[i,0], Z[i,1]))
20+
plt.show()
21+
22+
23+
if __name__ == '__main__':
24+
# main(Model=TSNE)
25+
26+
# D=80, M=80
27+
main(we_file='gru_nonorm_part1_word_embeddings.npy', w2i_file='gru_nonorm_part1_wikipedia_word2idx.json', Model=TSNE)

0 commit comments

Comments
 (0)