def test_same_fft_optimized_median_initializations_for_transform(self): """Transform with interpolation based optimization should be deterministic.""" x_train, x_test = train_test_split(self.iris, test_size=0.33, random_state=42) embedding = openTSNE.TSNE( early_exaggeration_iter=10, n_iter=10, neighbors="exact", negative_gradient_method="fft", random_state=42, ).fit(x_train) for init in self.transform_initializations: new_embedding_1 = embedding.transform(x_test, initialization=init, n_iter=10, learning_rate=10) new_embedding_2 = embedding.transform(x_test, initialization=init, n_iter=10, learning_rate=10) np.testing.assert_equal(new_embedding_1, new_embedding_2, init)
def test_iris_bh_transform_equivalency_with_one_by_one(self): """Compare one by one embedding vs all at once using BH gradients.""" x_train, x_test = train_test_split( self.iris.data, test_size=0.33, random_state=42 ) # Set up the initial embedding embedding = openTSNE.TSNE( early_exaggeration_iter=0, n_iter=50, neighbors="exact", negative_gradient_method="bh", ).fit(x_train) params = dict(n_iter=100, perplexity=5) # Build up an embedding by adding points one by one new_embedding_1 = np.vstack( [embedding.transform(np.atleast_2d(point), **params) for point in x_test] ) # Add new points altogether new_embedding_2 = embedding.transform(x_test, **params) # Verify that the embedding has actually been optimized self.assertRaises( AssertionError, np.testing.assert_almost_equal, embedding.prepare_partial(x_test, perplexity=params["perplexity"]), new_embedding_1, ) # Check that both methods produced the same embedding np.testing.assert_almost_equal(new_embedding_1, new_embedding_2)
def plot_embeddings(embeddings, Y): # Sample only a subset of all data points (include the positive labels, though). sample_indices = np.zeros(shape=Y.shape[0], dtype=np.bool) sample_indices[np.random.choice(np.arange(Y.shape[0]), size=10000)] = 1 sample_indices[Y[:, 0] > 0] = 1 proj = openTSNE.TSNE().fit(embeddings[sample_indices]) x, y = proj[:, 0], proj[:, 1] fig, ax = plt.subplots(figsize=(10, 10)) has_label = Y[sample_indices, 0] != -1 sns.scatterplot(x[~has_label], y[~has_label], hue=0, palette="Greys", markers="x", alpha=0.1) sns.scatterplot( x[has_label], y[has_label], hue=Y[sample_indices, 0][has_label], palette="tab10", alpha=0.5, ) ax.legend( ax.get_legend_handles_labels()[0], ["unlabelled", "other", "following", "dancing"], loc="best", ) plt.show() return proj[:, :2]
def open_tSNE(X, grad_X, Y, cate_0, fn): #model.embedding_ = model.embedding_.astype(np.float32, order='A') #Embedding original data. embedding = openTSNE.TSNE().fit(X) vis_x = embedding[:, 0] vis_y = embedding[:, 1] #print(vis_x.shape) #print(vis_y.shape) """ Scale data to 0-1 range on both axis """ norm_x, norm_y = scale_data(vis_x, vis_y) print(norm_x.shape) print(norm_y.shape) #Plot tsne embedding. n = len(cate_0) #plt.scatter(vis_x, vis_y, c=Y, cmap=plt.cm.get_cmap("hot"), s = 3) #normalize = matplotlib.colors.Normalize(vmin=0, vmax=1) plt.scatter(norm_x, norm_y, c=Y ,cmap=plt.cm.get_cmap("hot"), s = 3) plt.colorbar() ''' #Embed new points(grad vectors) into the existing embedding. grad_embedding = embedding.transform(grad_X) vis_gx = grad_embedding[:, 0] vis_gy = grad_embedding[:, 1] #Plot new points from tsne. plt.quiver(vis_x, vis_y, vis_gx, vis_gy, angles='xy') #, scale_units='xy', scale=1) ''' mp.savefig(fn) plt.close()
def get_manifold(self, output_mid, fit=False): if (self.mode == 'pixel'): # output_mid = output_mid.reshape(output_mid.shape[0], -1).swapaxes(0, 1) if self.option == 'multiscale': affinities_multiscale_mixture = openTSNE.affinity.Multiscale( output_mid, perplexities=[50, 1000], metric="cosine", n_jobs=8, random_state=3, ) init = openTSNE.initialization.pca(output_mid, random_state=42) embedding_multiscale = openTSNE.TSNE(n_jobs=8).fit( affinities=affinities_multiscale_mixture, initialization=init, ) output_2d = scaler.fit_transform(embedding_multiscale) elif self.option == 'std': output_2d = self.function.fit(output_mid) elif (self.mode == 'feature'): # output_mid = output_mid.reshape(output_mid.shape[0],-1) # output_2d = self.function.fit_transform(output_mid).astype( # # np.float) if self.option == 'multiscale': affinities_multiscale_mixture = openTSNE.affinity.Multiscale( output_mid, perplexities=[2, 20], metric="cosine", n_jobs=8, random_state=3, ) init = openTSNE.initialization.pca(output_mid, random_state=42) embedding_multiscale = openTSNE.TSNE(n_jobs=8).fit( affinities=affinities_multiscale_mixture, initialization=init, ) output_2d = scaler.fit_transform(embedding_multiscale) # ? elif self.option == 'std': output_2d = self.function.fit(output_mid) return output_2d
def test_interpolation_grid_not_called_using_bh(self): x1 = np.random.normal(0, 1, (50, 10)) x2 = np.random.normal(0, 1, (20, 10)) with patch("openTSNE.TSNEEmbedding.prepare_interpolation_grid" ) as prep_grid: tsne = openTSNE.TSNE(negative_gradient_method="bh") embedding = tsne.fit(x1) # Calling transform shouldn't call `prepare_interpolation_grid` embedding.transform(x2) prep_grid.assert_not_called()
def fit(self, X: DataFrame, y: Series = None): def callback(iteration, error, embedding): print(f'Current Iteration: {iteration} (error: {error})') self.builder.recordProgress() tsne = openTSNE.TSNE(n_components=2, neighbors="exact", negative_gradient_method="bh", callbacks=callback, callbacks_every_iters=25, **self.params) self._model = tsne.fit(X.values)
def test_random_state_parameter_is_propagated_pca_init_approx(self, init, neighbors): random_state = 1 tsne = openTSNE.TSNE( neighbors="approx", initialization="pca", random_state=random_state, ) tsne.prepare_initial(self.x) # Verify that `random_state` was passed init.assert_called_once() check_mock_called_with_kwargs(init, {"random_state": random_state}) neighbors.assert_called_once() check_mock_called_with_kwargs(neighbors, {"random_state": random_state})
def test_iris_fft_transform_equivalency_with_one_by_one(self): """Compare one by one embedding vs all at once using FFT gradients. Note that this won't return the exact same embedding both times because the grid placed over the embedding will differ when placing points one by one vs. when placing them at once. The min/max coords will differ, thus changing the overall approximation. They should be quite similar though. """ x_train, x_test = train_test_split(self.iris.data, test_size=0.1, random_state=42) # Set up the initial embedding embedding = openTSNE.TSNE( early_exaggeration_iter=0, n_iter=50, neighbors="exact", negative_gradient_method="fft", ).fit(x_train) # Changing the gradients using clipping changes how the points move # sufficiently so that the interpolation grid is shifted. This test is # more reliable when we don't do gradient clipping and reduce the # learning rate. We increase the number of iterations so that the points # have time to move around params = dict(perplexity=5) # Build up an embedding by adding points one by one new_embedding_1 = np.vstack([ embedding.transform(np.atleast_2d(point), **params) for point in x_test ]) # Add new points altogether new_embedding_2 = embedding.transform(x_test, **params) # Verify that the embedding has actually been optimized self.assertRaises( AssertionError, np.testing.assert_almost_equal, embedding.prepare_partial(x_test, perplexity=params["perplexity"]), new_embedding_1, ) # Check that both methods produced the same embedding np.testing.assert_almost_equal(new_embedding_1, new_embedding_2, decimal=2)
def test_same_unoptimized_initializations_for_transform(self): """Initializations should be deterministic.""" x_train, x_test = train_test_split(self.iris, test_size=0.33, random_state=42) embedding = openTSNE.TSNE( early_exaggeration_iter=50, n_iter=50, neighbors="exact", negative_gradient_method="bh", random_state=42, ).fit(x_train) for init in self.transform_initializations: new_embedding_1 = embedding.prepare_partial(x_test, initialization=init) new_embedding_2 = embedding.prepare_partial(x_test, initialization=init) np.testing.assert_equal(new_embedding_1, new_embedding_2, init)
def __init__(self, mode, perp, option='std', init='random', n_components=2, n_iter=500): super().__init__(mode) self.perp = perp self.mode = mode self.init = init # self.function = sklearn.manifold.TSNE(n_components=n_components, perplexity=perp, init=init, n_iter=n_iter) self.function = openTSNE.TSNE( perplexity=perp, initialization=init, metric="cosine", n_jobs=8, random_state=3, ) self.option = option
def test_iris_fft_transform_correctness(self): x_train, x_test, y_train, y_test = train_test_split( self.iris.data, self.iris.target, test_size=0.33, random_state=42 ) # Set up the initial embedding embedding = openTSNE.TSNE( neighbors="exact", negative_gradient_method="fft", early_exaggeration_iter=0, n_iter=50, random_state=0, ).fit(x_train) # Evaluate t-SNE optimization using a KNN classifier knn = KNeighborsClassifier(n_neighbors=10) knn.fit(embedding, y_train) new_embedding = embedding.transform(x_test, n_iter=100) predictions = knn.predict(new_embedding) self.assertGreater(accuracy_score(predictions, y_test), 0.95)