Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (57 loc) · 1.8 KB
/
Copy pathutils.py
File metadata and controls
69 lines (57 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import numpy.random as npr
from sklearn.manifold import TSNE
import pandas as pd
import matplotlib.pyplot as plt
def alias_setup(probs):
K = len(probs)
q = np.zeros(K)
J = np.zeros(K, dtype=np.int)
# Sort the data into the outcomes with probabilities
# that are larger and smaller than 1/K.
smaller = []
larger = []
for kk, prob in enumerate(probs):
q[kk] = K*prob
if q[kk] < 1.0:
smaller.append(kk)
else:
larger.append(kk)
# Loop though and create little binary mixtures that
# appropriately allocate the larger outcomes over the
# overall uniform mixture.
while len(smaller) > 0 and len(larger) > 0:
small = smaller.pop()
large = larger.pop()
J[small] = large
q[large] = q[large] - (1.0 - q[small])
if q[large] < 1.0:
smaller.append(large)
else:
larger.append(large)
return J, q
def alias_draw(J, q):
K = len(J)
# Draw from the overall uniform mixture.
kk = int(np.floor(npr.rand()*K))
# Draw from the binary mixture, either keeping the
# small one, or choosing the associated larger one.
if npr.rand() < q[kk]:
return kk
else:
return J[kk]
def visualize(vocab,embeddings):
"""
plot a 2d t-sne of the generated embeddings
Arguments:
embeddings {array} -- vector with n-dimension features for each point
"""
#tsne = TSNE(n_components = 2,random_state = 0)
#X_tsne = tsne.fit_transform(embeddings)
df = pd.DataFrame(embeddings,index=vocab,columns = ['x','y'])
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(df['x'], df['y'])
for word, pos in df.iterrows():
ax.annotate(word, pos)
plt.show()