forked from cemoody/topicsne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
73 lines (62 loc) · 2.1 KB
/
run.py
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
70
71
from sklearn import manifold, datasets
from sklearn.metrics.pairwise import pairwise_distances
from scipy.spatial.distance import squareform
from matplotlib.patches import Ellipse
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from wrapper import Wrapper
# from tsne import TSNE
from vtsne import VTSNE
def preprocess(perplexity=30, metric='euclidean'):
""" Compute pairiwse probabilities for MNIST pixels.
"""
digits = datasets.load_digits(n_class=6)
pos = digits.data
y = digits.target
n_points = pos.shape[0]
distances2 = pairwise_distances(pos, metric=metric, squared=True)
# This return a n x (n-1) prob array
pij = manifold.t_sne._joint_probabilities(distances2, perplexity, False)
# Convert to n x n prob array
pij = squareform(pij)
return n_points, pij, y
draw_ellipse = True
n_points, pij2d, y = preprocess()
i, j = np.indices(pij2d.shape)
i = i.ravel()
j = j.ravel()
pij = pij2d.ravel().astype('float32')
# Remove self-indices
idx = i != j
i, j, pij = i[idx], j[idx], pij[idx]
n_topics = 2
n_dim = 2
print(n_points, n_dim, n_topics)
model = VTSNE(n_points, n_topics, n_dim)
wrap = Wrapper(model, batchsize=4096, epochs=1)
for itr in range(500):
wrap.fit(pij, i, j)
# Visualize the results
embed = model.logits.weight.cpu().data.numpy()
f = plt.figure()
if not draw_ellipse:
plt.scatter(embed[:, 0], embed[:, 1], c=y * 1.0 / y.max())
plt.axis('off')
plt.savefig('scatter_{:03d}.png'.format(itr), bbox_inches='tight')
plt.close(f)
else:
# Visualize with ellipses
var = np.sqrt(model.logits_lv.weight.clone().exp_().cpu().data.numpy())
ax = plt.gca()
for xy, (w, h), c in zip(embed, var, y):
e = Ellipse(xy=xy, width=w, height=h, ec=None, lw=0.0)
e.set_facecolor(plt.cm.Paired(c * 1.0 / y.max()))
e.set_alpha(0.5)
ax.add_artist(e)
ax.set_xlim(-9, 9)
ax.set_ylim(-9, 9)
plt.axis('off')
plt.savefig('scatter_{:03d}.png'.format(itr), bbox_inches='tight')
plt.close(f)