Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheduler reduce on plateau #140

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion R/callbacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,12 @@ luz_callback_lr_scheduler <- luz_callback(
lr_scheduler(optimizer, ...)
}
self[[call_on]] <- function() {
self$scheduler$step()
if ("metrics" %in% names(formals(self$scheduler$step))) {
current_loss <- ctx$loss[[self$opt_name]]
self$scheduler$step(current_loss)
} else {
self$scheduler$step()
}
}
self$opt_name <- opt_name
},
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/_snaps/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
Adjusting learning rate of group 1 to 0.0001
Adjusting learning rate of group 1 to 0.0000

---

Code
expect_message({
output <- mod %>% set_hparams(input_size = 10, output_size = 1) %>% fit(dl,
verbose = FALSE, epochs = 20, callbacks = list(luz_callback_lr_scheduler(
torch::lr_reduce_on_plateau, verbose = TRUE, patience = 2, threshold = 0.1)))
})
Message
Epoch 7: reducing learning rate of group 1 to 1.0000e-05
Epoch 10: reducing learning rate of group 1 to 1.0000e-06
Epoch 13: reducing learning rate of group 1 to 1.0000e-07
Epoch 16: reducing learning rate of group 1 to 1.0000e-08

# progressbar appears with training and validation

Code
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-callbacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ test_that("callback lr scheduler", {
})
})

expect_snapshot({
expect_message({
output <- mod %>%
set_hparams(input_size = 10, output_size = 1) %>%
fit(dl, verbose = FALSE, epochs = 20, callbacks = list(
luz_callback_lr_scheduler(
torch::lr_reduce_on_plateau,
verbose = TRUE,
patience = 2,
threshold = 1e-1
)
))
})
})

})

test_that("csv callback", {
Expand Down
Loading