/
approach_vaeac.R
2864 lines (2582 loc) · 137 KB
/
approach_vaeac.R
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
# SHAPR functions -------------------------------------------------------------------------------------------------
#' @rdname setup_approach
#'
#' @param vaeac.depth Positive integer (default is `3`). The number of hidden layers
#' in the neural networks of the masked encoder, full encoder, and decoder.
#' @param vaeac.width Positive integer (default is `32`). The number of neurons in each
#' hidden layer in the neural networks of the masked encoder, full encoder, and decoder.
#' @param vaeac.latent_dim Positive integer (default is `8`). The number of dimensions in the latent space.
#' @param vaeac.lr Positive numeric (default is `0.001`). The learning rate used in the [torch::optim_adam()] optimizer.
#' @param vaeac.activation_function An [torch::nn_module()] representing an activation function such as, e.g.,
#' [torch::nn_relu()] (default), [torch::nn_leaky_relu()], [torch::nn_selu()], or [torch::nn_sigmoid()].
#' @param vaeac.n_vaeacs_initialize Positive integer (default is `4`). The number of different vaeac models to initiate
#' in the start. Pick the best performing one after `vaeac.extra_parameters$epochs_initiation_phase`
#' epochs (default is `2`) and continue training that one.
#' @param vaeac.epochs Positive integer (default is `100`). The number of epochs to train the final vaeac model.
#' This includes `vaeac.extra_parameters$epochs_initiation_phase`, where the default is `2`.
#' @param vaeac.extra_parameters Named list with extra parameters to the `vaeac` approach. See
#' [shapr::vaeac_get_extra_para_default()] for description of possible additional parameters and their default values.
#'
#' @inheritParams default_doc_explain
#'
#' @export
#' @author Lars Henry Berge Olsen
setup_approach.vaeac <- function(internal, # add default values for vaeac here.
vaeac.depth = 3,
vaeac.width = 32,
vaeac.latent_dim = 8,
vaeac.activation_function = torch::nn_relu,
vaeac.lr = 0.001,
vaeac.n_vaeacs_initialize = 4,
vaeac.epochs = 100,
vaeac.extra_parameters = list(),
...) {
# Check that torch is installed
if (!requireNamespace("torch", quietly = TRUE)) {
stop("`torch` is not installed. Please run `install.packages('torch')`.")
}
if (!torch::torch_is_installed()) stop("`torch` is not properly installed. Please run `torch::install_torch()`.")
# Extract the objects we will use later
S <- internal$objects$S
X <- internal$objects$X
parameters <- internal$parameters
# Small printout to user
if (parameters$verbose == 2) message("Setting up the `vaeac` approach.")
# Check if we are doing a combination of approaches
combined_approaches <- length(parameters$approach) > 1
# Ensure that `parameters$vaeac.extra_parameters` is a named list
if (is.null(parameters$vaeac.extra_parameters)) parameters$vaeac.extra_parameters <- list()
if (!is.list(parameters$vaeac.extra_parameters)) stop("`vaeac.extra_parameters` must be a list.")
if (length(parameters$vaeac.extra_parameters) > 0) vaeac_check_extra_named_list(parameters$vaeac.extra_parameters)
# Ensure that all vaeac parameters are in their right location
parameters <- vaeac_update_para_locations(parameters = parameters)
# Extract the default values defined for the vaeac parameters in this function
vaeac_main_para_names <- methods::formalArgs(setup_approach.vaeac)
vaeac_main_para_names <- vaeac_main_para_names[!vaeac_main_para_names %in% c("internal", "...")]
vaeac_main_para <- mget(vaeac_main_para_names)
# Add the default extra parameter values for the non-user specified extra parameters
parameters$vaeac.extra_parameters <- utils::modifyList(vaeac_get_extra_para_default(),
parameters$vaeac.extra_parameters,
keep.null = TRUE
)
# Add the default main parameter values for the non-user specified main parameters
parameters <- utils::modifyList(vaeac_main_para, parameters, keep.null = TRUE)
# Reorder them such that the vaeac parameters are at the end of the parameters list
parameters <- c(parameters[(length(vaeac_main_para) + 1):length(parameters)], parameters[seq_along(vaeac_main_para)])
# Check if vaeac is to be applied on a subset of coalitions.
if (!parameters$exact || parameters$is_groupwise || combined_approaches) {
# We have either:
# 1) sampled `n_combinations` different subsets of coalitions (i.e., not exact),
# 2) using the coalitions which respects the groups in group Shapley values, and/or
# 3) using a combination of approaches where vaeac is only used on a subset of the coalitions.
# Here, objects$S contains the coalitions while objects$X contains the information about the approach.
# Extract the the coalitions / masks which are estimated using vaeac as a matrix
parameters$vaeac.extra_parameters$vaeac.mask_gen_coalitions <-
S[X[approach == "vaeac"]$id_combination, , drop = FALSE]
# Extract the weights for the corresponding coalitions / masks.
parameters$vaeac.extra_parameters$vaeac.mask_gen_coalitions_prob <-
X$shapley_weight[X[approach == "vaeac"]$id_combination]
# Normalize the weights/probabilities such that they sum to one.
parameters$vaeac.extra_parameters$vaeac.mask_gen_coalitions_prob <-
parameters$vaeac.extra_parameters$vaeac.mask_gen_coalitions_prob /
sum(parameters$vaeac.extra_parameters$vaeac.mask_gen_coalitions_prob)
} else {
# We are going to use the MCAR(`masking_ratio`) masking scheme. Set the variables to `NULL` as we do not need them.
parameters$vaeac.mask_gen_coalitions <- parameters$vaeac.mask_gen_coalitions_prob <- NULL
}
# Check if user provided a pre-trained vaeac model, otherwise, we train one from scratch.
if (is.null(parameters$vaeac.extra_parameters$vaeac.pretrained_vaeac_model)) {
# We train a vaeac model with the parameters in `parameters`, as user did not provide pre-trained vaeac model
if (parameters$verbose == 2) {
message(paste0(
"Training the `vaeac` model with the provided parameters from scratch on ",
ifelse(parameters$vaeac.extra_parameter$vaeac.cuda, "GPU", "CPU"), "."
))
}
# Specify that a vaeac model was NOT provided
parameters$vaeac.extra_parameters$vaeac.pretrained_vaeac_model_provided <- FALSE
# Extract all veaac parameters and remove the "vaeac." prefix as the names need to mach the parameters in "do.call"
vaeac_all_parameters <- c(
parameters$vaeac.extra_parameters,
parameters[vaeac_main_para_names[vaeac_main_para_names != "vaeac.extra_parameters"]]
)
names(vaeac_all_parameters) <- sub("vaeac\\.", "", names(vaeac_all_parameters))
vaeac_all_parameters <- c(vaeac_all_parameters, parameters[c("seed", "verbose")]) # Add seed and verbose
# Fit/train the vaeac model with the provided model parameters
vaeac_model <- do.call(vaeac_train_model, c(vaeac_all_parameters, list(x_train = internal$data$x_train)))
# Add this to the explainer object
parameters$vaeac <- list(
models = vaeac_model[1:(grep("train_vlb", names(vaeac_model)) - 1)], # Models are all entries before `train_vlb`
results = vaeac_model[c("train_vlb", "val_iwae", "val_iwae_running")], # The train & val results
parameters = vaeac_model$parameters # List of all the parameters used to train the vaeac model
)
# Add `vaeac` as a class to the object. We use this to validate the input when
# `vaeac.pretrained_vaeac_model` is given to the `shapr::explain()` function.
class(parameters$vaeac) <- c(class(parameters$vaeac), "vaeac")
} else {
# User provided a pre-trained vaeac model. (Minimal checking for valid vaeac model is conducted.)
# The pre-trained vaeac model is either:
# 1. The explanation$internal$parameters$vaeac list of type "vaeac" from an earlier call to explain().
# 2. A string containing the path to where the "vaeac" model is stored on disk.
if (parameters$verbose == 2) message("Loading the provided `vaeac` model.")
# Boolean representing that a pre-trained vaeac model was provided
parameters$vaeac.extra_parameters$vaeac.pretrained_vaeac_model_provided <- TRUE
# Check some aspects of the pre-trained vaeac model and add it to the parameters list if it passes the checks
parameters <- vaeac_update_pretrained_model(parameters = parameters)
# Small printout informing about the location of the model
if (parameters$verbose == 2) {
message(paste0(
"The `vaeac` model runs/is trained on ", ifelse(parameters$vaeac$parameters$cuda, "GPU", "CPU"), "."
))
}
}
# Get which vaeac model we are to use, load it and then store the checkpoint
checkpoint <- torch::torch_load(parameters$vaeac$models[[parameters$vaeac.extra_parameters$vaeac.which_vaeac_model]])
parameters$vaeac.checkpoint <- checkpoint
# Set up and store the vaeac model such that it is loaded before calling the `prepare_data.vaeac()` function.
parameters$vaeac.model <-
vaeac_get_model_from_checkp(checkpoint = checkpoint, cuda = checkpoint$cuda, mode_train = FALSE)
# Extract and save sampling method. That is, if we are to sample randomly from the inferred generative distributions
# or if we are to sample the most likely values (mean for cont and class with highest prob for cat features).
parameters$vaeac.sampler <- if (parameters$vaeac.extra_parameters$vaeac.sample_random) {
parameters$vaeac.model$sampler_random
} else {
parameters$vaeac.model$sampler_most_likely
}
# Update/overwrite the parameters list in the internal list.
internal$parameters <- parameters
# Small printout to user
if (parameters$verbose == 2) message("Done with setting up the `vaeac` approach.\n")
# Return the updated internal list.
return(internal)
}
#' @inheritParams default_doc
#'
#' @rdname prepare_data
#' @export
#' @author Lars Henry Berge Olsen
prepare_data.vaeac <- function(internal, index_features = NULL, ...) {
# If not provided, then set `index_features` to all non trivial coalitions
if (is.null(index_features)) index_features <- seq(2, internal$parameters$n_combinations - 1)
# Extract objects we are going to need later
S <- internal$objects$S
seed <- internal$parameters$seed
verbose <- internal$parameters$verbose
x_explain <- internal$data$x_explain
n_explain <- internal$parameters$n_explain
n_samples <- internal$parameters$n_samples
vaeac.model <- internal$parameters$vaeac.model
vaeac.sampler <- internal$parameters$vaeac.sampler
vaeac.checkpoint <- internal$parameters$vaeac.checkpoint
vaeac.batch_size_sampling <- internal$parameters$vaeac.extra_parameters$vaeac.batch_size_sampling
# Small printout to the user about which batch we are working on
if (verbose == 2) vaeac_prep_message_batch(internal = internal, index_features = index_features)
# Apply all coalitions to all explicands to get a data table where `vaeac` will impute the `NaN` values
x_explain_extended <- vaeac_get_x_explain_extended(x_explain = x_explain, S = S, index_features = index_features)
# Set the number of observations do generate the MC samples for at the time.
n_explain_extended <- nrow(x_explain_extended)
batch_size <- if (is.null(vaeac.batch_size_sampling)) n_explain_extended else vaeac.batch_size_sampling
if (batch_size > n_explain_extended) batch_size <- n_explain_extended
# Impute the missing entries using the vaeac approach.
x_explain_with_MC_samples_dt <- vaeac_impute_missing_entries(
x_explain_with_NaNs = x_explain_extended,
n_explain = n_explain,
n_samples = n_samples,
vaeac_model = vaeac.model,
checkpoint = vaeac.checkpoint,
sampler = vaeac.sampler,
batch_size = batch_size,
verbose = verbose,
seed = seed,
index_features = index_features
)
# Return the generated conditional Monte Carlo samples
return(x_explain_with_MC_samples_dt)
}
# Train functions ======================================================================================================
#' Train the Vaeac Model
#'
#' @description Function that fits a vaeac model to the given dataset based on the provided parameters,
#' as described in \href{https://www.jmlr.org/papers/volume23/21-1413/21-1413.pdf}{Olsen et al. (2022)}. Note that
#' all default parameters specified below origin from [shapr::setup_approach.vaeac()] and
#' [shapr::vaeac_get_extra_para_default()].
#'
#' @details
#' The vaeac model consists of three neural networks, i.e., a masked encoder, a full encoder, and a decoder.
#' The networks have shared `depth`, `width`, and `activation_function`. The encoders maps the `x_train`
#' to a latent representation of dimension `latent_dim`, while the decoder maps the latent representations
#' back to the feature space. See \href{https://www.jmlr.org/papers/volume23/21-1413/21-1413.pdf}{Olsen et al. (2022)}
#' for more details. The function first initiates `n_vaeacs_initialize` vaeac models with different randomly
#' initiated network parameter values to remedy poorly initiated values. After `epochs_initiation_phase` epochs, the
#' `n_vaeacs_initialize` vaeac models are compared and the function continues to only train the best performing
#' one for a total of `epochs` epochs. The networks are trained using the ADAM optimizer with the learning rate is `lr`.
#'
#' @param depth Positive integer (default is `3`). The number of hidden layers
#' in the neural networks of the masked encoder, full encoder, and decoder.
#' @param width Positive integer (default is `32`). The number of neurons in each
#' hidden layer in the neural networks of the masked encoder, full encoder, and decoder.
#' @param latent_dim Positive integer (default is `8`). The number of dimensions in the latent space.
#' @param lr Positive numeric (default is `0.001`). The learning rate used in the [torch::optim_adam()] optimizer.
#' @param activation_function An [torch::nn_module()] representing an activation function such as, e.g.,
#' [torch::nn_relu()] (default), [torch::nn_leaky_relu()], [torch::nn_selu()], or [torch::nn_sigmoid()].
#' @param n_vaeacs_initialize Positive integer (default is `4`). The number of different vaeac models to initiate
#' in the start. Pick the best performing one after `epochs_initiation_phase`
#' epochs (default is `2`) and continue training that one.
#' @param epochs Positive integer (default is `100`). The number of epochs to train the final vaeac model.
#' This includes `epochs_initiation_phase`, where the default is `2`.
#' @param x_train A data.table containing the training data. Categorical data must have class names \eqn{1,2,\dots,K}.
#' @param model_description String (default is `make.names(Sys.time())`). String containing, e.g., the name of the
#' data distribution or additional parameter information. Used in the save name of the fitted model. If not provided,
#' then a name will be generated based on [base::Sys.time()] to ensure a unique name. We use [base::make.names()] to
#' ensure a valid file name for all operating systems.
#' @param folder_to_save_model String (default is [base::tempdir()]). String specifying a path to a folder where
#' the function is to save the fitted vaeac model. Note that the path will be removed from the returned
#' [shapr::explain()] object if `vaeac.save_model = FALSE`.
#' @param cuda Logical (default is `FALSE`). If `TRUE`, then the `vaeac` model will be trained using cuda/GPU.
#' If [torch::cuda_is_available()] is `FALSE`, the we fall back to use CPU. If `FALSE`, we use the CPU. Using a GPU
#' for smaller tabular dataset often do not improve the efficiency.
#' See \code{vignette("installation", package = "torch")} fo help to enable running on the GPU (only Linux and Windows).
#' @param epochs_initiation_phase Positive integer (default is `2`). The number of epochs to run each of the
#' `n_vaeacs_initialize` `vaeac` models before continuing to train only the best performing model.
#' @param epochs_early_stopping Positive integer (default is `NULL`). The training stops if there has been no
#' improvement in the validation IWAE for `epochs_early_stopping` epochs. If the user wants the training process
#' to be solely based on this training criterion, then `epochs` in [shapr::explain()] should be set to a large
#' number. If `NULL`, then `shapr` will internally set `epochs_early_stopping = vaeac.epochs` such that early
#' stopping does not occur.
#' @param save_every_nth_epoch Positive integer (default is `NULL`). If provided, then the vaeac model after
#' every `save_every_nth_epoch`th epoch will be saved.
#' @param val_ratio Numeric (default is `0.25`). Scalar between `0` and `1` indicating the ratio of
#' instances from the input data which will be used as validation data. That is, `val_ratio = 0.25` means
#' that `75%` of the provided data is used as training data, while the remaining `25%` is used as validation data.
#' @param val_iwae_n_samples Positive integer (default is `25`). The number of generated samples used
#' to compute the IWAE criterion when validating the vaeac model on the validation data.
#' @param batch_size Positive integer (default is `64`). The number of samples to include in each batch
#' during the training of the vaeac model. Used in [torch::dataloader()].
#' @param skip_conn_layer Logical (default is `TRUE`). If `TRUE`, we apply identity skip connections in each
#' layer, see [shapr::skip_connection()]. That is, we add the input \eqn{X} to the outcome of each hidden layer,
#' so the output becomes \eqn{X + activation(WX + b)}.
#' @param skip_conn_masked_enc_dec Logical (default is `TRUE`). If `TRUE`, we apply concatenate skip
#' connections between the layers in the masked encoder and decoder. The first layer of the masked encoder will be
#' linked to the last layer of the decoder. The second layer of the masked encoder will be
#' linked to the second to last layer of the decoder, and so on.
#' @param batch_normalization Logical (default is `FALSE`). If `TRUE`, we apply batch normalization after the
#' activation function. Note that if `skip_conn_layer = TRUE`, then the normalization is applied after the
#' inclusion of the skip connection. That is, we batch normalize the whole quantity \eqn{X + activation(WX + b)}.
#' @param paired_sampling Logical (default is `TRUE`). If `TRUE`, we apply paired sampling to the training
#' batches. That is, the training observations in each batch will be duplicated, where the first instance will be masked
#' by \eqn{S} while the second instance will be masked by \eqn{\bar{S}}. This ensures that the training of the
#' `vaeac` model becomes more stable as the model has access to the full version of each training observation. However,
#' this will increase the training time due to more complex implementation and doubling the size of each batch. See
#' [shapr::paired_sampler()] for more information.
#' @param running_avg_n_values running_avg_n_values Positive integer (default is `5`).
#' The number of previous IWAE values to include
#' when we compute the running means of the IWAE criterion.
#' @param masking_ratio Numeric (default is `0.5`). Probability of masking a feature in the
#' [shapr::mcar_mask_generator()] (MCAR = Missing Completely At Random). The MCAR masking scheme ensures that `vaeac`
#' model can do arbitrary conditioning as all coalitions will be trained. `masking_ratio` will be overruled if
#' `mask_gen_coalitions` is specified.
#' @param mask_gen_coalitions Matrix (default is `NULL`). Matrix containing the coalitions that the
#' `vaeac` model will be trained on, see [shapr::specified_masks_mask_generator()]. This parameter is used internally
#' in `shapr` when we only consider a subset of coalitions/combinations, i.e., when
#' `n_combinations` \eqn{< 2^{n_{\text{features}}}}, and for group Shapley, i.e.,
#' when `group` is specified in [shapr::explain()].
#' @param mask_gen_coalitions_prob Numeric array (default is `NULL`). Array of length equal to the height
#' of `mask_gen_coalitions` containing the probabilities of sampling the corresponding coalitions in
#' `mask_gen_coalitions`.
#' @param sigma_mu Numeric (default is `1e4`). One of two hyperparameter values in the normal-gamma prior
#' used in the masked encoder, see Section 3.3.1 in
#' \href{https://www.jmlr.org/papers/volume23/21-1413/21-1413.pdf}{Olsen et al. (2022)}.
#' @param sigma_sigma Numeric (default is `1e-4`). One of two hyperparameter values in the normal-gamma prior
#' used in the masked encoder, see Section 3.3.1 in
#' \href{https://www.jmlr.org/papers/volume23/21-1413/21-1413.pdf}{Olsen et al. (2022)}.
#' @param save_data Logical (default is `FALSE`). If `TRUE`, then the data is stored together with
#' the model. Useful if one are to continue to train the model later using [shapr::vaeac_train_model_continue()].
#' @param log_exp_cont_feat Logical (default is `FALSE`). If we are to \eqn{\log} transform all
#' continuous features before sending the data to [shapr::vaeac()]. The `vaeac` model creates unbounded Monte Carlo
#' sample values. Thus, if the continuous features are strictly positive (as for, e.g., the Burr distribution and
#' Abalone data set), it can be advantageous to \eqn{\log} transform the data to unbounded form before using `vaeac`.
#' If `TRUE`, then [shapr::vaeac_postprocess_data()] will take the \eqn{\exp} of the results to get back to strictly
#' positive values when using the `vaeac` model to impute missing values/generate the Monte Carlo samples.
#' @param verbose Boolean. An integer specifying the level of verbosity. Use `0` (default) for no verbosity,
#' `1` for low verbose, and `2` for high verbose.
#' @param seed Positive integer (default is `1`). Seed for reproducibility. Specifies the seed before any randomness
#' based code is being run.
#' @param which_vaeac_model String (default is `best`). The name of the `vaeac` model (snapshots from different
#' epochs) to use when generating the Monte Carlo samples. The standard choices are: `"best"` (epoch with lowest IWAE),
#' `"best_running"` (epoch with lowest running IWAE, see `vaeac.running_avg_n_values`), and `last` (the last epoch).
#' Note that additional choices are available if `vaeac.save_every_nth_epoch` is provided. For example, if
#' `vaeac.save_every_nth_epoch = 5`, then `vaeac.which_vaeac_model` can also take the values `"epoch_5"`, `"epoch_10"`,
#' `"epoch_15"`, and so on.
#' @param ... List of extra parameters, currently not used.
#'
#' @return A list containing the training/validation errors and paths to where the vaeac models are saved on the disk.
#' @export
#' @author Lars Henry Berge Olsen
vaeac_train_model <- function(x_train,
model_description,
folder_to_save_model,
cuda,
n_vaeacs_initialize,
epochs_initiation_phase,
epochs,
epochs_early_stopping,
save_every_nth_epoch,
val_ratio,
val_iwae_n_samples,
depth,
width,
latent_dim,
lr,
batch_size,
running_avg_n_values,
activation_function,
skip_conn_layer,
skip_conn_masked_enc_dec,
batch_normalization,
paired_sampling,
masking_ratio,
mask_gen_coalitions,
mask_gen_coalitions_prob,
sigma_mu,
sigma_sigma,
save_data,
log_exp_cont_feat,
which_vaeac_model,
verbose,
seed,
...) {
# Set seed for reproducibility for both R and torch
set.seed(seed)
torch::torch_manual_seed(seed)
# Set epochs_early_stopping to epochs to ensure that early stopping never occurs
if (is.null(epochs_early_stopping)) epochs_early_stopping <- epochs
# Check all the vaeac parameters
do.call(vaeac_check_parameters, mget(methods::formalArgs(vaeac_train_model)))
# Check if we can use cuda
if (cuda) cuda <- vaeac_check_cuda(cuda)
# Determine which mask generator to use
mask_generator_name <- vaeac_get_mask_generator_name(
mask_gen_coalitions = mask_gen_coalitions,
mask_gen_coalitions_prob = mask_gen_coalitions_prob,
masking_ratio = masking_ratio,
verbose = verbose
)
# Set up the data loaders and get the save file names and load them into the local environment
list2env(
vaeac_get_data_objects(
x_train = x_train,
log_exp_cont_feat = log_exp_cont_feat,
val_ratio = val_ratio,
batch_size = batch_size,
paired_sampling = paired_sampling,
model_description = model_description,
depth = depth,
width = width,
latent_dim = latent_dim,
lr = lr,
epochs = epochs,
save_every_nth_epoch = save_every_nth_epoch,
folder_to_save_model = folder_to_save_model,
train_indices = NULL,
val_indices = NULL
),
envir = environment()
)
# Get information saved together with the vaeac model to make it possible to load the model from disk later.
# Note that some of the parameters could be derived from others, but for simplicity we store all needed objects.
state_list <- vaeac_get_full_state_list(environment())
# Check if we are to add the training data to the state list
if (save_data) state_list <- c(state_list, list(x_train = x_train, x_train_torch = x_train_torch))
## Initializing vaeac models
# Initialize several vaeac models and keep the one with the best training variational lower bound
# after a given number of epochs. Keep the version with highest vlb, denoted by "best_vlb".
best_vlb <- -Inf
# Create a `progressr::progressor()` to keep track of the overall training time of the vaeac approach
if (requireNamespace("progressr", quietly = TRUE)) {
progressr_bar <- progressr::progressor(steps = epochs_initiation_phase * (n_vaeacs_initialize - 1) + epochs)
} else {
progressr_bar <- NULL
}
# Iterate over the initializations.
initialization_idx <- 1
for (initialization_idx in seq(n_vaeacs_initialize)) {
# Initialize a new vaeac model
vaeac_model <- vaeac(
one_hot_max_sizes = one_hot_max_sizes,
width = width,
depth = depth,
latent_dim = latent_dim,
activation_function = activation_function,
skip_conn_layer = skip_conn_layer,
skip_conn_masked_enc_dec = skip_conn_masked_enc_dec,
batch_normalization = batch_normalization,
paired_sampling = paired_sampling,
mask_generator_name = mask_generator_name,
masking_ratio = masking_ratio,
mask_gen_coalitions = mask_gen_coalitions,
mask_gen_coalitions_prob = mask_gen_coalitions_prob,
sigma_mu = sigma_mu,
sigma_sigma = sigma_sigma
)
# Send the model to the GPU, if we have access to it and user wants to
if (cuda) vaeac_model$cuda()
# Add the number of trainable parameters in the vaeac model to the state list
if (initialization_idx == 1) {
state_list$n_trainable_parameters <- vaeac_model$n_train_param
if (verbose == 2) {
message(paste0("The vaeac model contains ", vaeac_model$n_train_param[1, 1], " trainable parameters."))
}
}
# Print which initialization vaeac the function is working on
if (verbose == 2) {
message(paste0("Initializing vaeac number ", initialization_idx, " of ", n_vaeacs_initialize, "."))
}
# Create the ADAM optimizer
optimizer <- vaeac_get_optimizer(vaeac_model = vaeac_model, lr = lr, optimizer_name = "adam")
# Train the current initialized vaeac model
vaeac_model_now_list <- vaeac_train_model_auxiliary(
vaeac_model = vaeac_model,
optimizer = optimizer,
epochs = epochs_initiation_phase,
epochs_start = 1, # All the vaeacs should start from scratch
train_dataloader = train_dataloader,
val_dataloader = val_dataloader,
val_iwae_n_samples = val_iwae_n_samples,
running_avg_n_values = running_avg_n_values,
epochs_early_stopping = FALSE, # Do not want to do early stopping during initialization
verbose = verbose,
cuda = cuda,
progressr_bar = progressr_bar,
save_every_nth_epoch = save_every_nth_epoch,
initialization_idx = initialization_idx,
n_vaeacs_initialize = n_vaeacs_initialize,
train_vlb = NULL, # We start from scratch
val_iwae = NULL, # We start from scratch
val_iwae_running = NULL # We start from scratch
)
# If the new initialization have lower training VLB than previous initializations, then we keep it.
if ((best_vlb <= vaeac_model_now_list$avg_vlb)$item()) {
vaeac_model_best_list <- vaeac_model_now_list
}
} # Done with initial training of all vaeac models
# Check if we are printing detailed debug information
# Small printout to the user stating which initiated vaeac model was the best.
if (verbose == 2) {
message(paste0(
"Best vaeac inititalization was number ", vaeac_model_best_list$initialization_idx, " (of ", n_vaeacs_initialize,
") with a training VLB = ", round(as.numeric(vaeac_model_best_list$train_vlb[-1]$cpu()), 3),
" after ", epochs_initiation_phase, " epochs. Continue to train this inititalization."
))
}
return_list <- vaeac_train_model_auxiliary(
vaeac_model = vaeac_model_best_list$vaeac_model,
optimizer = vaeac_model_best_list$optimizer,
train_dataloader = train_dataloader,
val_dataloader = val_dataloader,
val_iwae_n_samples = val_iwae_n_samples,
running_avg_n_values = running_avg_n_values,
verbose = verbose,
cuda = cuda,
progressr_bar = progressr_bar,
epochs = epochs,
epochs_start = epochs_initiation_phase + 1,
epochs_early_stopping = epochs_early_stopping,
save_every_nth_epoch = save_every_nth_epoch,
vaeac_save_file_names = vaeac_save_file_names, # Provide the save names for the models
state_list = state_list, # Need to provide the state list as it will be saved together with the models
initialization_idx = NULL, # Do not need to specify it as we are not doing the initialization now
n_vaeacs_initialize = NULL, # Do not need to specify it as we are not doing the initialization now
train_vlb = vaeac_model_best_list$train_vlb, # Send in the array from the best initiated vaeac model
val_iwae = vaeac_model_best_list$val_iwae,
val_iwae_running = vaeac_model_best_list$val_iwae_running
)
# Return the paths where the models are saved and the training/validation errors.
return(return_list)
}
#' Function used to train a `vaeac` model
#'
#' @description
#' This function can be applied both in the initialization phase when, we train several initiated `vaeac` models, and
#' to keep training the best performing `vaeac` model for the remaining number of epochs. We are in the former setting
#' when `initialization_idx` is provided and the latter when it is `NULL`. When it is `NULL`, we save the `vaeac` models
#' with lowest VLB, IWAE, running IWAE, and the epochs according to `save_every_nth_epoch` to disk.
#'
#' @inheritParams vaeac_train_model
#' @param vaeac_model A [shapr::vaeac()] object. The `vaeac` model this function is to train.
#' @param optimizer A [torch::optimizer()] object. See [shapr::vaeac_get_optimizer()].
#' @param train_dataloader A [torch::dataloader()] containing the training data for the `vaeac` model.
#' @param val_dataloader A [torch::dataloader()] containing the validation data for the `vaeac` model.
#' @param train_vlb A [torch::torch_tensor()] (default is `NULL`)
#' of one dimension containing previous values for the training VLB.
#' @param val_iwae A [torch::torch_tensor()] (default is `NULL`)
#' of one dimension containing previous values for the validation IWAE.
#' @param val_iwae_running A [torch::torch_tensor()] (default is `NULL`)
#' of one dimension containing previous values for the running validation IWAE.
#' @param progressr_bar A [progressr::progressor()] object (default is `NULL`) to keep track of progress.
#' @param epochs_start Positive integer (default is `1`). At which epoch the training is starting at.
#' @param vaeac_save_file_names Array of strings containing the save file names for the `vaeac` model.
#' @param state_list Named list containing the objects returned from [shapr::vaeac_get_full_state_list()].
#' @param initialization_idx Positive integer (default is `NULL`). The index
#' of the current `vaeac` model in the initialization phase.
#'
#' @return Depending on if we are in the initialization phase or not. Then either the trained `vaeac` model, or
#' a list of where the `vaeac` models are stored on disk and the parameters of the model.
#' @keywords internal
#' @author Lars Henry Berge Olsen
vaeac_train_model_auxiliary <- function(vaeac_model,
optimizer,
train_dataloader,
val_dataloader,
val_iwae_n_samples,
running_avg_n_values,
verbose,
cuda,
epochs,
save_every_nth_epoch,
epochs_early_stopping,
epochs_start = 1,
progressr_bar = NULL,
vaeac_save_file_names = NULL,
state_list = NULL,
initialization_idx = NULL,
n_vaeacs_initialize = NULL,
train_vlb = NULL,
val_iwae = NULL,
val_iwae_running = NULL) {
# Check for valid input
if (xor(is.null(initialization_idx), is.null(n_vaeacs_initialize))) {
stop("Either none or both of `initialization_idx` and `n_vaeacs_initialize` must be given.")
}
if (is.null(state_list) && is.null(initialization_idx)) {
stop("`state_list` must be provide when `initialization_idx = NULL` to properly save the `vaeac` model.")
}
if (is.null(vaeac_save_file_names) && is.null(initialization_idx)) {
stop(paste0(
"`vaeac_save_file_names` must be provide when `initialization_idx = NULL` ",
"to know where to save the vaeac model."
))
}
if (!((is.null(train_vlb) && is.null(val_iwae) && is.null(val_iwae_running)) ||
(!is.null(train_vlb) && !is.null(val_iwae) && !is.null(val_iwae_running)))) {
stop("Either none or all of `train_vlb`, `val_iwae`, and `val_iwae_running` must be given.")
}
# Variable that we change to `TRUE` if early stopping is applied
if (!is.null(state_list)) state_list$early_stopping_applied <- FALSE
# Variable to store the epochs of the `vaeac` at the best epoch according to IWAE and IWAE_running
if (is.null(initialization_idx)) best_epoch <- best_epoch_running <- NULL
# Get the batch size
batch_size <- train_dataloader$batch_size
# Extract the mask generator and the variational lower bound scale factor from the vaeac model object.
mask_generator <- vaeac_model$mask_generator
vlb_scale_factor <- vaeac_model$vlb_scale_factor
# Start the training loop
for (epoch in seq(from = epochs_start, to = epochs)) {
avg_vlb <- 0 # Set average variational lower bound to 0 for this epoch
batch_index <- 1 # Index to keep track of which batch we are working on
# Iterate over the training data
coro::loop(for (batch in train_dataloader) {
# If batch size is less than batch_size, extend it with objects from the beginning of the dataset
if (batch$shape[1] < batch_size) {
batch <- vaeac_extend_batch(batch = batch, dataloader = train_dataloader, batch_size = batch_size)
}
# Generate mask and do an optimizer step over the mask and the batch
mask <- mask_generator(batch)
# Send the batch and mask to GPU if we have access to it and user wants to
if (cuda) {
batch <- batch$cuda()
mask <- mask$cuda()
}
# Set all previous gradients to zero.
optimizer$zero_grad()
# Compute the variational lower bound for the batch given the mask
vlb <- vaeac_model$batch_vlb(batch, mask)$mean()
# Backpropagation: minimize the negative vlb.
vlb_loss <- (-vlb / vlb_scale_factor)
vlb_loss$backward()
# Update the vaeac_model parameters by using the optimizer
optimizer$step()
# Update running variational lower bound average using the recursive average formula/update.
# a + (new - a)/(i+1) = {(i+1)a + new - a}/(i+1) = { a(i) + new}/(i+1) = a *i/(i+1) + new/(i+1)
avg_vlb <- avg_vlb + (vlb$to(dtype = torch::torch_float())$clone()$detach() - avg_vlb) / batch_index
# Update the batch index.
batch_index <- batch_index + 1
}) # Done with one new epoch
## Time to evaluate the vaeac_model on the validation data, potentially save it, and check for early stopping.
# Store the VLB
train_vlb <- torch::torch_cat(c(train_vlb, avg_vlb), -1)
# Compute the validation IWAE
val_iwae_now <- vaeac_get_val_iwae(
val_dataloader = val_dataloader,
mask_generator = mask_generator,
batch_size = batch_size,
vaeac_model = vaeac_model,
val_iwae_n_samples = val_iwae_n_samples
)
val_iwae <- torch::torch_cat(c(val_iwae, val_iwae_now), -1)
# Compute the running validation IWAE
val_iwae_running_now <-
val_iwae[
(-min(length(val_iwae), running_avg_n_values) +
length(val_iwae) + 1):(-1 + length(val_iwae) + 1),
drop = FALSE
]$mean()$view(1)
val_iwae_running <- torch::torch_cat(c(val_iwae_running, val_iwae_running_now), -1)
# Check if we are to save the models
if (is.null(initialization_idx)) {
# Save if current vaeac model has the lowest validation IWAE error
if ((max(val_iwae) <= val_iwae_now)$item() || is.null(best_epoch)) {
best_epoch <- epoch
if (verbose == 2) message("Saving `best` vaeac model at epoch ", epoch, ".")
vaeac_save_state(state_list = state_list, file_name = vaeac_save_file_names[1])
}
# Save if current vaeac model has the lowest running validation IWAE error
if ((max(val_iwae_running) <= val_iwae_running_now)$item() || is.null(best_epoch_running)) {
best_epoch_running <- epoch
if (verbose == 2) message("Saving `best_running` vaeac model at epoch ", epoch, ".")
vaeac_save_state(state_list = state_list, file_name = vaeac_save_file_names[2])
}
# Save if we are in an n'th epoch and are to save every n'th epoch
if (is.numeric(save_every_nth_epoch) && epoch %% save_every_nth_epoch == 0) {
if (verbose == 2) message("Saving `nth_epoch` vaeac model at epoch ", epoch, ".")
vaeac_save_state(state_list = state_list, file_name = vaeac_save_file_names[3 + epoch %/% save_every_nth_epoch])
}
}
# Handle the message to the progress bar based on if we are doing initialization or final training
if (!is.null(progressr_bar)) {
update_message <- if (!is.null(initialization_idx)) {
paste0(
"Training vaeac (init. ", initialization_idx, " of ", n_vaeacs_initialize, "): Epoch: ", epoch,
" | VLB: ", vaeac_get_n_decimals(avg_vlb$item()), " | IWAE: ", vaeac_get_n_decimals(val_iwae_now$item()), " |"
)
} else {
paste0(
"Training vaeac (final model): Epoch: ", epoch, " | best epoch: ", best_epoch,
" | VLB: ", vaeac_get_n_decimals(avg_vlb$item()), " | IWAE: ", vaeac_get_n_decimals(val_iwae_now$item()), " |"
)
}
progressr_bar(message = update_message)
}
# Check if we are to apply early stopping, i.e., no improvement in the IWAE for `epochs_early_stopping` epochs.
if (is.numeric(epochs_early_stopping)) {
if (epoch - best_epoch >= epochs_early_stopping) {
if (verbose == 2) {
message(paste0(
"No IWAE improvment in ", epochs_early_stopping, " epochs. Apply early stopping at epoch ",
epoch, "."
))
}
if (!is.null(progressr_bar)) progressr_bar("Training vaeac (early stopping)", amount = epochs - epoch)
state_list$early_stopping_applied <- TRUE # Add that we did early stopping to the state list
state_list$epochs <- epoch # Update the number of used epochs.
break # Stop the training loop
}
}
} # Done with all epochs in training phase
# Find out what to return
if (!is.null(initialization_idx)) {
# Here we return the models and the optimizer which we will train further if this was the best initialization
return_list <- list(
vaeac_model = vaeac_model,
optimizer = optimizer,
train_vlb = train_vlb,
val_iwae = val_iwae,
val_iwae_running = val_iwae_running,
avg_vlb = avg_vlb,
initialization_idx = initialization_idx,
state_list = state_list
)
} else {
# Save the vaeac model at the last epoch
if (verbose == 2) message("Saving `last` vaeac model at epoch ", epoch, ".")
last_state <- vaeac_save_state(state_list = state_list, file_name = vaeac_save_file_names[3], return_state = TRUE)
# Summary printout
if (verbose == 2) vaeac_print_train_summary(best_epoch, best_epoch_running, last_state)
# Create a return list
return_list <- list(
best = vaeac_save_file_names[1],
best_running = vaeac_save_file_names[2],
last = vaeac_save_file_names[3],
train_vlb = as.array(train_vlb$cpu()),
val_iwae = as.array(val_iwae$cpu()),
val_iwae_running = as.array(val_iwae_running$cpu()),
parameters = last_state
)
# Add the potentially additional save names
if (!is.null(vaeac_save_file_names) && length(vaeac_save_file_names) > 3) {
return_list <- append(
return_list,
setNames(
as.list(vaeac_save_file_names[-(1:3)]),
paste0("epoch_", save_every_nth_epoch * seq(length(vaeac_save_file_names) - 3))
),
3
)
}
# Update the class of the returned object
attr(return_list, "class") <- c("vaeac", class(return_list))
}
return(return_list)
}
#' Continue to Train the vaeac Model
#'
#' @description Function that loads a previously trained vaeac model and continue the training, either
#' on new data or on the same dataset as it was trained on before. If we are given a new dataset, then
#' we assume that new dataset has the same distribution and one_hot_max_sizes as the original dataset.
#'
#' @inheritParams vaeac_train_model
#' @param explanation A [shapr::explain()] object and `vaeac` must be the used approach.
#' @param epochs_new Positive integer. The number of extra epochs to conduct.
#' @param lr_new Positive numeric. If we are to overwrite the old learning rate in the adam optimizer.
#'
#' @return A list containing the training/validation errors and paths to where the vaeac models are saved on the disk.
#' @export
#' @author Lars Henry Berge Olsen
vaeac_train_model_continue <- function(explanation,
epochs_new,
lr_new = NULL,
x_train = NULL,
save_data = FALSE,
verbose = 0,
seed = 1) {
# Check the input
if (!"shapr" %in% class(explanation)) stop("`explanation` must be a list of class `shapr`.")
if (!"vaeac" %in% explanation$internal$parameters$approach) stop("`vaeac` is not an approach in `explanation`.")
if (!is.null(lr_new)) vaeac_check_positive_numerics(list(lr_new = lr_new))
if (!is.null(x_train) && !data.table::is.data.table(x_train)) stop("`x_train` must be a `data.table` object.")
vaeac_check_verbose(verbose)
vaeac_check_positive_integers(list(epochs_new = epochs_new, seed = seed))
vaeac_check_logicals(list(save_data = save_data))
# Set seed for reproducibility
set.seed(seed)
# Extract the vaeac list and load the model at the last epoch or the best (default 'best' when path is provided)
vaeac_model <- explanation$internal$parameters$vaeac
vaeac_model_path <- if (!is.null(vaeac_model$models$last)) vaeac_model$models$last else vaeac_model$models$best
checkpoint <- torch::torch_load(vaeac_model_path)
# Get which device we are to continue to train the model
device <- ifelse(checkpoint$cuda, "cuda", "cpu")
# If we applied early stopping before and are calling this function, then we turn early stopping off
if (isTRUE(checkpoint$early_stopping_applied)) checkpoint$epochs_early_stopping <- epochs_new
# Check for access to a single training data set and use the data from the checkpoint if `x_train` is not provided
if (is.null(checkpoint$normalized_data) && is.null(x_train)) {
stop("The `vaeac` model did not include data (set `vaeac.save_data = TRUE in `explain()`) and `x_train = NULL`.")
}
if (!is.null(checkpoint$x_train) && !is.null(x_train)) {
message("The `vaeac` model includes data and `x_train` was provided to this function. We only use `x_train`.")
}
if (is.null(x_train)) x_train <- checkpoint$x_train
# Check that the provided vaeac model is trained on a dataset with the same feature names
vaeac_check_x_colnames(feature_names_vaeac = checkpoint$feature_list$labels, feature_names_new = names(x_train))
# Check if we can reuse the original validation and training indices
if (!is.null(checkpoint$x_train) || nrow(x_train) == checkpoint$n_train) {
val_indices <- checkpoint$val_indices
train_indices <- checkpoint$train_indices
} else {
val_indices <- train_indices <- NULL
}
# Set up the data loaders and get the save file names and load them into the local environment
list2env(
vaeac_get_data_objects(
x_train = x_train,
log_exp_cont_feat = checkpoint$log_exp_cont_feat,
val_ratio = checkpoint$val_ratio,
batch_size = checkpoint$batch_size,
paired_sampling = checkpoint$paired_sampling,
model_description = checkpoint$ model_description,
depth = checkpoint$depth,
width = checkpoint$width,
latent_dim = checkpoint$latent_dim,
lr = checkpoint$lr, # Use the old one as this parameter is used in the filenames
epochs = checkpoint$epochs + epochs_new,
save_every_nth_epoch = checkpoint$save_every_nth_epoch,
folder_to_save_model = checkpoint$folder_to_save_model,
train_indices = train_indices,
val_indices = val_indices
),
envir = environment()
)
# List to values saved to disk together with the vaeac models below.
state_list_new <- list(
norm_mean = as.array(x_train_preprocessed$norm_mean),
norm_std = as.array(x_train_preprocessed$norm_std),
n_train = n_train,
epochs_new = epochs_new,
train_indices = train_indices,
val_indices = val_indices,
lr_new = lr_new
)
# If we are also to save the data to state_list.
if (save_data) {
state_list_new <- c(state_list_new, list(x_train = x_train, x_train_torch = x_train_torch))
# Give a message regarding disk usage
vaeac_check_save_parameters(
save_data = save_data,
epochs = epochs_new,
save_every_nth_epoch = checkpoint$save_every_nth_epoch,
x_train_size = format(utils::object.size(x_train), units = "auto")
)
}
# Add the new state list as a list to the checkpoint
n_times_continued_trained <- sum(grepl("state_list_new", names(checkpoint)))
state_list_new_name <- paste("state_list_new", n_times_continued_trained + 1, sep = "_")
state_list <- checkpoint
state_list[[state_list_new_name]] <- state_list_new
# Set up the vaeac model in training mode and based on the parameters stored in the checkpoint
vaeac_model <- vaeac_get_model_from_checkp(checkpoint = checkpoint, cuda = checkpoint$cuda, mode_train = TRUE)
# Send the loaded optimizer parameters to GPU if necessary
if (checkpoint$cuda) {
checkpoint$optimizer_state_dict$state <- lapply(
checkpoint$optimizer_state_dict$state,
function(x) lapply(x, function(y) if ("torch_tensor" %in% class(y)) y$cuda() else y)
)
}
# Specify the learning rate we will use, create the an adam optimizer, and insert the stored optimizer state.
lr_now <- if (!is.null(lr_new)) lr_new else checkpoint$lr
optimizer <- vaeac_get_optimizer(vaeac_model = vaeac_model, lr = lr_now, optimizer_name = "adam")
optimizer$load_state_dict(checkpoint$optimizer_state_dict)
# Compute the new number of epochs
epochs_old <- checkpoint$epochs
epochs <- epochs_old + epochs_new
state_list$epochs <- epochs
# Create a `progressr::progressor()` to keep track of the new training
if (requireNamespace("progressr", quietly = TRUE)) {
progressr_bar <- progressr::progressor(steps = epochs_new)
} else {
progressr_bar <- NULL
}
# Train the vaeac model for `epochs_new` number of epochs
vaeac_tmp <- vaeac_train_model_auxiliary(
vaeac_model = vaeac_model,
optimizer = optimizer,
train_dataloader = train_dataloader,
val_dataloader = val_dataloader,
val_iwae_n_samples = checkpoint$val_iwae_n_samples,
running_avg_n_values = checkpoint$running_avg_n_values,
verbose = verbose,
cuda = checkpoint$cuda,
progressr_bar = progressr_bar,
epochs = epochs,
epochs_start = epochs_old + 1,
epochs_early_stopping = checkpoint$epochs_early_stopping,
save_every_nth_epoch = checkpoint$save_every_nth_epoch,
vaeac_save_file_names = vaeac_save_file_names, # Provide the save names for the models
state_list = state_list, # Need to provide the state list as it will be saved together with the models
initialization_idx = NULL, # Do not need to specify it as we are not doing the initialization now
n_vaeacs_initialize = NULL, # Do not need to specify it as we are not doing the initialization now
train_vlb = checkpoint$train_vlb$to(device = device), # Send to correct device such that we can append new values
val_iwae = checkpoint$val_iwae$to(device = device),
val_iwae_running = checkpoint$val_iwae_running$to(device = device)
)
# Create the return list
return_list <- list(
models = vaeac_tmp[1:(grep("train_vlb", names(vaeac_tmp)) - 1)], # Models are all entries before `train_vlb`
results = vaeac_tmp[c("train_vlb", "val_iwae", "val_iwae_running")], # The train & val results
parameters = vaeac_tmp$parameters # List of all the parameters used to train the vaeac model
)
# Add `vaeac` as a class to the object. We use this to validate the input when
# `vaeac.pretrained_vaeac_model` is given to the `shapr::explain()` function.
class(return_list) <- c(class(return_list), "vaeac")
# Return the paths where the models are saved and the training/validation errors.
return(return_list)
}
# Imputation functions =================================================================================================
#' Impute Missing Values Using Vaeac
#'
#' @details Function that imputes the missing values in 2D matrix where each row constitute an individual.
#' The values are sampled from the conditional distribution estimated by a vaeac model.
#'
#' @inheritParams vaeac_train_model
#' @param x_explain_with_NaNs A 2D matrix, where the missing entries to impute are represented by `NaN`.