-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSNEImplementation.py
367 lines (316 loc) · 14.2 KB
/
TSNEImplementation.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
from PyQt5.QtWidgets import *
# from UtilityClasses import Cluster
# import Constants
import numpy as np
import pylab
# import matplotlib.pyplot as plt
import pyglet
# import time
import os
# import copy
class TSNEWindow(QMainWindow):
def __init__(self, parent):
super().__init__(parent)
# Глобальные переменные для пошагового исполнения алгоритма.
# Для того чтобы выполнить шаг нужно помнить состояние достигнутое прыдыдущими шагами.
# Это состояние хранится в следующих переменных
self.max_iteration = 1000
self.perplexity = 30.0
self.tsne_error = 0
self.frames_amount = 200
self.frames_path_prefix = 'tempimages/foo'
self.frames_list_paths=[]
self.frames_list=[]
self.workData = self.parent().globalData
self.rowsToClusterize = list(self.workData)
self.rowsToConsider = list(self.workData)
self.resultClusters = []
self.supposedClusterElems = set()
self.stepIterator = 0
self.clusterIterator = 0
self.neighborsCurrent = set()
self.neighborsToConsider = set()
self.neighborsConsidered = set()
self.cluster = None
self.prevClusterSize = 0
self.currClusterSize = 0
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.layout = QVBoxLayout(self.centralWidget)
self.perplexityLabel = QLabel("Perplexity")
self.perplexityEdit = QLineEdit()
self.max_iterationLabel = QLabel("Число итераций")
self.max_iterationEdit = QLineEdit()
self.pics_iterationLabel = QLabel("Количество кадров")
self.pics_iterationEdit = QLineEdit()
self.layout.addWidget(self.perplexityLabel)
self.layout.addWidget(self.perplexityEdit)
self.layout.addWidget(self.max_iterationLabel)
self.layout.addWidget(self.max_iterationEdit)
self.confirmationButton = QPushButton("Выполнить алгоритм")
self.confirmationButton.clicked.connect(self.performAlgorithm)
self.layout.addWidget(self.confirmationButton)
self.setGeometry(100, 100, 200, 200)
self.setWindowTitle("t-SNE")
self.show()
# algorithm
def performAlgorithm(self):
if self.prepareData():
a = []
# a = np.array(a)
for i in range(len(self.rowsToClusterize)):
for j in range(len(self.rowsToClusterize.__getitem__(i))):
a.append(self.rowsToClusterize.__getitem__(i).__getitem__(j))
data_numpy_array = np.array(a)
data_numpy_array = np.reshape(data_numpy_array, (-1, len(self.rowsToClusterize.__getitem__(0))))
# print(data_numpy_array)
result = self.tsne(data_numpy_array, 2,
len(self.rowsToClusterize.__getitem__(0)),
self.perplexity,
self.max_iteration, 1)
# print("x =", data_numpy_array)
# print("y =", result)
# print("ошибка =", self.tsne_error)
# ОТРИСОВКА АНИМАЦИИ
animation = pyglet.image.Animation.from_image_sequence(self.frames_list, 0.25, loop=False)
# Создаем спрайт объект
anim_sprite = pyglet.sprite.Sprite(animation)
# Главное окно Pyglet
w = anim_sprite.width
h = anim_sprite.height
win = pyglet.window.Window(width=w, height=h)
# Устанавливаем белый цвет фона
pyglet.gl.glClearColor(1, 1, 1, 1)
@ win.event
def on_draw():
win.clear()
anim_sprite.draw()
pyglet.app.run()
# self.remove_all_frames()
def perform_without_drawing(self, dim):
if self.prepareData():
a = []
# a = np.array(a)
for i in range(len(self.rowsToClusterize)):
for j in range(len(self.rowsToClusterize.__getitem__(i))):
a.append(self.rowsToClusterize.__getitem__(i).__getitem__(j))
data_numpy_array = np.array(a)
data_numpy_array = np.reshape(data_numpy_array, (-1, len(self.rowsToClusterize.__getitem__(0))))
# print(data_numpy_array)
result = self.tsne(data_numpy_array, dim, len(self.rowsToClusterize.__getitem__(0)), self.perplexity, self.max_iteration, 1)
# print("x=", data_numpy_array)
# print("y=", result)
# print("ошибка= ", self.tsne_error)
return result
def reduce_data_dim(self, dim):
if self.prepareData():
a = []
# a = np.array(a)
for i in range(len(self.rowsToClusterize)):
for j in range(len(self.rowsToClusterize.__getitem__(i))):
a.append(self.rowsToClusterize.__getitem__(i).__getitem__(j))
data_numpy_array = np.array(a)
data_numpy_array = np.reshape(data_numpy_array, (-1, len(self.rowsToClusterize.__getitem__(0))))
result = self.tsne(data_numpy_array, dim, len(self.rowsToClusterize.__getitem__(0)), self.perplexity, self.max_iteration, 1)
# print("result class - ", result.__class__.__name__)
return result
def prepareData(self):
""" Подготавилвает данные, нужные для работы алгоритма
:return: возвращает True если подготовка выполнена успешно и False в противном случае
"""
try:
self.perplexity = float(self.perplexityEdit.text())
except ValueError:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("perplexity задан некорректно")
msg.setWindowTitle("Внимание")
msg.exec_()
return False
try:
self.max_iteration = int(self.max_iterationEdit.text())
except ValueError:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Число итераций задано некорректно")
msg.setWindowTitle("Внимание")
msg.exec_()
return False
self.workData = self.parent().globalData
self.rowsToClusterize = list(self.workData)
self.resultClusters = []
self.supposedClusterElems = set()
return True
def remove_all_frames(self):
for i in range(self.frames_amount+1):
if os.path.exists(self.frames_path_prefix+str(i)+'.png'):
os.remove(self.frames_path_prefix + str(i) + '.png')
else:
print("попытка удалить несуществующий файл: " + self.frames_path_prefix+str(i)+'.png')
# реализация алгоритма t-sne
@staticmethod
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)
#### experiments
if(sumP==0):
# print("SumP from =")
# print(P)
sumP=0.000000001
####
H = np.log(sumP) + beta * np.sum(D * P) / sumP
P = P / sumP
return H, P
@staticmethod
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))
pass
# 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) = TSNEWindow.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) = TSNEWindow.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
@staticmethod
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(self, X=np.array([]), no_dims=2, initial_dims=50, perplexity=30.0, max_iteration=1000, draw_image=0):
"""
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.
"""
# индекс для сохранения изображений
current_index = 0
shift = self.max_iteration // self.frames_amount
# 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 = TSNEWindow.pca(X, initial_dims).real
(n, d) = X.shape
max_iter = max_iteration
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 = TSNEWindow.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)
# Подкотовка изображений
# 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))
# 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)) # TODO: изменить на .format
self.tsne_error = C # error saving
# Stop lying about P-values
if iter == 100:
P = P / 4.
# сохраняем рисунки для анимации
if draw_image != 0:
if iter % shift == 0:
pylab.scatter(Y[:, 0], Y[:, 1], c='r')
#
pylab.savefig(self.frames_path_prefix+str(current_index)+'.png')
pylab.gcf().clear() # очистка плота
# добавляем текушее изображение в последовательность кадров
current_img = pyglet.image.load(self.frames_path_prefix+str(current_index)+'.png')
self.frames_list.append(current_img)
# удаляем изображение сразу
if os.path.exists(self.frames_path_prefix+str(current_index)+'.png'):
os.remove(self.frames_path_prefix+str(current_index)+'.png')
#self.frames_list_paths.append(self.frames_path_prefix+str(current_index)+'.png')
#print("сохранено изображение номер"+str(current_index))
current_index += 1
# Return solution
return Y