-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_fast_tsne.py
More file actions
175 lines (115 loc) · 4.1 KB
/
check_fast_tsne.py
File metadata and controls
175 lines (115 loc) · 4.1 KB
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
RANDOM_SEED = 42
def benchmark_tsne():
import os
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from memory_profiler import memory_usage
from sklearn.manifold import TSNE
from openTSNE import TSNE as FastTSNE
save_path = './results/tsne_benchmark'
os.makedirs(save_path, exist_ok=True)
rng = np.random.RandomState(RANDOM_SEED)
run = True
if run:
def measure_block(func):
start = time.time()
mem_usage, out = memory_usage(
func,
interval=0.01,
timestamps=False,
retval=True,
)
end = time.time()
peak_mem_gb = max(mem_usage) / 1024 # MB to GB
wall_time = end - start
return peak_mem_gb, wall_time, out
def run_tsne_scikit_learn(x: np.ndarray):
reducer = TSNE(n_jobs=8, random_state=RANDOM_SEED)
x_tsne = reducer.fit_transform(X=x)
return x_tsne
def run_tsne_opentsne(x: np.ndarray):
reducer = FastTSNE(n_jobs=8, random_state=RANDOM_SEED)
x_tsne = reducer.fit(X=x)
return x_tsne
n_features = [10, ]
sample_sizes = [100, 1000, 10000, 20000, 30000, 40000, 50000]
methods = ['Scikit-learn', 'openTSNE']
method_to_fct = {
'Scikit-learn': run_tsne_scikit_learn,
'openTSNE': run_tsne_opentsne,
}
# Warmup run
reducer = FastTSNE(n_jobs=8, random_state=RANDOM_SEED)
reducer.fit(X=rng.randn(100, 10))
rows = []
for n in n_features:
print(f'# --- Num features: {n} --- ')
for sample_size in sample_sizes:
print(f'# --- Sample size: {sample_size} ')
x_test = rng.randn(sample_size, n)
for method in methods:
print(f'# --- {method}')
def tsne_block():
return method_to_fct[method](x_test)
mem_peak, wall_time, out = measure_block(tsne_block)
rows.append({
'method': method,
'n_features': n,
'n_samples': sample_size,
'mem_peak_gb': mem_peak,
'wall_time_s': wall_time,
})
res_df = pd.DataFrame(rows)
res_df.to_csv(os.path.join(save_path, 'res_df.csv'), index=False)
print(res_df)
res_df = pd.read_csv(os.path.join(save_path, 'res_df.csv'))
fig, ax = plt.subplots(dpi=600)
sns.lineplot(
data=res_df,
x='n_samples',
y='mem_peak_gb',
hue='method',
palette='Set2',
marker='o',
ax=ax,
)
ax.set_xlabel('Sample size')
ax.set_ylabel('Peak RSS [GB]')
ax.set_xscale('log')
ax.legend()
plt.tight_layout()
fig.savefig(os.path.join(save_path, 'peak_memory.png'))
plt.close(fig)
fig, ax = plt.subplots(dpi=600)
sns.lineplot(
data=res_df,
x='n_samples',
y='wall_time_s',
hue='method',
palette='Set2',
marker='o',
ax=ax,
)
ax.set_xlabel('Sample size')
ax.set_ylabel('Wall time [s]')
ax.set_xscale('log')
ax.legend()
plt.tight_layout()
fig.savefig(os.path.join(save_path, 'wall_time.png'))
plt.close(fig)
def minimal_example():
import numpy as np
from openTSNE import TSNE
x_train = np.random.randn(100, 10)
reducer = TSNE(n_jobs=-1)
x_tsne = reducer.fit(X=x_train)
print(x_tsne)
if __name__ == '__main__':
# INSTALLATION:
# conda install opentsne
# benchmark_tsne()
# minimal_example()
print('done')