-
Notifications
You must be signed in to change notification settings - Fork 2
/
01_1_train_CF.py
327 lines (258 loc) · 9.47 KB
/
01_1_train_CF.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.2
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# # Collaborative Filtering
# %% tags=["hide-input"]
import logging
from pprint import pprint
import matplotlib.pyplot as plt
# overwriting Recorder callback with custom plot_loss
from fastai import learner
from fastai.collab import *
from fastai.collab import (EarlyStoppingCallback, EmbeddingDotBias, Learner,
MSELossFlat, default_device)
from fastai.tabular.all import *
import pimmslearn
import pimmslearn.model
import pimmslearn.models as models
import pimmslearn.nb
from pimmslearn.io import datasplits
from pimmslearn.logging import setup_logger
from pimmslearn.models import RecorderDump, plot_loss
learner.Recorder.plot_loss = plot_loss
# import fastai.callback.hook # Learner.summary
logger = setup_logger(logger=logging.getLogger('pimmslearn'))
logger.info(
"Experiment 03 - Analysis of latent spaces and performance comparisions")
figures = {} # collection of ax or figures
# %% [markdown]
# Papermill script parameters:
# %% tags=["hide-input"]
# catch passed parameters
args = None
args = dict(globals()).keys()
# %% tags=["parameters"]
# files and folders
# Datasplit folder with data for experiment
folder_experiment: str = 'runs/example'
folder_data: str = '' # specify data directory if needed
file_format: str = 'csv' # change default to pickled files
# training
epochs_max: int = 20 # Maximum number of epochs
# early_stopping:bool = True # Wheather to use early stopping or not
patience: int = 1 # Patience for early stopping
batch_size: int = 32_768 # Batch size for training (and evaluation)
cuda: bool = True # Use the GPU for training?
# model
# Dimensionality of encoding dimension (latent space of model)
latent_dim: int = 10
sample_idx_position: int = 0 # position of index which is sample ID
model: str = 'CF' # model name
model_key: str = 'CF' # potentially alternative key for model (grid search)
save_pred_real_na: bool = True # Save all predictions for missing values
# %% [markdown]
# Some argument transformations
# %% tags=["hide-input"]
args = pimmslearn.nb.get_params(args, globals=globals())
args
# %% tags=["hide-input"]
args = pimmslearn.nb.args_from_dict(args)
# # Currently not needed -> DotProduct used, not a FNN
# if isinstance(args.hidden_layers, str):
# args.overwrite_entry("hidden_layers", [int(x) for x in args.hidden_layers.split('_')])
# else:
# raise ValueError(f"hidden_layers is of unknown type {type(args.hidden_layers)}")
args
# %% [markdown]
# Some naming conventions
# %% tags=["hide-input"]
TEMPLATE_MODEL_PARAMS = 'model_params_{}.json'
if not args.cuda:
default_device(use=False) # set to cpu
# %% [markdown]
# ## Load data in long format
# %% tags=["hide-input"]
data = datasplits.DataSplits.from_folder(
args.data, file_format=args.file_format)
# %% [markdown]
# data is loaded in long format
# %% tags=["hide-input"]
data.train_X
# %% tags=["hide-input"]
# # ! add check that specified data is available
# silent error in fastai if e.g. target column is not available
# %% [markdown]
# Infer index names from long format
# %% tags=["hide-input"]
index_columns = list(data.train_X.index.names)
sample_id = index_columns.pop(args.sample_idx_position)
if len(index_columns) == 1:
index_column = index_columns.pop()
index_columns = None
logger.info(f"{sample_id = }, single feature: {index_column = }")
else:
logger.info(f"{sample_id = }, multiple features: {index_columns = }")
if not index_columns:
index_columns = [sample_id, index_column]
else:
raise NotImplementedError(
"More than one feature: Needs to be implemented. see above logging output.")
# %% [markdown]
# ### Use some simulated missing for evaluation
# %% [markdown]
# The validation simulated NA is used to by all models to evaluate training performance.
# %% tags=["hide-input"]
val_pred_simulated_na = data.val_y.to_frame(name='observed')
val_pred_simulated_na
# %% tags=["hide-input"]
test_pred_simulated_na = data.test_y.to_frame(name='observed')
test_pred_simulated_na.describe()
# %% [markdown]
# ## Collaborative Filtering
#
# - save custom collab batch size (increase AE batch size by a factor), could be setup separately.
# - the test data is used to evaluate the performance after training
# %% tags=["hide-input"]
# larger mini-batches speed up training
ana_collab = models.collab.CollabAnalysis(
datasplits=data,
sample_column=sample_id,
item_column=index_column, # not generic
target_column='intensity',
model_kwargs=dict(n_factors=args.latent_dim,
y_range=(int(data.train_X.min()),
int(data.train_X.max()) + 1)
),
batch_size=args.batch_size)
# %% tags=["hide-input"]
print("Args:")
pprint(ana_collab.model_kwargs)
# %% tags=["hide-input"]
ana_collab.model = EmbeddingDotBias.from_classes(
classes=ana_collab.dls.classes,
**ana_collab.model_kwargs)
args.n_params = models.calc_net_weight_count(ana_collab.model)
ana_collab.params['n_parameters'] = args.n_params
ana_collab.learn = Learner(dls=ana_collab.dls, model=ana_collab.model, loss_func=MSELossFlat(),
cbs=EarlyStoppingCallback(patience=args.patience),
model_dir=args.out_models)
if args.cuda:
ana_collab.learn.model = ana_collab.learn.model.cuda()
else:
# try to set explicitly cpu in case not cuda
# MPS logic might not work properly in fastai yet https://github.com/fastai/fastai/pull/3858
ana_collab.learn.model = ana_collab.learn.model.cpu()
# learn.summary() # see comment at DAE
# %% [markdown]
# ### Training
# %% tags=["hide-input"]
# papermill_description=train_collab
suggested_lr = ana_collab.learn.lr_find()
print(f"{suggested_lr.valley = :.5f}")
ana_collab.learn.fit_one_cycle(args.epochs_max, lr_max=suggested_lr.valley)
args.epoch_trained = ana_collab.learn.epoch + 1
# ana_collab.learn.fit_one_cycle(args.epochs_max, lr_max=1e-3)
ana_collab.model_kwargs['suggested_inital_lr'] = suggested_lr.valley
ana_collab.learn.save('collab_model')
fig, ax = plt.subplots(figsize=(15, 8))
ax.set_title('CF loss: Reconstruction loss')
ana_collab.learn.recorder.plot_loss(skip_start=5, ax=ax)
recorder_dump = RecorderDump(
recorder=ana_collab.learn.recorder, name='CF')
recorder_dump.save(args.out_figures)
del recorder_dump
pimmslearn.savefig(fig, name='collab_training',
folder=args.out_figures)
ana_collab.model_kwargs['batch_size'] = ana_collab.batch_size
pimmslearn.io.dump_json(ana_collab.model_kwargs, args.out_models /
TEMPLATE_MODEL_PARAMS.format('CF'))
# %% [markdown]
# ### Predictions
# %% [markdown]
# Compare simulated_na data predictions to original values
# %% tags=["hide-input"]
# this could be done using the validation data laoder now
ana_collab.test_dl = ana_collab.dls.test_dl(
data.val_y.reset_index()) # test_dl is here validation data
val_pred_simulated_na['CF'], _ = ana_collab.learn.get_preds(
dl=ana_collab.test_dl)
val_pred_simulated_na
# %% [markdown]
# select test data predictions
# %% tags=["hide-input"]
ana_collab.test_dl = ana_collab.dls.test_dl(data.test_y.reset_index())
test_pred_simulated_na['CF'], _ = ana_collab.learn.get_preds(dl=ana_collab.test_dl)
test_pred_simulated_na
# %% tags=["hide-input"]
if args.save_pred_real_na:
pred_real_na = models.collab.get_missing_values(
df_train_long=data.train_X,
val_idx=data.val_y.index,
test_idx=data.test_y.index,
analysis_collab=ana_collab)
pred_real_na.to_csv(args.out_preds / f"pred_real_na_{args.model_key}.csv")
# %% [markdown]
# ## Data in wide format
#
# - Autoencoder need data in wide format
# %% tags=["hide-input"]
data.to_wide_format()
args.M = data.train_X.shape[-1]
data.train_X.head()
# %% [markdown]
# ### Validation data
#
# - all measured (identified, observed) peptides in validation data
#
# > Does not make to much sense to compare collab and AEs,
# > as the setup differs of training and validation data differs
# %% tags=["hide-input"]
# papermill_description=metrics
d_metrics = models.Metrics()
# %% [markdown]
# The simulated NA for the validation step are real test data (not used for training nor early stopping)
# %%
added_metrics = d_metrics.add_metrics(val_pred_simulated_na, 'valid_simulated_na')
added_metrics
# %% [markdown]
# ### Test Datasplit
#
# Simulated NAs : Artificially created NAs. Some data was sampled and set
# explicitly to misssing before it was fed to the model for
# reconstruction.
# %%
added_metrics = d_metrics.add_metrics(test_pred_simulated_na, 'test_simulated_na')
added_metrics
# %% [markdown]
# Save all metrics as json
# %%
pimmslearn.io.dump_json(d_metrics.metrics, args.out_metrics /
f'metrics_{args.model_key}.json')
# %%
metrics_df = models.get_df_from_nested_dict(
d_metrics.metrics, column_levels=['model', 'metric_name']).T
metrics_df
# %% [markdown]
# ## Save predictions
# %% tags=["hide-input"]
# save simulated missing values for both splits
val_pred_simulated_na.to_csv(args.out_preds / f"pred_val_{args.model_key}.csv")
test_pred_simulated_na.to_csv(args.out_preds / f"pred_test_{args.model_key}.csv")
# %% [markdown]
# ## Config
# %% tags=["hide-input"]
args.dump(fname=args.out_models / f"model_config_{args.model_key}.yaml")
args
# %% tags=["hide-input"]