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
| from transformers import set_seed import os import torch import random import numpy as np from sklearn.manifold import TSNE from fitsne import FItSNE from tsnecuda import TSNE as CudaTSNE import math import matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator import time from functools import reduce
def quzheng(num:float): if num<0: return math.floor(num) else: return math.ceil(num)
if __name__ == "__main__": seed = 1234 setup_seed(seed)
m = 2 n = 3072 data1 = np.random.uniform(-100, 100, size=(n, 1)).astype(np.float32) data2 = np.random.uniform(-100, 100, size=(n, 1)).astype(np.float32) data = np.column_stack((data1, data2)) origin_data = data.copy() data_dict = dict()
tsne = TSNE(n_components = 2,perplexity=30,verbose=1,max_iter=2000,random_state=seed,init='random') start_time = time.perf_counter() tsne_data = tsne.fit_transform(data) end_time = time.perf_counter() execution_time = end_time-start_time print("tsne excute cost ",execution_time) if not np.array_equal(data,origin_data): print("data change!") exit(0) data_dict['tsne'] = tsne_data
start_time = time.perf_counter() fit_tsne_data = FItSNE(data.astype(np.float64), perplexity=30, max_iter=2000,fft_not_bh=True,rand_seed=seed,initialization='random') end_time = time.perf_counter() execution_time = end_time-start_time print("fit-tsne excute cost ",execution_time) if not np.array_equal(data,origin_data): print("data change!") exit(0) data_dict['fit-tsne'] = fit_tsne_data
cuda_tsne = CudaTSNE(n_iter=2000, verbose=1, perplexity=30,random_seed=seed) start_time = time.perf_counter() cuda_tsne_data = cuda_tsne.fit_transform(data) end_time = time.perf_counter() execution_time = end_time-start_time print("cuda-tsne excute cost ",execution_time) if not np.array_equal(data,origin_data): print("data change!") exit(0) data_dict['cuda-tsne'] = cuda_tsne_data
xy_min_max_list = list(map(quzheng,reduce(lambda x,y:[min(x[0],y[0]),min(x[1],y[1]),max(x[2],y[2]),max(x[3],y[3])],map(lambda x:[np.min(x[:,0]),np.min(x[:,1]),np.max(x[:,0]),np.max(x[:,1])],data_dict.values())))) print("get xy_range is ",xy_min_max_list) x_min = xy_min_max_list[0] y_min = xy_min_max_list[1] x_max = xy_min_max_list[2] y_max = xy_min_max_list[3] plt.figure(figsize=(32, 24)) plt.suptitle(f"Tsne Test",fontsize=40, fontweight='bold', fontstyle='italic', color='blue' ) keys_list = list(data_dict.keys()) for i in range(0,len(keys_list)): sample = data_dict[keys_list[i]] ax = plt.subplot(1,len(keys_list),i+1) ax.xaxis.set_major_locator(MultipleLocator(25)) ax.yaxis.set_major_locator(MultipleLocator(25)) ax.scatter(sample[:, 0], sample[:, 1], s=5, alpha=0.6) ax.set_title(f'{keys_list[i]}') ax.set_ylim(y_min,y_max) ax.set_xlim(x_min,x_max) ax.set_aspect('equal', 'box') plt.tight_layout() save_file_path = "./tsne" plt.savefig(save_file_path,dpi=300) plt.close()
|