-
Notifications
You must be signed in to change notification settings - Fork 158
/
05_tutorial_tSNE.py
409 lines (303 loc) · 12.5 KB
/
05_tutorial_tSNE.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# coding: utf-8
# # Dimensionality Reduction via t-SNE
#
# Example code for the lecture series "Machine Learning for Physicists" by Florian Marquardt
#
# Lecture 5, Tutorial (this is discussed in session 5)
#
# See https://machine-learning-for-physicists.org and the current course website linked there!
#
#
# The following python code for t-SNE is an original code by the inventor of t-SNE, Laurens van der Maaten. It is available on his website https://lvdmaaten.github.io/tsne/ .
#
# It is stated on that website: "*You are free to use, modify, or redistribute this software in any way you want, but only for non-commercial purposes. The use of the software is at your own risk; the authors are not responsible for any damage as a result from errors in the software.*"
# In[17]:
import numpy as np
import matplotlib.pyplot as plt # for plotting
import matplotlib
matplotlib.rcParams['figure.dpi']=300 # highres display
# for updating display
# (very simple animation)
from IPython.display import clear_output
# In[38]:
#
# tsne.py
#
# Implementation of t-SNE in Python. The implementation was tested on Python
# 2.7.10, and it requires a working installation of NumPy. The implementation
# comes with an example on the MNIST dataset. In order to plot the
# results of this example, a working installation of matplotlib is required.
#
# The example can be run by executing: `ipython tsne.py`
#
#
# Created by Laurens van der Maaten on 20-12-08.
# Copyright (c) 2008 Tilburg University. All rights reserved.
# note by FM: For this notebook, the MNIST example was dropped
import numpy as np
def Hbeta(D=np.array([]), beta=1.0):
"""
Compute the perplexity and the P-row for a specific value of the
precision of a Gaussian distribution.
"""
# Compute P-row and corresponding perplexity
P = np.exp(-D.copy() * beta)
sumP = sum(P)
H = np.log(sumP) + beta * np.sum(D * P) / sumP
P = P / sumP
return H, P
def x2p(X=np.array([]), tol=1e-5, perplexity=30.0):
"""
Performs a binary search to get P-values in such a way that each
conditional Gaussian has the same perplexity.
"""
# Initialize some variables
print("Computing pairwise distances...")
(n, d) = X.shape
sum_X = np.sum(np.square(X), 1)
D = np.add(np.add(-2 * np.dot(X, X.T), sum_X).T, sum_X)
P = np.zeros((n, n))
beta = np.ones((n, 1))
logU = np.log(perplexity)
# Loop over all datapoints
for i in range(n):
# Print progress
if i % 500 == 0:
print("Computing P-values for point %d of %d..." % (i, n))
# Compute the Gaussian kernel and entropy for the current precision
betamin = -np.inf
betamax = np.inf
Di = D[i, np.concatenate((np.r_[0:i], np.r_[i+1:n]))]
(H, thisP) = Hbeta(Di, beta[i])
# Evaluate whether the perplexity is within tolerance
Hdiff = H - logU
tries = 0
while np.abs(Hdiff) > tol and tries < 50:
# If not, increase or decrease precision
if Hdiff > 0:
betamin = beta[i].copy()
if betamax == np.inf or betamax == -np.inf:
beta[i] = beta[i] * 2.
else:
beta[i] = (beta[i] + betamax) / 2.
else:
betamax = beta[i].copy()
if betamin == np.inf or betamin == -np.inf:
beta[i] = beta[i] / 2.
else:
beta[i] = (beta[i] + betamin) / 2.
# Recompute the values
(H, thisP) = Hbeta(Di, beta[i])
Hdiff = H - logU
tries += 1
# Set the final row of P
P[i, np.concatenate((np.r_[0:i], np.r_[i+1:n]))] = thisP
# Return final P-matrix
print("Mean value of sigma: %f" % np.mean(np.sqrt(1 / beta)))
return P
def pca(X=np.array([]), no_dims=50):
"""
Runs PCA on the NxD array X in order to reduce its dimensionality to
no_dims dimensions.
"""
print("Preprocessing the data using PCA...")
(n, d) = X.shape
X = X - np.tile(np.mean(X, 0), (n, 1))
(l, M) = np.linalg.eig(np.dot(X.T, X))
Y = np.dot(X, M[:, 0:no_dims])
return Y
def tsne(X=np.array([]), no_dims=2, initial_dims=50, perplexity=30.0,
do_animation=False, animation_skip_steps=10, max_iter = 1000):
"""
Runs t-SNE on the dataset in the NxD array X to reduce its
dimensionality to no_dims dimensions. The syntaxis of the function is
`Y = tsne.tsne(X, no_dims, perplexity), where X is an NxD NumPy array.
Added by F. Marquardt: do_animation==True will give you a graphical animation of
the progress, use animation_skip_steps to control how often this will
be plotted; max_iter controls the total number of gradient descent steps
"""
# Check inputs
if isinstance(no_dims, float):
print("Error: array X should have type float.")
return -1
if round(no_dims) != no_dims:
print("Error: number of dimensions should be an integer.")
return -1
# Initialize variables
X = pca(X, initial_dims).real
(n, d) = X.shape
initial_momentum = 0.5
final_momentum = 0.8
eta = 500
min_gain = 0.01
Y = np.random.randn(n, no_dims)
dY = np.zeros((n, no_dims))
iY = np.zeros((n, no_dims))
gains = np.ones((n, no_dims))
# Compute P-values
P = x2p(X, 1e-5, perplexity)
P = P + np.transpose(P)
P = P / np.sum(P)
P = P * 4. # early exaggeration
P = np.maximum(P, 1e-12)
if do_animation: # added by FM
costs=np.zeros(max_iter) # to store the cost values
# Run iterations
for iter in range(max_iter):
# Compute pairwise affinities
sum_Y = np.sum(np.square(Y), 1)
num = -2. * np.dot(Y, Y.T)
num = 1. / (1. + np.add(np.add(num, sum_Y).T, sum_Y))
num[range(n), range(n)] = 0.
Q = num / np.sum(num)
Q = np.maximum(Q, 1e-12)
# Compute gradient
PQ = P - Q
for i in range(n):
dY[i, :] = np.sum(np.tile(PQ[:, i] * num[:, i], (no_dims, 1)).T * (Y[i, :] - Y), 0)
# Perform the update
if iter < 20:
momentum = initial_momentum
else:
momentum = final_momentum
gains = (gains + 0.2) * ((dY > 0.) != (iY > 0.)) + (gains * 0.8) * ((dY > 0.) == (iY > 0.))
gains[gains < min_gain] = min_gain
iY = momentum * iY - eta * (gains * dY)
Y = Y + iY
Y = Y - np.tile(np.mean(Y, 0), (n, 1))
if not do_animation: # added by FM: do not print if we are animating!
# Compute current value of cost function
if (iter + 1) % 10 == 0:
C = np.sum(P * np.log(P / Q))
print("Iteration %d: error is %f" % (iter + 1, C), end=" \r") # modified to overwrite line
# Stop lying about P-values
if iter == 100:
P = P / 4.
if do_animation: # added by FM
C = np.sum(P * np.log(P / Q)) # compute for every step, to store it in 'costs'
costs[iter]=C
if iter % animation_skip_steps==0:
clear_output(wait=True)
fig,ax=plt.subplots(ncols=2,nrows=1,figsize=(10,5))
ax[0].plot(costs)
ax[1].scatter(Y[:,0],Y[:,1],color="orange")
plt.show()
# Return solution
return Y
# if __name__ == "__main__":
# print("Run Y = tsne.tsne(X, no_dims, perplexity) to perform t-SNE on your dataset.")
# print("Running example on 2,500 MNIST digits...")
# X = np.loadtxt("mnist2500_X.txt")
# labels = np.loadtxt("mnist2500_labels.txt")
# Y = tsne(X, 2, 50, 20.0)
# pylab.scatter(Y[:, 0], Y[:, 1], 20, labels)
# pylab.show()
# # Example 1: A few Gaussian clouds in high-dimensional space
# In[191]:
# produce a high-dimensional data set, composed of a few Gaussian point clouds in high-dimensional space
n_dim=100 # a really high-dimensional space
n_clusters=5 # number of clusters, i.e. clouds
N_cluster_points=30 # number of points inside each cluster
N=n_clusters*N_cluster_points # total number of points
Gauss_spread=1.0 # size of each cluster (cloud)
X=np.zeros([N,n_dim])
for j in range(n_clusters):
Xmean=np.random.randn(n_dim) # the center position of the cluster
X[j*N_cluster_points:(j+1)*N_cluster_points,:]=Xmean[None,:]+Gauss_spread*np.random.randn(N_cluster_points,n_dim)
# plot these points in a projection into 2D space
# color them according to the cluster they belong to!
plt.figure(figsize=(8,8))
for j in range(n_clusters):
X0=X[j*N_cluster_points:(j+1)*N_cluster_points,0]
X1=X[j*N_cluster_points:(j+1)*N_cluster_points,1]
plt.scatter(X0,X1)
plt.show()
# In[31]:
# now apply t-SNE to reduce to two dimensions in a smart way!
Y=tsne(X, no_dims=2, initial_dims=50, perplexity=20.0,
do_animation=True, animation_skip_steps=10, max_iter=300)
# plot the points according to the t-SNE projection into 2D space
# color them according to the cluster they belong to!
plt.figure(figsize=(8,8))
for j in range(n_clusters):
Y0=Y[j*N_cluster_points:(j+1)*N_cluster_points,0]
Y1=Y[j*N_cluster_points:(j+1)*N_cluster_points,1]
plt.scatter(Y0,Y1)
plt.show()
# # Example 2: Multiple Gaussians: finding the true number without labels!
# In[183]:
# Plot a tSNE-scatterplot, but with some randomly marked points,
# together with the corresponding high-dimensional data points plotted
# as curves!
def plot_tsne_with_curves(y0,y1,x,xlabel,n_picks=10,vmin=-0.1,vmax=2):
random_picks=np.random.randint(np.shape(y0)[0],size=n_picks) # pick some random points
fig=plt.figure(constrained_layout=True,figsize=(8,4))
gs=fig.add_gridspec(ncols=8,nrows=4)
scatter_plot=fig.add_subplot(gs[0:4,0:4])
myplot={}
j=0
for n0 in range(4):
for n1 in range(4):
myplot[j]=fig.add_subplot(gs[n0,4+n1])
myplot[j].axis('off')
j+=1
scatter_plot.scatter(y0,y1,c=xlabel)
scatter_plot.scatter(y0[random_picks],y1[random_picks],color="black",alpha=0.7,s=80)
for idx in range(len(random_picks)):
scatter_plot.text(y0[random_picks[idx]], y1[random_picks[idx]],
str(idx), fontsize=8, color="orange",
alpha=0.8, horizontalalignment='center',
verticalalignment='center')
for idx,m in enumerate(random_picks):
if idx<j:
myplot[idx].plot(x[m,:])
myplot[idx].text(0.1, 0.75, str(idx), fontsize=12, color="orange",
alpha=0.5, horizontalalignment='center',
verticalalignment='center', transform=myplot[idx].transAxes)
myplot[idx].set_ylim([vmin,vmax])
plt.show()
# In[185]:
def my_generator1D(batchsize,x): # produce a batch of curves, a random number of Gaussian
maxNum=2 # the maximum number of Gaussians
NumGaussians=np.random.randint(maxNum+1,size=batchsize) # select the number, for each sample
Curves=np.zeros([batchsize,len(x)])
for j in range(maxNum):
R=np.random.uniform(low=0.1,high=0.11,size=batchsize) # width
A=np.random.uniform(low=0.9,high=1.0,size=batchsize) # amplitude
x0=np.random.uniform(size=batchsize,low=-0.8,high=0.8) # position
Curves+=(j<=NumGaussians[:,None]-1)*A[:,None]*np.exp(-((x[None,:]-x0[:,None])/R[:,None])**2)
Curves+=0.1*np.random.randn(batchsize,len(x)) # add a bit of background noise on top
return( Curves, NumGaussians )
n_dim=100
x=np.linspace(-1,1,n_dim)
N=2000 # how many curves
X,Xlabel=my_generator1D(N,x)
# small x is the coordinate, capital X are the high-dim. "data points", meaning samples of curves!
# In[186]:
fig,ax=plt.subplots(ncols=10,nrows=1,figsize=(10,1))
for n in range(10):
ax[n].plot(X[n,:])
ax[n].set_ylim([-0.1,2])
ax[n].axis('off')
plt.show()
# In[187]:
# plot these points in a projection into 2D space
# pick two arbitrary coordinates
j0=17
j1=35
plt.figure(figsize=(8,8))
X0=X[:,j0]
X1=X[:,j1]
plt.scatter(X0,X1,c=Xlabel)
plt.show()
# In[188]:
# now apply t-SNE to reduce to two dimensions in a smart way!
Y=tsne(X, 2, 20, 30.0, do_animation=True, animation_skip_steps=10, max_iter=300)
# In[189]:
plot_tsne_with_curves(Y[:,0],Y[:,1],X,Xlabel,n_picks=16)
# # Tutorial Exercise 1: Change the perplexity parameter in the 'Gaussian cloud' example and/or the 'multiple Gaussians' example and observe its effect!
#
#
# # Tutorial Exercise 2: Modify the 'multiple Gaussians' example by inventing different sample curves
#
#