-
Notifications
You must be signed in to change notification settings - Fork 45
/
trainers.py
1328 lines (1132 loc) · 61.5 KB
/
trainers.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2022 The BayesFlow Developers
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import logging
import os
from pickle import load as pickle_load
import numpy as np
import tensorflow as tf
from tqdm.autonotebook import tqdm
from bayesflow.amortizers import (
AmortizedLikelihood,
AmortizedModelComparison,
AmortizedPosterior,
AmortizedPosteriorLikelihood,
)
from bayesflow.computational_utilities import maximum_mean_discrepancy
from bayesflow.configuration import *
from bayesflow.default_settings import DEFAULT_KEYS, OPTIMIZER_DEFAULTS, TQDM_MININTERVAL
from bayesflow.diagnostics import plot_latent_space_2d, plot_sbc_histograms
from bayesflow.exceptions import ArgumentError, SimulationError
from bayesflow.helper_classes import (
EarlyStopper,
LossHistory,
MemoryReplayBuffer,
MultiSimulationDataset,
SimulationDataset,
SimulationMemory,
)
from bayesflow.helper_functions import backprop_step, extract_current_lr, format_loss_string, loss_to_string
from bayesflow.simulation import GenerativeModel, MultiGenerativeModel
logging.basicConfig()
class Trainer:
"""This class connects a generative model (or, already simulated data from a model) with
a configurator and a neural inference architecture for amortized inference (amortizer). A Trainer
instance is responsible for optimizing the amortizer via various forms of simulation-based training.
At the very minimum, the trainer must be initialized with an `amortizer` instance, which is capable
of processing the (configured) outputs of a generative model. A `configurator` will then process
the outputs of the generative model and convert them into suitable inputs for the amortizer. Users
can choose from a palette of default configurators or create their own configurators, essentially
building a modularized pipeline `GenerativeModel` -> `Configurator` -> `Amortizer`. Most complex models
will require custom configurators.
Notes
-----
Currently, the trainer supports the following simulation-based training regimes, based on efficiency
considerations:
* Online training
>>> trainer.train_online(epochs, iterations_per_epoch, batch_size, **kwargs)
This training regime is optimal for fast generative models which can efficiently simulated data on-the-fly.
In order for this training regime to be efficient, on-the-fly batch simulations should not take longer
than 2-3 seconds.
* Experience replay training
>>> trainer.train_experience_replay(epochs, iterations_per_epoch, batch_size, **kwargs)
This training regime is also good for fast generative models capable of efficiently simulating data on-the-fly.
Compare to pure online training, this training will keep an experience replay buffer from which simulations
are randomly sampled, so the networks will likely see some simulations multiple times.
* Round-based training
>>> trainer.train_rounds(rounds, sim_per_round, epochs, batch_size, **kwargs)
This training regime is optimal for slow, but still reasonably performant generative models.
In order for this training regime to be efficient, on-the-fly batch simulations should not take
longer than 2-3 minutes.
.. note:: overfitting presents a danger when using small numbers of simulated data sets, so it is recommended
to use some amount of regularization for the neural amortizer(s).
* Offline training
>>> trainer.train_offline(simulations_dict, epochs, batch_size, **kwargs)
This training regime is optimal for very slow, external simulators, which take several minutes for a
single simulation. It assumes that all training data has been already simulated and stored on disk.
.. warning:: Overfitting presents a danger when using a small simulated data set, so it is recommended to use
some amount of regularization for the neural amortizer(s).
.. note::
For extremely slow simulators (i.e., more than an hour of a single simulation), the BayesFlow framework
might not be the ideal choice and should probably be considered in combination with a black-box surrogate
optimization method, such as Bayesian optimization.
"""
def __init__(
self,
amortizer,
generative_model=None,
configurator=None,
checkpoint_path=None,
max_to_keep=3,
default_lr=0.0005,
skip_checks=False,
memory=False,
**kwargs,
):
"""Creates a trainer which will use a generative model (or data simulated from it) to optimize
a neural architecture (amortizer) for amortized posterior inference, likelihood inference, or both.
Parameters
----------
amortizer : `bayesflow.amortizers.Amortizer`
The neural architecture to be optimized.
generative_model : `bayesflow.forward_inference.GenerativeModel`
A generative model returning a dictionary with randomly sampled parameters, data, and optional context
configurator : callable or None, optional, default: None
A callable object transforming and combining the outputs of the generative model into inputs for a BayesFlow
amortizer.
checkpoint_path : string or None, optional, default: None
Optional file path for storing the trained amortizer, loss history and optional memory.
max_to_keep : int, optional, default: 3
Number of checkpoints and loss history snapshots to keep.
default_lr : float, optional, default: 0.0005
The default learning rate to use for default optimizers.
skip_checks : bool, optional, default: False
If True, do not perform consistency checks, i.e., simulator runs and passed through nets
memory : bool or bayesflow.SimulationMemory, optional, default: False
If ``True``, store a pre-defined amount of simulations for later use (validation, etc.).
If `SimulationMemory` instance provided, stores a reference to the instance.
Otherwise the corresponding attribute will be set to None.
Other Parameters:
-----------------
memory_kwargs : dict
Keyword arguments to be passed to the `SimulationMemory` instance, if ``memory=True``
num_models : int
The number of models in an amortized model comparison scenario, in case of a custom model comparison
amortizer which does not have a num_models attribute.
"""
# Set-up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
self.amortizer = amortizer
self.generative_model = generative_model
if self.generative_model is None:
logger.info(
"Trainer initialization: No generative model provided. Only offline learning mode is available!"
)
# Determine n models in case model comparison mode
if type(generative_model) is MultiGenerativeModel:
_num_models = generative_model.num_models
elif type(amortizer) is AmortizedModelComparison:
_num_models = amortizer.num_models
else:
_num_models = kwargs.get("num_models")
# Set-up configurator
self.configurator = self._manage_configurator(configurator, num_models=_num_models)
# Set-up memory classes
self.loss_history = LossHistory()
if memory is True:
self.simulation_memory = SimulationMemory(**kwargs.pop("memory_kwargs", {}))
elif type(memory) is SimulationMemory:
self.simulation_memory = memory
else:
self.simulation_memory = None
# Set-up replay buffer and optimizer attributes
self.replay_buffer = None
self.optimizer = None
self.default_lr = default_lr
# Checkpoint and helper classes settings
self.max_to_keep = max_to_keep
if checkpoint_path is not None:
self.checkpoint = tf.train.Checkpoint(model=self.amortizer)
self.manager = tf.train.CheckpointManager(self.checkpoint, checkpoint_path, max_to_keep=max_to_keep)
self.checkpoint.restore(self.manager.latest_checkpoint)
self.loss_history.load_from_file(checkpoint_path)
if self.simulation_memory is not None:
self.simulation_memory.load_from_file(checkpoint_path)
if self.manager.latest_checkpoint:
logger.info("Networks loaded from {}".format(self.manager.latest_checkpoint))
else:
logger.info("Initialized networks from scratch.")
else:
self.checkpoint = None
self.manager = None
self.checkpoint_path = checkpoint_path
# Perform a sanity check with provided components
if not skip_checks:
self._check_consistency()
def diagnose_latent2d(self, inputs=None, **kwargs):
"""Performs visual pre-inference diagnostics of latent space on either provided validation data
(new simulations) or internal simulation memory.
If ``inputs is not None``, then diagnostics will be performed on the inputs, regardless
whether the `simulation_memory` of the trainer is empty or not. If ``inputs is None``, then
the trainer will try to access is memory or raise a `ConfigurationError`.
Parameters
----------
inputs : None, list, or dict, optional, default: None
The optional inputs to use
Other Parameters
----------------
conf_args :
optional keyword arguments passed to the configurator
net_args :
optional keyword arguments passed to the amortizer
plot_args :
optional keyword arguments passed to `plot_latent_space_2d`
Returns
-------
fig : plt.Figure
The figure object which can be readily saved to disk using `fig.savefig()`.
"""
if type(self.amortizer) is AmortizedPosterior:
# If no inputs, try memory and throw if no memory
if inputs is None:
if self.simulation_memory is None:
raise ConfigurationError(
"You should either enable simulation memory or supply the inputs argument."
)
else:
inputs = self.simulation_memory.get_memory()
else:
inputs = self.configurator(inputs, **kwargs.pop("conf_args", {}))
# Do inference
if type(inputs) is list:
z, _ = self.amortizer.call_loop(inputs, **kwargs.pop("net_args", {}))
else:
z, _ = self.amortizer(inputs, **kwargs.pop("net_args", {}))
return plot_latent_space_2d(z, **kwargs.pop("plot_args", {}))
else:
raise NotImplementedError("Latent space diagnostics are only available for type AmortizedPosterior!")
def diagnose_sbc_histograms(self, inputs=None, n_samples=None, **kwargs):
"""Performs visual pre-inference diagnostics via simulation-based calibration (SBC)
(new simulations) or internal simulation memory.
If ``inputs is not None``, then diagnostics will be performed on the inputs, regardless
whether the `simulation_memory` of the trainer is empty or not. If ``inputs is None``, then
the trainer will try to access is memory or raise a `ConfigurationError`.
Parameters
----------
inputs : None, list or dict, optional, default: None
The optional inputs to use
n_samples : int or None, optional, default: None
The number of posterior samples to draw for each simulated data set.
If None, the number will be heuristically determined so that n_sim / n_draws is approximately equal to 20
Other Parameters
----------------
conf_args :
optional keyword arguments passed to the configurator
net_args :
optional keyword arguments passed to the amortizer
plot_args :
optional keyword arguments passed to `plot_sbc()`
Returns
-------
fig : plt.Figure
The figure object which can be readily saved to disk using `fig.savefig()`.
"""
if type(self.amortizer) is AmortizedPosterior:
# If no inputs, try memory and throw if no memory
if inputs is None:
if self.simulation_memory is None:
raise ConfigurationError("You should either ")
else:
inputs = self.simulation_memory.get_memory()
else:
inputs = self.configurator(inputs, **kwargs.pop("conf_args", {}))
# Heuristically determine the number of posterior samples
if n_samples is None:
if type(inputs) is list:
n_sim = np.sum([inp["parameters"].shape[0] for inp in inputs])
n_samples = int(np.ceil(n_sim / 20))
else:
n_samples = int(np.ceil(inputs["parameters"].shape[0] / 20))
# Do inference
if type(inputs) is list:
post_samples = self.amortizer.sample_loop(inputs, n_samples=n_samples, **kwargs.pop("net_args", {}))
prior_samples = np.concatenate([inp["parameters"] for inp in inputs], axis=0)
else:
post_samples = self.amortizer(inputs, n_samples, n_samples, **kwargs.pop("net_args", {}))
prior_samples = inputs["parameters"]
# Check for prior names and override keyword if available
plot_kwargs = kwargs.pop("plot_args", {})
if type(self.generative_model) is GenerativeModel and plot_kwargs.get("param_names") is None:
plot_kwargs["param_names"] = self.generative_model.param_names
return plot_sbc_histograms(post_samples, prior_samples, **plot_kwargs)
else:
raise NotImplementedError("SBC diagnostics are only available for type AmortizedPosterior!")
def load_pretrained_network(self):
"""Attempts to load a pre-trained network if checkpoint path is provided and a checkpoint manager exists."""
if self.manager is None or self.checkpoint is None:
return False
status = self.checkpoint.restore(self.manager.latest_checkpoint)
return status
def train_online(
self,
epochs,
iterations_per_epoch,
batch_size,
save_checkpoint=True,
optimizer=None,
reuse_optimizer=False,
early_stopping=False,
use_autograph=True,
validation_sims=None,
**kwargs,
):
"""Trains an amortizer via online learning. Additional keyword arguments
are passed to the generative mode, configurator, and amortizer.
Parameters
----------
epochs : int
Number of epochs (and number of times a checkpoint is stored)
iterations_per_epoch : int
Number of batch simulations to perform per epoch
batch_size : int
Number of simulations to perform at each backprop step
save_checkpoint : bool, default: True
A flag to decide whether to save checkpoints after each epoch,
if a checkpoint_path provided during initialization, otherwise ignored.
optimizer : tf.keras.optimizer.Optimizer or None
Optimizer for the neural network. ``None`` will result in ``tf.keras.optimizers.Adam``
using a learning rate of 5e-4 and a cosine decay from 5e-4 to 0. A custom optimizer
will override default learning rate and schedule settings.
reuse_optimizer : bool, optional, default: False
A flag indicating whether the optimizer instance should be treated as persistent or not.
If ``False``, the optimizer and its states are not stored after training has finished.
Otherwise, the optimizer will be stored as ``self.optimizer` and re-used in further training runs.
early_stopping : bool, optional, default: False
Whether to use optional stopping or not during training. Could speed up training.
Only works if ``validation_sims is not None``, i.e., validation data has been provided.
use_autograph : bool, optional, default: True
Whether to use autograph for the backprop step. Could lead to enormous speed-ups but
could also be harder to debug.
validation_sims : dict or None, optional, default: None
Simulations used as a "validation set".
If ``dict``, will assume it's the output of a generative model and try
``amortizer.compute_loss(configurator(validation_sims))``
after each epoch.
If ``int``, will assume it's the number of sims to generate from the generative
model before starting training. Only considered if a generative model has been
provided during initialization.
If ``None`` (default), no validation set will be used.
Other Parameters
----------------
model_args :
optional kwargs passed to the generative model
val_model_args:
optional kwargs passed to the generative model for generating validation data. Only useful if
``type(validation_sims) is int``.
conf_args :
optional kwargs passed to the configurator before each backprop (update) step.
val_conf_args :
optional kwargs passed to the configurator then configuring the validation data.
net_args :
optional kwargs passed to the amortizer
early_stopping_args :
optional kwargs passed to the `EarlyStopper`
Returns
-------
losses : dict or pandas.DataFrame
A dictionary storing the losses across epochs and iterations
"""
assert self.generative_model is not None, "No generative model found. Only offline training is possible!"
# Compile update function, if specified
if use_autograph:
_backprop_step = tf.function(backprop_step, reduce_retracing=True)
else:
_backprop_step = backprop_step
# Create new optimizer and initialize loss history
self._setup_optimizer(optimizer, epochs, iterations_per_epoch)
self.loss_history.start_new_run()
validation_sims = self._config_validation(validation_sims, **kwargs.pop("val_model_args", {}))
# Create early stopper, if conditions met, otherwise None returned
early_stopper = self._config_early_stopping(early_stopping, validation_sims, **kwargs)
# Loop through training epochs
for ep in range(1, epochs + 1):
with tqdm(total=iterations_per_epoch, desc=f"Training epoch {ep}", mininterval=TQDM_MININTERVAL) as p_bar:
for it in range(1, iterations_per_epoch + 1):
# Perform one training step and obtain current loss value
loss = self._train_step(batch_size, update_step=_backprop_step, **kwargs)
# Store returned loss
self.loss_history.add_entry(ep, loss)
# Compute running loss
avg_dict = self.loss_history.get_running_losses(ep)
# Extract current learning rate
lr = extract_current_lr(self.optimizer)
# Format for display on progress bar
disp_str = format_loss_string(ep, it, loss, avg_dict, lr=lr)
# Update progress bar
p_bar.set_postfix_str(disp_str, refresh=False)
p_bar.update(1)
# Store and compute validation loss, if specified
self._validation(ep, validation_sims, **kwargs)
self._save_trainer(save_checkpoint)
# Check early stopping, if specified
if self._check_early_stopping(early_stopper):
break
# Remove optimizer reference, if not set as persistent
if not reuse_optimizer:
self.optimizer = None
return self.loss_history.get_plottable()
def train_offline(
self,
simulations_dict,
epochs,
batch_size,
save_checkpoint=True,
optimizer=None,
reuse_optimizer=False,
early_stopping=False,
validation_sims=None,
use_autograph=True,
**kwargs,
):
"""Trains an amortizer via offline learning. Assume parameters, data and optional
context have already been simulated (i.e., forward inference has been performed).
Parameters
----------
simulations_dict : dict
A dictionary containing the simulated data / context, if using the default keys,
the method expects at least the mandatory keys ``sim_data`` and ``prior_draws`` to be present
epochs : int
Number of epochs (and number of times a checkpoint is stored)
batch_size : int
Number of simulations to perform at each backpropagation step
save_checkpoint : bool, default: True
Determines whether to save checkpoints after each epoch,
if a checkpoint_path provided during initialization, otherwise ignored.
optimizer : tf.keras.optimizer.Optimizer or None
Optimizer for the neural network. ``None`` will result in ``tf.keras.optimizers.Adam``
using a learning rate of 5e-4 and a cosine decay from 5e-4 to 0. A custom optimizer
will override default learning rate and schedule settings.
reuse_optimizer : bool, optional, default: False
A flag indicating whether the optimizer instance should be treated as persistent or not.
If ``False``, the optimizer and its states are not stored after training has finished.
Otherwise, the optimizer will be stored as ``self.optimizer`` and re-used in further training runs.
early_stopping : bool, optional, default: False
Whether to use optional stopping or not during training. Could speed up training.
Only works if ``validation_sims is not None``, i.e., validation data has been provided.
use_autograph : bool, optional, default: True
Whether to use autograph for the backprop step. Could lead to enormous speed-ups but
could also be harder to debug.
validation_sims : dict, int, or None, optional, default: None
Simulations used as a "validation set".
If ``dict``, will assume it's the output of a generative model and try
``amortizer.compute_loss(configurator(validation_sims))`` after each epoch.
If ``int``, will assume it's the number of sims to generate from the generative
model before starting training. Only considered if a generative model has been
provided during initialization.
If ``None`` (default), no validation set will be used.
Other Parameters
----------------
val_model_args :
optional kwargs passed to the generative model for generating validation data.
Only useful if ``type(validation_sims) is int``.
conf_args :
optional kwargs passed to the configurator before each backprop (update) step.
val_conf_args :
optional kwargs passed to the configurator then configuring the validation data.
net_args :
optional kwargs passed to the amortizer
early_stopping_args :
optional kwargs passed to the `EarlyStopper`
Returns
-------
losses : ``dict`` or ``pandas.DataFrame``
A dictionary or a data frame storing the losses across epochs and iterations
"""
# Compile update function, if specified
if use_autograph:
_backprop_step = tf.function(backprop_step, reduce_retracing=True)
else:
_backprop_step = backprop_step
# Inits
if isinstance(self.amortizer, AmortizedModelComparison):
data_set = MultiSimulationDataset(simulations_dict, batch_size)
else:
data_set = SimulationDataset(simulations_dict, batch_size)
self._setup_optimizer(optimizer, epochs, data_set.num_batches)
self.loss_history.start_new_run()
validation_sims = self._config_validation(validation_sims, **kwargs.pop("val_model_args", {}))
# Create early stopper, if conditions met, otherwise None returned
early_stopper = self._config_early_stopping(early_stopping, validation_sims, **kwargs)
# Loop through epochs
for ep in range(1, epochs + 1):
with tqdm(
total=data_set.num_batches, desc="Training epoch {}".format(ep), mininterval=TQDM_MININTERVAL
) as p_bar:
# Loop through dataset
for bi, forward_dict in enumerate(data_set, start=1):
# Perform one training step and obtain current loss value
input_dict = self.configurator(forward_dict, **kwargs.pop("conf_args", {}))
loss = self._train_step(batch_size, _backprop_step, input_dict, **kwargs)
# Store returned loss
self.loss_history.add_entry(ep, loss)
# Compute running loss
avg_dict = self.loss_history.get_running_losses(ep)
# Extract current learning rate
lr = extract_current_lr(self.optimizer)
# Format for display on progress bar
disp_str = format_loss_string(ep, bi, loss, avg_dict, lr=lr, it_str="Batch")
# Update progress bar
p_bar.set_postfix_str(disp_str, refresh=False)
p_bar.update(1)
# Store and compute validation loss, if specified
self._validation(ep, validation_sims, **kwargs)
self._save_trainer(save_checkpoint)
# Check early stopping, if specified
if self._check_early_stopping(early_stopper):
break
# Remove optimizer reference, if not set as persistent
if not reuse_optimizer:
self.optimizer = None
return self.loss_history.get_plottable()
def train_from_presimulation(
self,
presimulation_path,
optimizer,
save_checkpoint=True,
max_epochs=None,
reuse_optimizer=False,
custom_loader=None,
early_stopping=False,
validation_sims=None,
use_autograph=True,
**kwargs,
):
"""Trains an amortizer via a modified form of offline training.
Like regular offline training, it assumes that parameters, data and optional context have already
been simulated (i.e., forward inference has been performed).
Also like regular offline training, it is faster than online training in scenarios where simulations are slow.
Unlike regular offline training, it uses each batch from the presimulated dataset only once during training,
if not otherwise specified by a higher maximal number of epochs. Then, presimulated data is reused in a cyclic
manner to achieve the desired number of epochs.
A larger presimulated dataset is therefore required than for offline training, and the increase in speed
gained by loading simulations instead of generating them on the fly comes at a cost:
a large presimulated dataset takes up a large amount of hard drive space.
Parameters
----------
presimulation_path : str
File path to the folder containing the files from the precomputed simulation.
Ideally generated using a GenerativeModel's presimulate_and_save method, otherwise must match
the structure produced by that method.
Each file contains the data for one epoch (i.e. a number of batches), and must be compatible
with the custom_loader provided.
The custom_loader must read each file into a collection (either a dictionary or a list) of simulation_dict
objects.
This is easily achieved with the pickle library: if the files were generated from collections of
simulation_dict objects using pickle.dump, the _default_loader (default for custom_load) will
load them using pickle.load.
Training parameters like number of iterations and batch size are inferred from the files during training.
optimizer : tf.keras.optimizer.Optimizer
Optimizer for the neural network training. Since for this training, it is impossible to guess the number of
iterations beforehead, an optimizer must be provided.
save_checkpoint : bool, optional, default : True
Determines whether to save checkpoints after each epoch,
if a checkpoint_path provided during initialization, otherwise ignored.
max_epochs : int or None, optional, default: None
An optional parameter to limit or extend the number of epochs. If number of epochs is larger than the files
of the dataset, presimulations will be reused.
reuse_optimizer : bool, optional, default: False
A flag indicating whether the optimizer instance should be treated as persistent or not.
If ``False``, the optimizer and its states are not stored after training has finished.
Otherwise, the optimizer will be stored as ``self.optimizer`` and re-used in further training runs.
custom_loader : callable, optional, default: self._default_loader
Must take a string file_path as an input and output a collection (dictionary or list) of
simulation_dict objects. A simulation_dict has the keys ``prior_non_batchable_context``,
``prior_batchable_context``, ``prior_draws``, ``sim_non_batchable_context``, ``sim_batchable_context``, and
``sim_data``.
Here, ``prior_draws`` and ``sim_data`` must have actual data as values, the rest are optional.
early_stopping : bool, optional, default: False
Whether to use optional stopping or not during training. Could speed up training.
validation_sims : dict, int, or None, optional, default: None
Simulations used as a validation set.
If ``dict``, will assume it's the output of a generative model and try
``amortizer.compute_loss(configurator(validation_sims))``
after each epoch.
If ``int``, will assume it's the number of sims to generate from the generative
model before starting training. Only considered if a generative model has been
provided during initialization.
If ``None`` (default), no validation set will be used.
use_autograph : bool, optional, default: True
Whether to use autograph for the backprop step. Could lead to enormous speed-ups but
could also be harder to debug.
Other Parameters
----------------
conf_args :
optional keyword arguments passed to the configurator
net_args :
optional keyword arguments passed to the amortizer
Returns
-------
losses : ``dict`` or ``pandas.DataFrame``
A dictionary or a data frame storing the losses across epochs and iterations
"""
self.optimizer = optimizer
# Compile update function, if specified
if use_autograph:
_backprop_step = tf.function(backprop_step, reduce_retracing=True)
else:
_backprop_step = backprop_step
# Inits
self.loss_history.start_new_run()
validation_sims = self._config_validation(validation_sims, **kwargs.pop("val_model_args", {}))
# Create early stopper, if conditions met, otherwise None returned
early_stopper = self._config_early_stopping(early_stopping, validation_sims, **kwargs)
# Loop over the presimulated dataset.
file_list = os.listdir(presimulation_path)
# Use default loading function if none is provided
if custom_loader is None:
custom_loader = self._default_loader
# Remove non-pickle files from the list
file_list = [f for f in file_list if f.endswith(".pkl")]
if max_epochs is not None:
# Limit number of epochs to max_epochs
if len(file_list) > max_epochs:
file_list = file_list[:max_epochs]
# If the number of files is smaller than the number of epochs, repeat the files until max_epochs is reached
elif len(file_list) < max_epochs:
file_list = file_list * int(np.ceil(max_epochs / len(file_list)))
file_list = file_list[:max_epochs]
for ep, current_filename in enumerate(file_list, start=1):
# Read single file into memory as a dictionary or list
file_path = os.path.join(presimulation_path, current_filename)
epoch_data = custom_loader(file_path)
# For each epoch, the number of iterations is inferred from the presimulated dictionary or
# list used for that epoch
if isinstance(epoch_data, dict):
index_list = list(epoch_data.keys())
elif isinstance(epoch_data, list):
index_list = np.arange(len(epoch_data))
else:
raise ValueError(
f"Loading a simulation file resulted in a {type(epoch_data)}. Must be a dictionary or a list!"
)
with tqdm(total=len(index_list), desc=f"Training epoch {ep}", mininterval=TQDM_MININTERVAL) as p_bar:
for it, index in enumerate(index_list, start=1):
# Perform one training step and obtain current loss value
input_dict = self.configurator(epoch_data[index])
# Like the number of iterations, the batch size is inferred from presimulated dictionary or list
if isinstance(self.amortizer, AmortizedModelComparison):
batch_size = input_dict[DEFAULT_KEYS["summary_conditions"]].shape[0]
else:
batch_size = epoch_data[index][DEFAULT_KEYS["sim_data"]].shape[0]
loss = self._train_step(batch_size, _backprop_step, input_dict, **kwargs)
# Store returned loss
self.loss_history.add_entry(ep, loss)
# Compute running loss
avg_dict = self.loss_history.get_running_losses(ep)
# Extract current learning rate
lr = extract_current_lr(self.optimizer)
# Format for display on progress bar
disp_str = format_loss_string(ep, it, loss, avg_dict, lr=lr)
# Update progress bar
p_bar.set_postfix_str(disp_str, refresh=False)
p_bar.update(1)
# Store after each epoch, if specified
self._validation(ep, validation_sims, **kwargs)
self._save_trainer(save_checkpoint)
# Check early stopping, if specified
if self._check_early_stopping(early_stopper):
break
# Remove optimizer reference, if not set as persistent
if not reuse_optimizer:
self.optimizer = None
return self.loss_history.get_plottable()
def train_experience_replay(
self,
epochs,
iterations_per_epoch,
batch_size,
save_checkpoint=True,
optimizer=None,
reuse_optimizer=False,
buffer_capacity=1000,
early_stopping=False,
use_autograph=True,
validation_sims=None,
**kwargs,
):
"""Trains the network(s) via experience replay using a memory replay buffer, as utilized
in reinforcement learning. Additional keyword arguments are passed to the generative mode,
configurator, and amortizer. Read below for signature.
Parameters
----------
epochs : int
Number of epochs (and number of times a checkpoint is stored)
iterations_per_epoch : int
Number of batch simulations to perform per epoch
batch_size : int
Number of simulations to perform at each backpropagation step.
save_checkpoint : bool, optional, default: True
A flag to decide whether to save checkpoints after each epoch,
if a ``checkpoint_path`` provided during initialization, otherwise ignored.
optimizer : tf.keras.optimizer.Optimizer or None
Optimizer for the neural network. ``None`` will result in ``tf.keras.optimizers.Adam``
using a learning rate of 5e-4 and a cosine decay from 5e-4 to 0. A custom optimizer
will override default learning rate and schedule settings.
reuse_optimizer : bool, optional, default: False
A flag indicating whether the optimizer instance should be treated as persistent or not.
If ``False``, the optimizer and its states are not stored after training has finished.
Otherwise, the optimizer will be stored as ``self.optimizer`` and re-used in further training runs.
buffer_capacity : int, optional, default: 1000
Max number of batches to store in buffer. For instance, if ``batch_size=32``
and ``capacity_in_batches=1000``, then the buffer will hold a maximum of
32 * 1000 = 32000 simulations. Be careful with memory!
Important! Argument will be ignored if buffer has previously been initialized!
early_stopping : bool, optional, default: True
Whether to use optional stopping or not during training. Could speed up training.
Only works if ``validation_sims is not None``, i.e., validation data has been provided.
use_autograph : bool, optional, default: True
Whether to use autograph for the backprop step. Could lead to enormous speed-ups but
could also be harder to debug.
validation_sims : dict or None, optional, default: None
Simulations used as a "validation set".
If ``dict``, will assume it's the output of a generative model and try
``amortizer.compute_loss(configurator(validation_sims))``
after each epoch.
If ``int``, will assume it's the number of sims to generate from the generative
model before starting training. Only considered if a generative model has been
provided during initialization.
If ``None`` (default), no validation set will be used.
Other Parameters
----------------
model_args :
optional kwargs passed to the generative model
val_model_args :
optional kwargs passed to the generative model for generating validation data. Only useful if
``type(validation_sims) is int``.
conf_args :
optional kwargs passed to the configurator before each backprop (update) step.
val_conf_args :
optional kwargs passed to the configurator then configuring the validation data.
net_args :
optional kwargs passed to the amortizer
early_stopping_args:
optional kwargs passed to the `EarlyStopper`
Returns
-------
losses : ``dict`` or ``pandas.DataFrame``
A dictionary or a data frame storing the losses across epochs and iterations.
"""
assert self.generative_model is not None, "No generative model found. Only offline training is possible!"
# Compile update function, if specified
if use_autograph:
_backprop_step = tf.function(backprop_step, reduce_retracing=True)
else:
_backprop_step = backprop_step
# Inits
self._setup_optimizer(optimizer, epochs, iterations_per_epoch)
self.loss_history.start_new_run()
if self.replay_buffer is None:
self.replay_buffer = MemoryReplayBuffer(buffer_capacity)
validation_sims = self._config_validation(validation_sims)
# Create early stopper, if conditions met, otherwise None returned
early_stopper = self._config_early_stopping(early_stopping, validation_sims, **kwargs)
# Loop through epochs
for ep in range(1, epochs + 1):
with tqdm(total=iterations_per_epoch, desc=f"Training epoch {ep}", mininterval=TQDM_MININTERVAL) as p_bar:
for it in range(1, iterations_per_epoch + 1):
# Simulate a batch of data and store into buffer
input_dict = self._forward_inference(
batch_size, **kwargs.pop("conf_args", {}), **kwargs.pop("model_args", {})
)
self.replay_buffer.store(input_dict)
# Sample from buffer
input_dict = self.replay_buffer.sample()
# One step backprop
loss = _backprop_step(input_dict, self.amortizer, self.optimizer, **kwargs.pop("net_args", {}))
# Store returned loss
self.loss_history.add_entry(ep, loss)
# Compute running loss
avg_dict = self.loss_history.get_running_losses(ep)
# Extract current learning rate
lr = extract_current_lr(self.optimizer)
# Format for display on progress bar
disp_str = format_loss_string(ep, it, loss, avg_dict, lr=lr)
# Update progress bar
p_bar.set_postfix_str(disp_str, refresh=False)
p_bar.update(1)
# Store and compute validation loss, if specified
self._validation(ep, validation_sims, **kwargs)
self._save_trainer(save_checkpoint)
# Check early stopping, if specified
if self._check_early_stopping(early_stopper):
break
# Remove optimizer reference, if not set as persistent
if not reuse_optimizer:
self.optimizer = None
return self.loss_history.get_plottable()
def train_rounds(
self,
rounds,
sim_per_round,
epochs,
batch_size,
save_checkpoint=True,
optimizer=None,
reuse_optimizer=False,
early_stopping=False,
use_autograph=True,
validation_sims=None,
**kwargs,
):
"""Trains an amortizer via round-based learning. In each round, ``sim_per_round`` data sets
are simulated from the generative model and added to the data sets simulated in previous
round. Then, the networks are trained for ``epochs`` on the augmented set of data sets.
.. note::
Training time will increase from round to round, since the number of simulations
increases correspondingly. The final round will then train the networks on ``rounds * sim_per_round``
data sets, so make sure this number does not eat up all available memory.
Parameters
----------
rounds : int
Number of rounds to perform (outer loop)
sim_per_round : int
Number of simulations per round
epochs : int
Number of epochs (and number of times a checkpoint is stored, inner loop) within a round.
batch_size : int
Number of simulations to use at each backpropagation step
save_checkpoint : bool, optional, default: True
A flag to decide whether to save checkpoints after each epoch,
if a checkpoint_path provided during initialization, otherwise ignored.
optimizer : tf.keras.optimizer.Optimizer or None
Optimizer for the neural network training. ``None`` will result in ``tf.keras.optimizers.Adam``
using a learning rate of 5e-4 and a cosine decay from 5e-4 to 0. A custom optimizer
will override default learning rate and schedule settings.
reuse_optimizer : bool, optional, default: False
A flag indicating whether the optimizer instance should be treated as persistent or not.
If ``False``, the optimizer and its states are not stored after training has finished.
Otherwise, the optimizer will be stored as ``self.optimizer`` and re-used in further training runs.
early_stopping : bool, optional, default: False
Whether to use optional stopping or not during training. Could speed up training.
Only works if ``validation_sims is not None``, i.e., validation data has been provided.
Will be performed within rounds, not between rounds!
use_autograph : bool, optional, default: True
Whether to use autograph for the backprop step. Could lead to enormous speed-ups but
could also be harder to debug.
validation_sims : dict or None, optional, default: None
Simulations used as a "validation set".
If ``dict``, will assume it's the output of a generative model and try
``amortizer.compute_loss(configurator(validation_sims))``
after each epoch.
If ``int``, will assume it's the number of sims to generate from the generative
model before starting training. Only considered if a generative model has been
provided during initialization.
If ``None`` (default), no validation set will be used.
Other Parameters
----------------
model_args :
optional kwargs passed to the generative model
val_model_args :
optional kwargs passed to the generative model for generating validation data. Only useful if
``type(validation_sims) is int``.
conf_args :
optional kwargs passed to the configurator before each backprop (update) step.
val_conf_args :
optional kwargs passed to the configurator then configuring the validation data.
net_args :
optional kwargs passed to the amortizer
early_stopping_args :
optional kwargs passed to the `EarlyStopper`
Returns
-------
losses : ``dict`` or ``pandas.DataFrame``