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

Feature/early stopping #7

Merged
merged 7 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add a few more tests
  • Loading branch information
dfalbel committed May 12, 2021
commit 01fdf9b7df95a41c86f6e5d0f98384b6a9242099
15 changes: 8 additions & 7 deletions R/callbacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ default_callbacks <- function() {
#' A `luz_callback` that can be passed to [fit.luz_module_generator()].
#' @family luz_callbacks
#' @export
luz_callback <- function(name, ..., private = NULL, active = NULL, parent_env = parent.frame(),
luz_callback <- function(name = NULL, ..., private = NULL, active = NULL, parent_env = parent.frame(),
inherit = NULL) {
make_class(
name = name,
Expand Down Expand Up @@ -306,7 +306,7 @@ luz_callback_early_stopping <- luz_callback(
self$baseline <- baseline

if (!is.null(self$baseline))
private$current_best <- baseline
self$current_best <- baseline

self$patience_counter <- 0L
},
Expand All @@ -333,7 +333,7 @@ luz_callback_early_stopping <- luz_callback(
self$patience_counter <- self$patience_counter + 1L
}

if (self$patience_counter > self$patience) {
if (self$patience_counter >= self$patience) {
rlang::signal("Early stopping", class = "early_stopping")
}

Expand All @@ -358,13 +358,14 @@ luz_callback_early_stopping <- luz_callback(

out
},
compare = function(x, y) {
# returns TRUE when the new is better then previous acording to mode
compare = function(new, old) {
out <- if (self$mode == "min")
x < y
(old - self$min_delta) > new
else if (self$mode == "max")
x > y
(new - self$min_delta) > old
else if (self$mode == "zero")
abs(x) < abs(y)
(abs(old) - self$min_delta) > abs(self$min_delta)

as.array(out)
}
Expand Down
29 changes: 27 additions & 2 deletions tests/testthat/_snaps/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,35 @@
})
Message <message>
Train metrics: Loss: 1.5301
Message <message>
Early stopping at epoch 1 of 25

---

Code
expect_message({
output <- mod %>% set_hparams(input_size = 10, output_size = 1) %>% fit(dl,
verbose = TRUE, epochs = 25, callbacks = list(luz_callback_early_stopping(
monitor = "train_loss", patience = 5, baseline = 0.001)))
})
Message <message>
Train metrics: Loss: 1.4807
Message <message>
Epoch 2/25
Message <message>
Train metrics: Loss: 1.654
Train metrics: Loss: 1.3641
Message <message>
Epoch 3/25
Message <message>
Train metrics: Loss: 1.2073
Message <message>
Epoch 4/25
Message <message>
Train metrics: Loss: 1.2524
Message <message>
Epoch 5/25
Message <message>
Train metrics: Loss: 1.1891
Message <message>
Early stopping at epoch 2 of 25
Early stopping at epoch 5 of 25

24 changes: 24 additions & 0 deletions tests/testthat/test-callbacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ test_that("early stopping", {
})
})

expect_snapshot({
expect_message({
output <- mod %>%
set_hparams(input_size = 10, output_size = 1) %>%
fit(dl, verbose = TRUE, epochs = 25, callbacks = list(
luz_callback_early_stopping(monitor = "train_loss", patience = 5,
baseline = 0.001)
))
})
})

x <- 0
output <- mod %>%
set_hparams(input_size = 10, output_size = 1) %>%
fit(dl, verbose = FALSE, epochs = 25, callbacks = list(
luz_callback_early_stopping(monitor = "train_loss", patience = 5,
baseline = 0.001),
luz_callback(on_early_stopping = function() {
x <<- 1
})()
))

expect_equal(x, 1)
})