-
Notifications
You must be signed in to change notification settings - Fork 3
/
w2v_embeddings.py
275 lines (236 loc) · 9.25 KB
/
w2v_embeddings.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
#%%[markdown]
# # Train and evaluate word2vec embeddings from sequences of drug orders
#%%[markdown]
# ## Imports
#%%
import os
import pathlib
import pickle
from datetime import datetime
from multiprocessing import cpu_count
import matplotlib.pyplot as plt
import pandas as pd
import scikitplot as skplt
import seaborn as sns
import umap
from gensim.sklearn_api import W2VTransformer
from mpl_toolkits import mplot3d
from sklearn import metrics
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import silhouette_score
from sklearn.model_selection import (GridSearchCV, TimeSeriesSplit,
cross_validate)
from sklearn.pipeline import Pipeline
from components import check_ipynb
#%%[markdown]
# ## Global variables
#%%[markdown]
# ### Save path
#
# Where everything will get saved. Will create a subdirectory
# called model with another subdirectory inside it with
# the date and time this block ran.
#%%
SAVE_STAMP = datetime.now().strftime('%Y%m%d-%H%M')
SAVE_PATH = os.path.join(os.getcwd(), 'paper', 'model', SAVE_STAMP + 'word2vec')
pathlib.Path(SAVE_PATH).mkdir(parents=True, exist_ok=True)
#%%[markdown]
# ### Data variables
#
# DATA_DIR indicates in which subdirectory of "preprocessed_data"
# to look for the profiles_list.pkl file generated by the preprocessor.
#%%
DATA_DIR = '5yr'
#%%[markdown]
# ### Word2vec hyperparameters
#
# #### Grid search hyperparameters
# This grid will be used when performing grid search
#%%
W2V_GRID = {
'w2v__alpha': [0.01,0.013,0.02],
'w2v__iter': [16,32,64],
'w2v__size': [64,128,256],
'w2v__hs': [0,1],
'w2v__sg': [0,1],
'w2v__min_count': [3,5,7],
'w2v__workers':[1],
}
#%%[markdown]
# ### Clustering hyperparameters
#
# #### Grid search hyperparameters
# This grid will be used when performing grid search
#%%
CLUST_GRID = {
'ac__n_clusters': [32,33,34,35,36,37,38,39,40,41,42],
}
#%%[markdown]
# ## Data
#
# Load the sequences
#%%
profiles_path = os.path.join(os.getcwd(), 'paper', 'preprocessed_data', DATA_DIR, 'profiles_list.pkl')
print('Loading data from path: {}...'.format(profiles_path))
with open(profiles_path, mode='rb') as file:
data = pickle.load(file)
data = list(data.values())
print('Data successfully loaded.')
#%%[markdown]
# ## Transformers
#
# Prepare the word2vec and clustering transformers
#%%[markdown]
# ### Word2vec transformer
#%%
w2v_pipe = Pipeline([
('w2v', W2VTransformer()),
])
#%%[markdown]
# ### Clustering transformer
#%%
clust_pipe = Pipeline([
('ac', AgglomerativeClustering()),
])
#%%[markdown]
# ## Helper functions
#
# These are scoring functions that will be used to score
# the word2vec embeddings and the clustering of the embeddings.
#%%
in_ipynb = check_ipynb().is_inipynb()
def accuracy_scorer_gensim(pipe, X=None, y=None):
acc_dict = pipe.named_steps['w2v'].gensim_model.wv.accuracy('paper/data/eval_analogy.txt') # YOUR ANALOGY FILE HERE, see https://radimrehurek.com/gensim/models/keyedvectors.html#gensim.models.keyedvectors.WordEmbeddingsKeyedVectors.accuracy for specifications.
accuracy = len(acc_dict[1]['correct'])/((len(acc_dict[1]['correct'])) + (len(acc_dict[1]['incorrect'])))
print('Accuracy is : {:.3f}'.format(accuracy))
return accuracy
def silhouette_scorer_cosine(pipe, X=None, y=None):
clusters = pipe.named_steps['ac'].fit_predict(X)
score = silhouette_score(X, clusters, metric='cosine')
print('Score is: {}'.format(score))
return score
#%%[markdown]
# ## Word2vec grid search
#
# Search for the best word2vec hyperparameters, then plot the results
#%%
print('Performing grid search for word2vec embeddings...')
w2v_gscv = GridSearchCV(w2v_pipe, W2V_GRID, scoring={'acc':accuracy_scorer_gensim}, cv=TimeSeriesSplit(n_splits=3), refit='acc', error_score=0, return_train_score=False, n_jobs=-2, verbose=1) # n_jobs=-2 otherwise is too cpu intensive and hangs the machine
w2v_gscv.fit(data)
# Convert results to a dataframe and save it
print('Saving results of grid search for word2vec embeddings...')
w2v_gscv_results_df = pd.DataFrame.from_dict(w2v_gscv.cv_results_)
w2v_gscv_results_df.to_csv(os.path.join(SAVE_PATH,'word2vec_gridsearch_results.csv'))
# Sort by rank
w2v_gscv_results_df.set_index('rank_test_acc', inplace=True)
# Select only useful columns
w2v_gscv_results_filtered = w2v_gscv_results_df[['split0_test_acc', 'split1_test_acc', 'split2_test_acc']].copy()
# Rename columns to clearer names
w2v_gscv_results_filtered.rename(inplace=True, index=str, columns={'split0_test_acc': 'Test analogy score', 'split1_test_acc': 'Test analogy score', 'split2_test_acc': 'Test analogy score'})
# Structure the dataframe as expected by Seaborn
w2v_gscv_results_graph_df = w2v_gscv_results_filtered.stack().reset_index()
w2v_gscv_results_graph_df.rename(inplace=True, index=str, columns={'rank_test_acc':'Rank', 'level_1':'Metric', 0:'Result'})
# Make sure the epochs are int to avoid weird ordering effects in the plot
w2v_gscv_results_graph_df['Rank'] = w2v_gscv_results_graph_df['Rank'].astype('int8')
# Plot
sns.set(style='darkgrid')
sns.relplot(x='Rank', y='Result', hue='Metric', kind='line', data=w2v_gscv_results_graph_df)
# Output the plot
if in_ipynb:
plt.show()
else:
plt.savefig(os.path.join(SAVE_PATH, 'word2vec_grid_search_results.png'))
# Clear
plt.gcf().clear()
#%%[markdown]
# ## Clustering grid search
#
# Search for the best clustering hyperparameters from the
# fitted w2v grid search, then plot the results
#%%
print('Reducing dimensionality of word2vec embeddings for clustering...')
# Get the normalized word2vec embeddings
w2v_gscv.best_estimator_.named_steps['w2v'].gensim_model.init_sims(replace=True)
vectors = w2v_gscv.best_estimator_.named_steps['w2v'].gensim_model.wv.vectors
# Reduce dimensionality to 3D using UMAP
umapper = umap.UMAP(n_components=3)
umap_vectors = umapper.fit_transform(vectors)
# Do the clustering
print('Performing grid search for clustering...')
clust_gscv = GridSearchCV(clust_pipe, CLUST_GRID, scoring={'sil':silhouette_scorer_cosine}, cv=TimeSeriesSplit(n_splits=3), refit='sil', error_score=0, return_train_score=False, n_jobs=-2, verbose=1) # n_jobs=-2 otherwise is too cpu intensive and hangs the machine
clust_gscv.fit(umap_vectors)
# Convert results to a dataframe and save it
print('Saving results of grid search for clustering...')
clust_cv_results_df = pd.DataFrame.from_dict(clust_gscv.cv_results_)
clust_cv_results_df.to_csv(os.path.join(SAVE_PATH, 'clustering_grid_search_results.csv'))
# You want this to be sorted by number of clusters otherwise weird plot
clust_cv_results_df.sort_values(by='param_ac__n_clusters', inplace=True)
# Select only useful columns
clust_cv_results_filtered = clust_cv_results_df[['param_ac__n_clusters', 'split0_test_sil', 'split1_test_sil', 'split2_test_sil']].copy()
# Rename columns to clearer names
clust_cv_results_filtered.rename(inplace=True, index=str, columns={'split0_test_sil': 'Test silhouette', 'split1_test_sil': 'Test silhouette', 'split2_test_sil': 'Test silhouette', 'param_ac__n_clusters':'k'})
# Structure the dataframe as expected by Seaborn
clust_cv_results_graph_df = clust_cv_results_filtered.set_index('k').stack().reset_index()
clust_cv_results_graph_df.rename(inplace=True, index=str, columns={'level_1':'Metric', 0:'Result'})
# Plot
sns.set(style='darkgrid')
sns.relplot(x='k', y='Result', hue='Metric', kind='line', data=clust_cv_results_graph_df)
# Output the plot
if in_ipynb:
plt.show()
else:
plt.savefig(os.path.join(SAVE_PATH, 'cluster_grid_search_results.png'))
# Clear
plt.gcf().clear()
#%%[markdown]
# ## Final embeddings
# Get final accuracy
acc = accuracy_scorer_gensim(w2v_gscv.best_estimator_)
# Get the normalized embeddings
vectors = w2v_gscv.best_estimator_.named_steps['w2v'].gensim_model.wv.vectors
# Reduce dimensionality for clustering and plotting
print('Reducing dimensionality of final word2vec embeddings for clustering...')
umapper = umap.UMAP(n_components=3)
umap_vectors = umapper.fit_transform(vectors)
# Cluster
print('Performing clustering...')
clusters = clust_gscv.best_estimator_.fit_predict(umap_vectors)
# Plot the silhouette graph
print('Plotting silhouette graph...')
skplt.metrics.plot_silhouette(umap_vectors, clusters, metric='cosine')
# Output the plot
if in_ipynb:
plt.show()
else:
plt.savefig(os.path.join(SAVE_PATH, 'silhouette_plot.png'))
# Clear
plt.gcf().clear()
# Plot the 3d clustered embeddings
# Make a list of 3d coordinates and and associated cluster for each drug
print('Plotting clustered 3d-UMAP projected embeddings...')
graph_data = []
index2entity = w2v_gscv.best_estimator_.named_steps['w2v'].gensim_model.wv.index2entity
for umapcoords, cluster, entity in zip(umap_vectors, clusters, index2entity):
graph_data.append([umapcoords[0], umapcoords[1], umapcoords[2], cluster, entity])
# Convert to dataframe
graph_data_df = pd.DataFrame(data=graph_data, columns=['x', 'y', 'z', 'cluster', 'entity'])
# Save the dataframe (to eventually manually label clusters)
graph_data_df.sort_values(by='cluster', inplace=True)
graph_data_df.to_csv(os.path.join(SAVE_PATH, 'graph_dataframe.csv'))
# Plot
ax = plt.figure(figsize=(16,10)).gca(projection='3d')
ax.scatter(
xs=graph_data_df['x'] ,
ys=graph_data_df['y'] ,
zs=graph_data_df['z'] ,
c=graph_data_df['cluster'],
cmap='prism'
)
# Output the plot
if in_ipynb:
plt.show()
else:
plt.savefig(os.path.join(SAVE_PATH, 'clusted_embeddings_plot.png'))
# Clear
plt.gcf().clear()
#%%