-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmlp-fit.R
More file actions
1431 lines (1317 loc) · 39.3 KB
/
mlp-fit.R
File metadata and controls
1431 lines (1317 loc) · 39.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
#' Fit neural networks
#'
#' `brulee_mlp()` fits neural network models. Multiple layers can be used. For
#' working with two-layer networks in tidymodels, `brulee_mlp_two_layer()` can
#' be helpful for specifying tuning parameters as scalars.
#'
#' @param x Depending on the context:
#'
#' * A __data frame__ of predictors.
#' * A __matrix__ of predictors.
#' * A __recipe__ specifying a set of preprocessing steps
#' created from [recipes::recipe()].
#'
#' The predictor data should be standardized (e.g. centered or scaled).
#'
#' @param y When `x` is a __data frame__ or __matrix__, `y` is the outcome
#' specified as:
#'
#' * A __data frame__ with 1 column (numeric or factor).
#' * A __matrix__ with numeric column (numeric or factor).
#' * A __vector__ (numeric or factor).
#'
#' @param data When a __recipe__ or __formula__ is used, `data` is specified as:
#'
#' * A __data frame__ containing both the predictors and the outcome.
#'
#' @param formula A formula specifying the outcome term(s) on the left-hand side,
#' and the predictor term(s) on the right-hand side.
#' @param epochs An integer for the number of epochs of training.
#' @param penalty The amount of weight decay (i.e., L2 regularization).
#' @param mixture Proportion of Lasso Penalty (type: double, default: 0.0). A
#' value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
#' indicates ridge regression (a.k.a weight decay). Must be zero for
#' optimizers `"ADAMw"`, `"RMSprop"`, `"Adadelta"`.
#' @param hidden_units An integer for the number of hidden units, or a vector
#' of integers. If a vector of integers, the model will have `length(hidden_units)`
#' layers each with `hidden_units[i]` hidden units.
#' @param hidden_units_2 An integer for the number of hidden units for a second layer.
#' @param activation A character vector for the activation function (such as
#' "relu", "tanh", "sigmoid", and so on). See [brulee_activations()] for
#' a list of possible values. If `hidden_units` is a vector, `activation`
#' can be a character vector with length equals to `length(hidden_units)`
#' specifying the activation for each hidden layer.
#' @param activation_2 A character vector for the activation function for a second layer.
#' @param optimizer The method used in the optimization procedure. Possible choices
#' are `"SGD"`, `"ADAMw"`, `"Adadelta"`, `"Adagrad"`, `"RMSprop"`, and
#' `"LBFGS"`. `"LBFGS"` is the only second-order method and does not use
#' batches.
#' @param learn_rate A positive number that controls the initial rapidity that
#' the model moves along the descent path. Values around 0.1 or less are
#' typical.
#' @param rate_schedule A single character value for how the learning rate
#' should change as the optimization proceeds. Possible values are
#' `"none"` (the default), `"decay_time"`, `"decay_expo"`, `"cyclic"` and
#' `"step"`. See [schedule_decay_time()] for more details.
#' @param momentum A positive number usually on `[0.50, 0.99]` for the momentum
#' parameter in gradient descent. (optimizers `"SGD"`, and `"RMSprop"` only,
#' ignored otherwise).
#' @param dropout The proportion of parameters set to zero.
#' @param class_weights Numeric class weights (classification only). The value
#' can be:
#'
#' * A named numeric vector (in any order) where the names are the outcome
#' factor levels.
#' * An unnamed numeric vector assumed to be in the same order as the outcome
#' factor levels.
#' * A single numeric value for the least frequent class in the training data
#' and all other classes receive a weight of one.
#' @param validation The proportion of the data randomly assigned to a
#' validation set.
#' @param batch_size An integer for the number of training set points in each
#' batch. (`optimizer != "LBFGS"` only, ignored otherwise)
#' @param stop_iter A non-negative integer for how many iterations with no
#' improvement before stopping.
#' @param grad_norm_clip,grad_value_clip Two numeric values, possibly `Inf`,
#' that prevents the gradient's values or norm(s) from exceeding the specified
#' value. This can be helpful if training stops early with the message that
#' `"Loss is NaN at epoch x Training is stopped."`
#' @param verbose A logical that prints out the iteration history.
#' @param device A single character string for the device to train on (e.g.,
#' `"cpu"` or `"cuda"` for GPU). If `NULL`, the function will use the GPU if
#' available, otherwise CPU. See [training_efficiency].
#' @param ... Options to pass to the learning rate schedulers via
#' [set_learn_rate()]. For example, the `reduction` or `steps` arguments to
#' [schedule_step()] could be passed here.
#'
#' @details
#'
#' This function fits feed-forward neural network models for regression (when
#' the outcome is a number) or classification (a factor). For regression, the
#' mean squared error is optimized and cross-entropy is the loss function for
#' classification.
#'
#' When the outcome is a number, the function internally standardizes the
#' outcome data to have mean zero and a standard deviation of one. The prediction
#' function creates predictions on the original scale.
#'
#' By default, training halts when the validation loss increases for at least
#' `step_iter` iterations. If `validation = 0` the training set loss is used.
#'
#' The _predictors_ data should all be numeric and encoded in the same units (e.g.
#' standardized to the same range or distribution). If there are factor
#' predictors, use a recipe or formula to create indicator variables (or some
#' other method) to make them numeric. Predictors should be in the same units
#' before training.
#'
#' The model objects are saved for each epoch so that the number of epochs can
#' be efficiently tuned. Both the [coef()] and [predict()] methods for this
#' model have an `epoch` argument (which defaults to the epoch with the best
#' loss value).
#'
#' The use of the L1 penalty (a.k.a. the lasso penalty) does _not_ force
#' parameters to be strictly zero (as it does in packages such as \pkg{glmnet}).
#' The zeroing out of parameters is a specific feature the optimization method
#' used in those packages.
#'
#' ## Learning Rates
#'
#' The learning rate can be set to constant (the default) or dynamically set
#' via a learning rate scheduler (via the `rate_schedule`). Using
#' `rate_schedule = 'none'` uses the `learn_rate` argument. Otherwise, any
#' arguments to the schedulers can be passed via `...`.
#'
#' @references
#'
#' adagrad (adaptive gradient algorithm): Duchi, J., Hazan, E., & Singer, Y.
#' (2011). Adaptive subgradient methods for online learning and stochastic
#' optimization. _Journal of machine learning research_, 12(7).
#'
#' adadelta: Zeiler, M. D. (2012). Adadelta: an adaptive learning rate method.
#' arXiv preprint arXiv:1212.5701.
#'
#' ADAMw: Loshchilov, I., & Hutter, F. (2017). Decoupled weight decay
#' regularization. arXiv preprint arXiv:1711.05101.
#'
#'
#' @seealso [predict.brulee_mlp()], [coef.brulee_mlp()], [autoplot.brulee_mlp()]
#' @return
#'
#' A `brulee_mlp` object with elements:
#' * `models_obj`: a serialized raw vector for the torch module.
#' * `estimates`: a list of matrices with the model parameter estimates per
#' epoch.
#' * `best_epoch`: an integer for the epoch with the smallest loss.
#' * `loss`: A vector of loss values (MSE for regression, negative log-
#' likelihood for classification) at each epoch.
#' * `dim`: A list of data dimensions.
#' * `y_stats`: A list of summary statistics for numeric outcomes.
#' * `parameters`: A list of some tuning parameter values.
#' * `blueprint`: The `hardhat` blueprint data.
#'
#' @examplesIf !brulee:::is_cran_check()
#' \donttest{
#' if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) {
#'
#' ## -----------------------------------------------------------------------------
#' # regression examples (increase # epochs to get better results)
#'
#' data(ames, package = "modeldata")
#'
#' ames$Sale_Price <- log10(ames$Sale_Price)
#'
#' set.seed(122)
#' in_train <- sample(1:nrow(ames), 2000)
#' ames_train <- ames[ in_train,]
#' ames_test <- ames[-in_train,]
#'
#'
#' # Using matrices
#' set.seed(1)
#' fit <-
#' brulee_mlp(x = as.matrix(ames_train[, c("Longitude", "Latitude")]),
#' y = ames_train$Sale_Price, penalty = 0.10)
#'
#' # Using recipe
#' library(recipes)
#'
#' ames_rec <-
#' recipe(Sale_Price ~ Bldg_Type + Neighborhood + Year_Built + Gr_Liv_Area +
#' Full_Bath + Year_Sold + Lot_Area + Central_Air + Longitude + Latitude,
#' data = ames_train) |>
#' # Transform some highly skewed predictors
#' step_BoxCox(Lot_Area, Gr_Liv_Area) |>
#' # Lump some rarely occurring categories into "other"
#' step_other(Neighborhood, threshold = 0.05) |>
#' # Encode categorical predictors as binary.
#' step_dummy(all_nominal_predictors(), one_hot = TRUE) |>
#' # Add an interaction effect:
#' step_interact(~ starts_with("Central_Air"):Year_Built) |>
#' step_zv(all_predictors()) |>
#' step_normalize(all_numeric_predictors())
#'
#' set.seed(2)
#' fit <- brulee_mlp(ames_rec, data = ames_train, hidden_units = 20,
#' dropout = 0.05, rate_schedule = "cyclic", step_size = 4)
#' fit
#'
#' autoplot(fit)
#'
#' library(ggplot2)
#'
#' predict(fit, ames_test) |>
#' bind_cols(ames_test) |>
#' ggplot(aes(x = .pred, y = Sale_Price)) +
#' geom_abline(col = "green") +
#' geom_point(alpha = .3) +
#' lims(x = c(4, 6), y = c(4, 6)) +
#' coord_fixed(ratio = 1)
#'
#' library(yardstick)
#' predict(fit, ames_test) |>
#' bind_cols(ames_test) |>
#' rmse(Sale_Price, .pred)
#'
#' # Using multiple hidden layers and activation functions
#' set.seed(2)
#' hidden_fit <- brulee_mlp(ames_rec, data = ames_train,
#' hidden_units = c(15L, 17L), activation = c("relu", "elu"),
#' dropout = 0.05, rate_schedule = "cyclic", step_size = 4)
#'
#' predict(hidden_fit, ames_test) |>
#' bind_cols(ames_test) |>
#' rmse(Sale_Price, .pred)
#'
#' # ------------------------------------------------------------------------------
#' # classification
#'
#' library(dplyr)
#' library(ggplot2)
#'
#' data("parabolic", package = "modeldata")
#'
#' set.seed(1)
#' in_train <- sample(1:nrow(parabolic), 300)
#' parabolic_tr <- parabolic[ in_train,]
#' parabolic_te <- parabolic[-in_train,]
#'
#' set.seed(2)
#' cls_fit <- brulee_mlp(class ~ ., data = parabolic_tr, hidden_units = 2,
#' epochs = 200L, learn_rate = 0.1, activation = "elu",
#' penalty = 0.1, batch_size = 2^8, optimizer = "SGD")
#'
#' summary(cls_fit)
#'
#' autoplot(cls_fit)
#'
#' grid_points <- seq(-4, 4, length.out = 100)
#'
#' grid <- expand.grid(X1 = grid_points, X2 = grid_points)
#'
#' predict(cls_fit, grid, type = "prob") |>
#' bind_cols(grid) |>
#' ggplot(aes(X1, X2)) +
#' geom_contour(aes(z = .pred_Class1), breaks = 1/2, col = "black") +
#' geom_point(data = parabolic_te, aes(col = class))
#'
#' }
#' }
#' @export
brulee_mlp <- function(x, ...) {
UseMethod("brulee_mlp")
}
#' @export
#' @rdname brulee_mlp
brulee_mlp.default <- function(x, ...) {
stop(
"`brulee_mlp()` is not defined for a '",
class(x)[1],
"'.",
call. = FALSE
)
}
# XY method - data frame
#' @export
#' @rdname brulee_mlp
brulee_mlp.data.frame <-
function(
x,
y,
epochs = 100L,
hidden_units = 3L,
activation = "relu",
penalty = 0.001,
mixture = 0,
dropout = 0,
validation = 0.1,
optimizer = "LBFGS",
learn_rate = 0.01,
rate_schedule = "none",
momentum = 0.0,
batch_size = NULL,
class_weights = NULL,
stop_iter = 5,
grad_value_clip = 5,
grad_norm_clip = 5,
verbose = FALSE,
device = NULL,
...
) {
processed <- hardhat::mold(x, y)
brulee_mlp_bridge(
processed,
epochs = epochs,
hidden_units = hidden_units,
activation = activation,
learn_rate = learn_rate,
rate_schedule = rate_schedule,
penalty = penalty,
mixture = mixture,
dropout = dropout,
validation = validation,
optimizer = optimizer,
momentum = momentum,
batch_size = batch_size,
class_weights = class_weights,
stop_iter = stop_iter,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
verbose = verbose,
device = device,
...
)
}
# XY method - matrix
#' @export
#' @rdname brulee_mlp
brulee_mlp.matrix <- function(
x,
y,
epochs = 100L,
hidden_units = 3L,
activation = "relu",
penalty = 0.001,
mixture = 0,
dropout = 0,
validation = 0.1,
optimizer = "LBFGS",
learn_rate = 0.01,
rate_schedule = "none",
momentum = 0.0,
batch_size = NULL,
class_weights = NULL,
stop_iter = 5,
grad_value_clip = 5,
grad_norm_clip = 5,
verbose = FALSE,
device = NULL,
...
) {
processed <- hardhat::mold(x, y)
brulee_mlp_bridge(
processed,
epochs = epochs,
hidden_units = hidden_units,
activation = activation,
learn_rate = learn_rate,
rate_schedule = rate_schedule,
momentum = momentum,
penalty = penalty,
mixture = mixture,
dropout = dropout,
validation = validation,
optimizer = optimizer,
batch_size = batch_size,
class_weights = class_weights,
stop_iter = stop_iter,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
verbose = verbose,
device = device,
...
)
}
# Formula method
#' @export
#' @rdname brulee_mlp
brulee_mlp.formula <-
function(
formula,
data,
epochs = 100L,
hidden_units = 3L,
activation = "relu",
penalty = 0.001,
mixture = 0,
dropout = 0,
validation = 0.1,
optimizer = "LBFGS",
learn_rate = 0.01,
rate_schedule = "none",
momentum = 0.0,
batch_size = NULL,
class_weights = NULL,
stop_iter = 5,
grad_value_clip = 5,
grad_norm_clip = 5,
verbose = FALSE,
device = NULL,
...
) {
processed <- hardhat::mold(formula, data)
brulee_mlp_bridge(
processed,
epochs = epochs,
hidden_units = hidden_units,
activation = activation,
learn_rate = learn_rate,
rate_schedule = rate_schedule,
momentum = momentum,
penalty = penalty,
mixture = mixture,
dropout = dropout,
validation = validation,
optimizer = optimizer,
batch_size = batch_size,
class_weights = class_weights,
stop_iter = stop_iter,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
verbose = verbose,
device = device,
...
)
}
# Recipe method
#' @export
#' @rdname brulee_mlp
brulee_mlp.recipe <-
function(
x,
data,
epochs = 100L,
hidden_units = 3L,
activation = "relu",
penalty = 0.001,
mixture = 0,
dropout = 0,
validation = 0.1,
optimizer = "LBFGS",
learn_rate = 0.01,
rate_schedule = "none",
momentum = 0.0,
batch_size = NULL,
class_weights = NULL,
stop_iter = 5,
grad_value_clip = 5,
grad_norm_clip = 5,
verbose = FALSE,
device = NULL,
...
) {
processed <- hardhat::mold(x, data)
brulee_mlp_bridge(
processed,
epochs = epochs,
hidden_units = hidden_units,
activation = activation,
learn_rate = learn_rate,
rate_schedule = rate_schedule,
momentum = momentum,
penalty = penalty,
mixture = mixture,
dropout = dropout,
validation = validation,
optimizer = optimizer,
batch_size = batch_size,
class_weights = class_weights,
stop_iter = stop_iter,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
verbose = verbose,
device = device,
...
)
}
# ------------------------------------------------------------------------------
# Bridge
brulee_mlp_bridge <- function(
processed,
epochs,
hidden_units,
activation,
learn_rate,
rate_schedule,
momentum,
penalty,
mixture,
dropout,
class_weights,
validation,
optimizer,
batch_size,
stop_iter,
grad_value_clip,
grad_norm_clip,
verbose,
device,
...,
call = rlang::caller_env()
) {
if (!torch::torch_is_installed()) {
cli::cli_abort(
"The torch backend has not been installed; use {.run torch::install_torch()}.",
call = call
)
}
# Guess device if not specified
device <- guess_brulee_device(device)
# Validate MLP-specific arguments
mlp_validated <- validate_mlp_args(
hidden_units = hidden_units,
activation = activation,
dropout = dropout,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
call = call
)
# Extract validated/coerced values
hidden_units <- mlp_validated$hidden_units
activation <- mlp_validated$activation
# Handle batch_size special logic for MLP (optimizer-dependent)
if (!is.null(batch_size) & optimizer != "LBFGS") {
if (is.numeric(batch_size) & !is.integer(batch_size)) {
batch_size <- as.integer(batch_size)
}
check_integer(batch_size, single = TRUE, 1, call = call)
}
if (is.null(batch_size) & optimizer != "LBFGS") {
batch_size <- 32L
if (batch_size >= nrow(processed)) {
batch_size <- max(2, ceiling(nrow(processed) / 10))
batch_size <- as.integer(batch_size)
}
}
# Validate common arguments
validated <- validate_common_args(
epochs = epochs,
batch_size = batch_size,
penalty = penalty,
mixture = mixture,
validation = validation,
momentum = momentum,
learn_rate = learn_rate,
verbose = verbose,
call = call
)
# Extract validated/coerced values
epochs <- validated$epochs
batch_size <- validated$batch_size
## -----------------------------------------------------------------------------
# Process predictors
predictors <- process_predictors(processed$predictors, call = call)
## -----------------------------------------------------------------------------
# Validate outcome (MLP accepts both numeric and factor)
outcome <- validate_mlp_outcome(processed$outcomes[[1]], call = call)
# ------------------------------------------------------------------------------
lvls <- levels(outcome)
xtab <- table(outcome)
class_weights <- check_class_weights(class_weights, lvls, xtab, call = call)
## -----------------------------------------------------------------------------
fit <-
mlp_fit_imp(
x = predictors,
y = outcome,
epochs = epochs,
hidden_units = hidden_units,
activation = activation,
learn_rate = learn_rate,
rate_schedule = rate_schedule,
momentum = momentum,
penalty = penalty,
mixture = mixture,
dropout = dropout,
validation = validation,
optimizer = optimizer,
batch_size = batch_size,
class_weights = class_weights,
stop_iter = stop_iter,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
verbose = verbose,
device = device,
...
)
new_brulee_mlp(
model_obj = fit$model_obj,
estimates = fit$estimates,
best_epoch = fit$best_epoch,
loss = fit$loss,
dims = fit$dims,
y_stats = fit$y_stats,
parameters = fit$parameters,
device = fit$device,
blueprint = processed$blueprint
)
}
new_brulee_mlp <- function(
model_obj,
estimates,
best_epoch,
loss,
dims,
y_stats,
parameters,
device,
blueprint
) {
if (!inherits(model_obj, "raw")) {
cli::cli_abort("{.arg model_obj} should be a raw vector.", call = NULL)
}
if (!is.list(estimates)) {
cli::cli_abort("{.arg estimates} should be a list.", call = NULL)
}
if (!is.vector(best_epoch) || !is.integer(best_epoch)) {
cli::cli_abort("{.arg best_epoch} should be an integer.", call = NULL)
}
if (!is.vector(loss) || !is.numeric(loss)) {
cli::cli_abort("{.arg loss} should be a numeric vector.", call = NULL)
}
if (!is.list(dims)) {
cli::cli_abort("{.arg dims} should be a list.", call = NULL)
}
if (!is.list(y_stats)) {
cli::cli_abort("{.arg y_stats} should be a list.", call = NULL)
}
if (!is.list(parameters)) {
cli::cli_abort("{.arg parameters} should be a list.", call = NULL)
}
if (!inherits(blueprint, "hardhat_blueprint")) {
cli::cli_abort(
"{.arg blueprint} should be a hardhat blueprint.",
call = NULL
)
}
# Save the estimates that have values
num_items <- purrr::map_int(estimates, length)
estimates <- estimates[num_items > 0]
hardhat::new_model(
model_obj = model_obj,
estimates = estimates,
best_epoch = best_epoch,
loss = loss,
dims = dims,
y_stats = y_stats,
parameters = parameters,
device = device,
blueprint = blueprint,
class = "brulee_mlp"
)
}
## -----------------------------------------------------------------------------
# Fit code
mlp_fit_imp <-
function(
x,
y,
epochs = 100L,
batch_size = 32,
hidden_units = 3L,
penalty = 0.001,
mixture = 0,
dropout = 0,
validation = 0.1,
optimizer = "LBFGS",
learn_rate = 0.01,
rate_schedule = "none",
momentum = 0.0,
activation = "relu",
class_weights = NULL,
stop_iter = 5,
grad_value_clip = 5,
grad_norm_clip = 5,
verbose = FALSE,
device = "cpu",
...
) {
start_seed <- sample.int(10^5, 1)
torch::torch_manual_seed(start_seed)
## ---------------------------------------------------------------------------
# General data checks:
check_data_att(x, y)
# Check missing values
compl_data <- check_missing_data(x, y, "brulee_mlp", verbose)
x <- compl_data$x
y <- compl_data$y
n <- length(y)
p <- ncol(x)
if (is.factor(y)) {
lvls <- levels(y)
y_dim <- length(lvls)
# the model will output softmax values.
# so we need to use negative likelihood loss and
# pass the log of softmax.
loss_fn <- function(input, target, wts = NULL) {
nnf_nll_loss(
weight = weights_to_tensor(wts),
input = torch::torch_log(input),
target = target,
)
}
} else {
y_dim <- 1
lvls <- NULL
loss_fn <- function(input, target, wts = NULL) {
nnf_mse_loss(input, target$view(c(-1, 1)))
}
}
# Split validation set
val_split <- split_validation(x, y, validation)
x <- val_split$x_train
y <- val_split$y_train
x_val <- val_split$x_val
y_val <- val_split$y_val
# Scale outcomes for regression
if (!is.factor(y)) {
y_stats <- scale_stats(y)
y <- scale_y(y, y_stats)
if (validation > 0) {
y_val <- scale_y(y_val, y_stats)
}
loss_label <- "\tLoss (scaled):"
} else {
y_stats <- list(mean = NA_real_, sd = NA_real_)
loss_label <- "\tLoss:"
}
# Determine batch size (MLP-specific logic for LBFGS)
if (optimizer == "LBFGS") {
batch_size <- nrow(x)
}
batch_size <- min(batch_size, nrow(x))
## ---------------------------------------------------------------------------
# Convert to index sampler and data loader
or_dtype <- torch::torch_get_default_dtype()
on.exit(torch::torch_set_default_dtype(or_dtype))
torch::torch_set_default_dtype(torch::torch_float64())
# Set device context for training
training_output <- torch::with_device(device = device, {
# Reset the seed so that different optimizers start from the same values
torch::torch_manual_seed(start_seed + 1)
torch_data <- setup_torch_data(
x,
y,
x_val,
y_val,
batch_size,
validation,
device = device
)
dl <- torch_data$dl
dl_val <- torch_data$dl_val
# ------------------------------------------------------------------------------
# Return value
res <-
list(
dims = list(
p = p,
n = n,
h = hidden_units,
y = y_dim,
levels = lvls,
features = colnames(x)
),
y_stats = y_stats,
parameters = list(
activation = activation,
hidden_units = hidden_units,
learn_rate = learn_rate,
class_weights = as.numeric(class_weights),
penalty = penalty,
mixture = mixture,
dropout = dropout,
validation = validation,
optimizer = optimizer,
batch_size = batch_size,
momentum = momentum,
stop_iter = stop_iter,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
sched = rate_schedule,
sched_opt = list(...)
)
)
## ---------------------------------------------------------------------------
# Initialize model and optimizer
d_type <- torch::torch_get_default_dtype()
on.exit(torch::torch_set_default_dtype(d_type))
torch::torch_set_default_dtype(torch::torch_float64())
model <- mlp_module(ncol(x), hidden_units, activation, dropout, y_dim)
model$to(device = device)
mixture <- check_mixture(mixture, optimizer)
# Note that if a penalty is used, it might affect the `loss_fn` _or_ the
# optimizer depending on whether it's pure L2 (mixture = 0) or has L1 component.
loss_fn <- make_penalized_loss(
loss_fn,
model,
penalty,
mixture,
optimizer
)
optimizer_obj <- set_optimizer(
optimizer,
model,
learn_rate,
momentum,
penalty,
mixture
)
## ---------------------------------------------------------------------------
best_epoch <- 0L
poor_epoch <- 0L
loss_vec <- rep(NA_real_, epochs + 1)
if (validation > 0) {
pred <- model(dl_val$dataset$tensors$x)
loss <- loss_fn(pred, dl_val$dataset$tensors$y, class_weights)
} else {
pred <- model(dl$dataset$tensors$x)
loss <- loss_fn(pred, dl$dataset$tensors$y, class_weights)
}
loss_vec[1] <- loss$item()
loss_prev <- loss_curr <- loss_vec[1]
loss_min <- loss_prev
if (verbose) {
epoch_chr <- gsub(" ", "0", format(0:epochs))
cli::cli_inform(
"epoch: {epoch_chr[1]}, learn rate: {signif(learn_rate, 3)}, {loss_label} {signif(loss_curr, 3)}"
)
epoch_chr <- epoch_chr[-1]
}
param_per_epoch <- vector(mode = "list", length = epochs + 1)
param_per_epoch[[1]] <-
lapply(model$state_dict(), function(x) torch::as_array(x$cpu()))
res$model_obj <- model_to_raw(model)
res$estimates <- param_per_epoch[[1]]
res$loss <- loss_vec[1]
res$best_epoch <- best_epoch
## -----------------------------------------------------------------------------
# Run training loop
training_result <- run_training_loop(
model = model,
dl = dl,
dl_val = dl_val,
loss_fn = loss_fn,
optimizer_obj = optimizer_obj,
epochs = epochs,
learn_rate = learn_rate,
stop_iter = stop_iter,
validation = validation,
class_weights = class_weights,
loss_label = loss_label,
verbose = verbose,
grad_value_clip = grad_value_clip,
grad_norm_clip = grad_norm_clip,
rate_schedule = rate_schedule,
...
)
# Prepend initial parameters and loss to match MLP's original behavior
param_per_epoch <- c(
list(param_per_epoch[[1]]),
training_result$param_per_epoch
)
loss_vec <- c(loss_vec[1], training_result$loss_vec)
best_epoch <- training_result$best_epoch
# Update result object
res$model_obj <- model_to_raw(model)
res$estimates <- param_per_epoch
res$loss <- loss_vec
res$best_epoch <- best_epoch
res
})
## ---------------------------------------------------------------------------
# Add device to result
training_output$device <- device
training_output
}
mlp_module <-
torch::nn_module(
"mlp_module",
initialize = function(num_pred, hidden_units, act_type, dropout, y_dim) {
layers <- list()
# input layer
layers[[1]] <- torch::nn_linear(num_pred, hidden_units[1])
layers[[1]] <- init_layer(layers[[1]], "linear")
layers[[2]] <- get_activation_fn(act_type[1])
# if hidden units is a vector then we add those layers
if (length(hidden_units) > 1) {
for (i in 2:length(hidden_units)) {
layers[[length(layers) + 1]] <- torch::nn_linear(
hidden_units[i - 1],
hidden_units[i]
)
layers[[length(layers)]] <- init_layer(
layers[[length(layers)]],
"linear"
)
layers[[length(layers) + 1]] <- get_activation_fn(act_type[i])
}
}
# we only add dropout between the last layer and the output layer
if (dropout > 0) {
layers[[length(layers) + 1]] <- torch::nn_dropout(p = dropout)
}
# output layer
layers[[length(layers) + 1]] <- torch::nn_linear(
hidden_units[length(hidden_units)],
y_dim
)
layers[[length(layers)]] <- init_layer(layers[[length(layers)]], "linear")
# conditionally add the softmax layer
if (y_dim > 1) {
layers[[length(layers) + 1]] <- torch::nn_softmax(dim = 2)
}
# create a sequential module that calls the layers in the same order.
self$model <- torch::nn_sequential(!!!layers)
},
forward = function(x) {
self$model(x)
}
)
## -----------------------------------------------------------------------------
get_num_mlp_coef <- function(x) {