
Trainer loop: mini-batch SGD with per-epoch evaluation (Geron Ch 14, morie.fn hmhftn)
Source:R/geron_w4b_native.R
morie_geron_hf_trainer.RdShuffle order comes from the shared LCG stream, not numpy's PCG64 permutation; with batch_size >= nrow(X) there is one batch per epoch so the result is exact and shuffle-independent.
Usage
morie_geron_hf_trainer(model, args = list(), train_ds, eval_ds = NULL)Value
List with params, best_params, train_loss, eval_loss, history, best_epoch, estimate, n, method.
Examples
if (requireNamespace("ranger", quietly = TRUE)) {
W <- list(W_z = matrix(0, 2, 2), U_z = matrix(0, 2, 2), b_z = c(0,
0), W_r = matrix(0, 2, 2), U_r = matrix(0, 2, 2), b_r = c(0,
0), W_h = matrix(0, 2, 2), U_h = matrix(0, 2, 2), b_h = c(0,
0))
r <- morie_geron_gru(c(0, 0), c(4, -2), W)
X <- matrix(c(1, 2, 3, 4), ncol = 1)
y <- c(3, 6, 9, 12)
lg <- function(p, Xb, yb) {
r <- as.numeric(Xb %*% p) - yb
list(mean(r^2), (2/length(yb)) * as.numeric(t(Xb) %*% r))
}
m0 <- list(params = 0, loss_and_grad = lg)
morie_geron_hf_trainer(m0, list(epochs = 50, batch_size = 4,
learning_rate = 0.05), list(X, y), list(X, y))
}
#> $params
#> [1] 3
#>
#> $best_params
#> [1] 3
#>
#> $train_loss
#> [1] 0
#>
#> $eval_loss
#> [1] 0
#>
#> $history
#> $history[[1]]
#> $history[[1]]$epoch
#> [1] 1
#>
#> $history[[1]]$train_loss
#> [1] 67.5
#>
#> $history[[1]]$eval_loss
#> [1] 4.21875
#>
#>
#> $history[[2]]
#> $history[[2]]$epoch
#> [1] 2
#>
#> $history[[2]]$train_loss
#> [1] 4.21875
#>
#> $history[[2]]$eval_loss
#> [1] 0.2636719
#>
#>
#> $history[[3]]
#> $history[[3]]$epoch
#> [1] 3
#>
#> $history[[3]]$train_loss
#> [1] 0.2636719
#>
#> $history[[3]]$eval_loss
#> [1] 0.01647949
#>
#>
#> $history[[4]]
#> $history[[4]]$epoch
#> [1] 4
#>
#> $history[[4]]$train_loss
#> [1] 0.01647949
#>
#> $history[[4]]$eval_loss
#> [1] 0.001029968
#>
#>
#> $history[[5]]
#> $history[[5]]$epoch
#> [1] 5
#>
#> $history[[5]]$train_loss
#> [1] 0.001029968
#>
#> $history[[5]]$eval_loss
#> [1] 6.437302e-05
#>
#>
#> $history[[6]]
#> $history[[6]]$epoch
#> [1] 6
#>
#> $history[[6]]$train_loss
#> [1] 6.437302e-05
#>
#> $history[[6]]$eval_loss
#> [1] 4.023314e-06
#>
#>
#> $history[[7]]
#> $history[[7]]$epoch
#> [1] 7
#>
#> $history[[7]]$train_loss
#> [1] 4.023314e-06
#>
#> $history[[7]]$eval_loss
#> [1] 2.514571e-07
#>
#>
#> $history[[8]]
#> $history[[8]]$epoch
#> [1] 8
#>
#> $history[[8]]$train_loss
#> [1] 2.514571e-07
#>
#> $history[[8]]$eval_loss
#> [1] 1.571607e-08
#>
#>
#> $history[[9]]
#> $history[[9]]$epoch
#> [1] 9
#>
#> $history[[9]]$train_loss
#> [1] 1.571607e-08
#>
#> $history[[9]]$eval_loss
#> [1] 9.822543e-10
#>
#>
#> $history[[10]]
#> $history[[10]]$epoch
#> [1] 10
#>
#> $history[[10]]$train_loss
#> [1] 9.822543e-10
#>
#> $history[[10]]$eval_loss
#> [1] 6.139089e-11
#>
#>
#> $history[[11]]
#> $history[[11]]$epoch
#> [1] 11
#>
#> $history[[11]]$train_loss
#> [1] 6.139089e-11
#>
#> $history[[11]]$eval_loss
#> [1] 3.836931e-12
#>
#>
#> $history[[12]]
#> $history[[12]]$epoch
#> [1] 12
#>
#> $history[[12]]$train_loss
#> [1] 3.836931e-12
#>
#> $history[[12]]$eval_loss
#> [1] 2.398082e-13
#>
#>
#> $history[[13]]
#> $history[[13]]$epoch
#> [1] 13
#>
#> $history[[13]]$train_loss
#> [1] 2.398082e-13
#>
#> $history[[13]]$eval_loss
#> [1] 1.498801e-14
#>
#>
#> $history[[14]]
#> $history[[14]]$epoch
#> [1] 14
#>
#> $history[[14]]$train_loss
#> [1] 1.498801e-14
#>
#> $history[[14]]$eval_loss
#> [1] 9.367507e-16
#>
#>
#> $history[[15]]
#> $history[[15]]$epoch
#> [1] 15
#>
#> $history[[15]]$train_loss
#> [1] 9.367507e-16
#>
#> $history[[15]]$eval_loss
#> [1] 5.854692e-17
#>
#>
#> $history[[16]]
#> $history[[16]]$epoch
#> [1] 16
#>
#> $history[[16]]$train_loss
#> [1] 5.854692e-17
#>
#> $history[[16]]$eval_loss
#> [1] 3.659182e-18
#>
#>
#> $history[[17]]
#> $history[[17]]$epoch
#> [1] 17
#>
#> $history[[17]]$train_loss
#> [1] 3.659182e-18
#>
#> $history[[17]]$eval_loss
#> [1] 2.286989e-19
#>
#>
#> $history[[18]]
#> $history[[18]]$epoch
#> [1] 18
#>
#> $history[[18]]$train_loss
#> [1] 2.286989e-19
#>
#> $history[[18]]$eval_loss
#> [1] 1.429368e-20
#>
#>
#> $history[[19]]
#> $history[[19]]$epoch
#> [1] 19
#>
#> $history[[19]]$train_loss
#> [1] 1.429368e-20
#>
#> $history[[19]]$eval_loss
#> [1] 8.933551e-22
#>
#>
#> $history[[20]]
#> $history[[20]]$epoch
#> [1] 20
#>
#> $history[[20]]$train_loss
#> [1] 8.933551e-22
#>
#> $history[[20]]$eval_loss
#> [1] 5.583469e-23
#>
#>
#> $history[[21]]
#> $history[[21]]$epoch
#> [1] 21
#>
#> $history[[21]]$train_loss
#> [1] 5.583469e-23
#>
#> $history[[21]]$eval_loss
#> [1] 3.489668e-24
#>
#>
#> $history[[22]]
#> $history[[22]]$epoch
#> [1] 22
#>
#> $history[[22]]$train_loss
#> [1] 3.489668e-24
#>
#> $history[[22]]$eval_loss
#> [1] 2.181043e-25
#>
#>
#> $history[[23]]
#> $history[[23]]$epoch
#> [1] 23
#>
#> $history[[23]]$train_loss
#> [1] 2.181043e-25
#>
#> $history[[23]]$eval_loss
#> [1] 1.363152e-26
#>
#>
#> $history[[24]]
#> $history[[24]]$epoch
#> [1] 24
#>
#> $history[[24]]$train_loss
#> [1] 1.363152e-26
#>
#> $history[[24]]$eval_loss
#> [1] 8.519698e-28
#>
#>
#> $history[[25]]
#> $history[[25]]$epoch
#> [1] 25
#>
#> $history[[25]]$train_loss
#> [1] 8.519698e-28
#>
#> $history[[25]]$eval_loss
#> [1] 4.989545e-29
#>
#>
#> $history[[26]]
#> $history[[26]]$epoch
#> [1] 26
#>
#> $history[[26]]$train_loss
#> [1] 4.989545e-29
#>
#> $history[[26]]$eval_loss
#> [1] 7.296963e-30
#>
#>
#> $history[[27]]
#> $history[[27]]$epoch
#> [1] 27
#>
#> $history[[27]]$train_loss
#> [1] 7.296963e-30
#>
#> $history[[27]]$eval_loss
#> [1] 0
#>
#>
#> $history[[28]]
#> $history[[28]]$epoch
#> [1] 28
#>
#> $history[[28]]$train_loss
#> [1] 0
#>
#> $history[[28]]$eval_loss
#> [1] 0
#>
#>
#> $history[[29]]
#> $history[[29]]$epoch
#> [1] 29
#>
#> $history[[29]]$train_loss
#> [1] 0
#>
#> $history[[29]]$eval_loss
#> [1] 0
#>
#>
#> $history[[30]]
#> $history[[30]]$epoch
#> [1] 30
#>
#> $history[[30]]$train_loss
#> [1] 0
#>
#> $history[[30]]$eval_loss
#> [1] 0
#>
#>
#> $history[[31]]
#> $history[[31]]$epoch
#> [1] 31
#>
#> $history[[31]]$train_loss
#> [1] 0
#>
#> $history[[31]]$eval_loss
#> [1] 0
#>
#>
#> $history[[32]]
#> $history[[32]]$epoch
#> [1] 32
#>
#> $history[[32]]$train_loss
#> [1] 0
#>
#> $history[[32]]$eval_loss
#> [1] 0
#>
#>
#> $history[[33]]
#> $history[[33]]$epoch
#> [1] 33
#>
#> $history[[33]]$train_loss
#> [1] 0
#>
#> $history[[33]]$eval_loss
#> [1] 0
#>
#>
#> $history[[34]]
#> $history[[34]]$epoch
#> [1] 34
#>
#> $history[[34]]$train_loss
#> [1] 0
#>
#> $history[[34]]$eval_loss
#> [1] 0
#>
#>
#> $history[[35]]
#> $history[[35]]$epoch
#> [1] 35
#>
#> $history[[35]]$train_loss
#> [1] 0
#>
#> $history[[35]]$eval_loss
#> [1] 0
#>
#>
#> $history[[36]]
#> $history[[36]]$epoch
#> [1] 36
#>
#> $history[[36]]$train_loss
#> [1] 0
#>
#> $history[[36]]$eval_loss
#> [1] 0
#>
#>
#> $history[[37]]
#> $history[[37]]$epoch
#> [1] 37
#>
#> $history[[37]]$train_loss
#> [1] 0
#>
#> $history[[37]]$eval_loss
#> [1] 0
#>
#>
#> $history[[38]]
#> $history[[38]]$epoch
#> [1] 38
#>
#> $history[[38]]$train_loss
#> [1] 0
#>
#> $history[[38]]$eval_loss
#> [1] 0
#>
#>
#> $history[[39]]
#> $history[[39]]$epoch
#> [1] 39
#>
#> $history[[39]]$train_loss
#> [1] 0
#>
#> $history[[39]]$eval_loss
#> [1] 0
#>
#>
#> $history[[40]]
#> $history[[40]]$epoch
#> [1] 40
#>
#> $history[[40]]$train_loss
#> [1] 0
#>
#> $history[[40]]$eval_loss
#> [1] 0
#>
#>
#> $history[[41]]
#> $history[[41]]$epoch
#> [1] 41
#>
#> $history[[41]]$train_loss
#> [1] 0
#>
#> $history[[41]]$eval_loss
#> [1] 0
#>
#>
#> $history[[42]]
#> $history[[42]]$epoch
#> [1] 42
#>
#> $history[[42]]$train_loss
#> [1] 0
#>
#> $history[[42]]$eval_loss
#> [1] 0
#>
#>
#> $history[[43]]
#> $history[[43]]$epoch
#> [1] 43
#>
#> $history[[43]]$train_loss
#> [1] 0
#>
#> $history[[43]]$eval_loss
#> [1] 0
#>
#>
#> $history[[44]]
#> $history[[44]]$epoch
#> [1] 44
#>
#> $history[[44]]$train_loss
#> [1] 0
#>
#> $history[[44]]$eval_loss
#> [1] 0
#>
#>
#> $history[[45]]
#> $history[[45]]$epoch
#> [1] 45
#>
#> $history[[45]]$train_loss
#> [1] 0
#>
#> $history[[45]]$eval_loss
#> [1] 0
#>
#>
#> $history[[46]]
#> $history[[46]]$epoch
#> [1] 46
#>
#> $history[[46]]$train_loss
#> [1] 0
#>
#> $history[[46]]$eval_loss
#> [1] 0
#>
#>
#> $history[[47]]
#> $history[[47]]$epoch
#> [1] 47
#>
#> $history[[47]]$train_loss
#> [1] 0
#>
#> $history[[47]]$eval_loss
#> [1] 0
#>
#>
#> $history[[48]]
#> $history[[48]]$epoch
#> [1] 48
#>
#> $history[[48]]$train_loss
#> [1] 0
#>
#> $history[[48]]$eval_loss
#> [1] 0
#>
#>
#> $history[[49]]
#> $history[[49]]$epoch
#> [1] 49
#>
#> $history[[49]]$train_loss
#> [1] 0
#>
#> $history[[49]]$eval_loss
#> [1] 0
#>
#>
#> $history[[50]]
#> $history[[50]]$epoch
#> [1] 50
#>
#> $history[[50]]$train_loss
#> [1] 0
#>
#> $history[[50]]$eval_loss
#> [1] 0
#>
#>
#>
#> $best_epoch
#> [1] 27
#>
#> $estimate
#> [1] 0
#>
#> $n
#> [1] 4
#>
#> $method
#> [1] "Trainer loop (mini-batch SGD with per-epoch evaluation)"
#>