MCPcopy Create free account
hub / github.com/rushter/MLAlgorithms / TSNE

Class TSNE

mla/tsne.py:19–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17
18
19class TSNE(BaseEstimator):
20 y_required = False
21
22 def __init__(
23 self, n_components=2, perplexity=30.0, max_iter=200, learning_rate=500
24 ):
25 """A t-Distributed Stochastic Neighbor Embedding implementation.
26
27 Parameters
28 ----------
29 max_iter : int, default 200
30 perplexity : float, default 30.0
31 n_components : int, default 2
32 """
33 self.max_iter = max_iter
34 self.perplexity = perplexity
35 self.n_components = n_components
36 self.initial_momentum = 0.5
37 self.final_momentum = 0.8
38 self.min_gain = 0.01
39 self.lr = learning_rate
40 self.tol = 1e-5
41 self.perplexity_tries = 50
42
43 def fit_transform(self, X, y=None):
44 self._setup_input(X, y)
45
46 Y = np.random.randn(self.n_samples, self.n_components)
47 velocity = np.zeros_like(Y)
48 gains = np.ones_like(Y)
49
50 P = self._get_pairwise_affinities(X)
51
52 iter_num = 0
53 while iter_num < self.max_iter:
54 iter_num += 1
55
56 D = l2_distance(Y)
57 Q = self._q_distribution(D)
58
59 # Normalizer q distribution
60 Q_n = Q / np.sum(Q)
61
62 # Early exaggeration & momentum
63 pmul = 4.0 if iter_num < 100 else 1.0
64 momentum = 0.5 if iter_num < 20 else 0.8
65
66 # Perform gradient step
67 grads = np.zeros(Y.shape)
68 for i in range(self.n_samples):
69 grad = 4 * np.dot((pmul * P[i] - Q_n[i]) * Q[i], Y[i] - Y)
70 grads[i] = grad
71
72 gains = (gains + 0.2) * ((grads > 0) != (velocity > 0)) + (gains * 0.8) * (
73 (grads > 0) == (velocity > 0)
74 )
75 gains = gains.clip(min=self.min_gain)
76

Callers 1

t-sne.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected