|
| 1 | +#' @title Unfreezing Weights Callback |
| 2 | +#' |
| 3 | +#' @name mlr_callback_set.unfreeze |
| 4 | +#' |
| 5 | +#' @description |
| 6 | +#' Unfreeze some weights (parameters of the network) after some number of steps or epochs. |
| 7 | +#' |
| 8 | +#' @param starting_weights (`Select`)\cr |
| 9 | +#' A `Select` denoting the weights that are trainable from the start. |
| 10 | +#' @param unfreeze (`data.table`)\cr |
| 11 | +#' A `data.table` with a column `weights` (a list column of `Select`s) and a column `epoch` or `batch`. |
| 12 | +#' The selector indicates which parameters to unfreeze, while the `epoch` or `batch` column indicates when to do so. |
| 13 | +#' |
| 14 | +#' @family Callback |
| 15 | +#' @export |
| 16 | +#' @include CallbackSet.R |
| 17 | +CallbackSetUnfreeze = R6Class("CallbackSetUnfreeze", |
| 18 | + inherit = CallbackSet, |
| 19 | + lock_objects = FALSE, |
| 20 | + public = list( |
| 21 | + #' @description |
| 22 | + #' Creates a new instance of this [R6][R6::R6Class] class. |
| 23 | + initialize = function(starting_weights, unfreeze) { |
| 24 | + self$starting_weights = starting_weights |
| 25 | + self$unfreeze = unfreeze |
| 26 | + private$.batchwise = "batch" %in% names(self$unfreeze) |
| 27 | + }, |
| 28 | + #' @description |
| 29 | + #' Sets the starting weights |
| 30 | + on_begin = function() { |
| 31 | + trainable_weights = self$starting_weights(names(self$ctx$network$parameters)) |
| 32 | + walk(self$ctx$network$parameters[trainable_weights], function(param) param$requires_grad_(TRUE)) |
| 33 | + frozen_weights = select_invert(self$starting_weights)(names(self$ctx$network$parameters)) |
| 34 | + walk(self$ctx$network$parameters[frozen_weights], function(param) param$requires_grad_(FALSE)) |
| 35 | + |
| 36 | + frozen_weights_str = paste(trainable_weights, collapse = ", ") |
| 37 | + lg$info(paste0("Training the following weights at the start: ", trainable_weights)) |
| 38 | + }, |
| 39 | + #' @description |
| 40 | + #' Unfreezes weights if the training is at the correct epoch |
| 41 | + on_epoch_begin = function() { |
| 42 | + if (!private$.batchwise) { |
| 43 | + if (self$ctx$epoch %in% self$unfreeze$epoch) { |
| 44 | + weights = (self$unfreeze[get("epoch") == self$ctx$epoch]$weights)[[1]](names(self$ctx$network$parameters)) |
| 45 | + if (!length(weights)) { |
| 46 | + lg$warn(paste0("No weights unfrozen at epoch ", self$ctx$epoch, " , check the specification of the Selector")) |
| 47 | + } else { |
| 48 | + walk(self$ctx$network$parameters[weights], function(param) param$requires_grad_(TRUE)) |
| 49 | + weights_str = paste(weights, collapse = ", ") |
| 50 | + lg$info(paste0("Unfreezing at epoch ", self$ctx$epoch, ": ", weights_str)) |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + }, |
| 56 | + #' @description |
| 57 | + #' Unfreezes weights if the training is at the correct batch |
| 58 | + on_batch_begin = function() { |
| 59 | + if (private$.batchwise) { |
| 60 | + batch_num = (self$ctx$epoch - 1) * length(self$ctx$loader_train) + self$ctx$step |
| 61 | + if (batch_num %in% self$unfreeze$batch) { |
| 62 | + weights = (self$unfreeze[get("batch") == batch_num]$weights)[[1]](names(self$ctx$network$parameters)) |
| 63 | + if (!length(weights)) { |
| 64 | + lg$warn(paste0("No weights unfrozen at batch ", batch_num, " , check the specification of the Selector")) |
| 65 | + } else { |
| 66 | + walk(self$ctx$network$parameters[weights], function(param) param$requires_grad_(TRUE)) |
| 67 | + weights_str = paste(weights, collapse = ", ") |
| 68 | + lg$info(paste0("Unfreezing at batch ", batch_num, ": ", weights_str)) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + ) |
| 74 | +) |
| 75 | + |
| 76 | +#' @include TorchCallback.R |
| 77 | +mlr3torch_callbacks$add("unfreeze", function() { |
| 78 | + TorchCallback$new( |
| 79 | + callback_generator = CallbackSetUnfreeze, |
| 80 | + param_set = ps( |
| 81 | + starting_weights = p_uty( |
| 82 | + tags = c("train", "required"), |
| 83 | + custom_check = function(input) check_class(input, "Select") |
| 84 | + ), |
| 85 | + unfreeze = p_uty( |
| 86 | + tags = c("train", "required"), |
| 87 | + custom_check = check_unfreeze_dt |
| 88 | + ) |
| 89 | + ), |
| 90 | + id = "unfreeze", |
| 91 | + label = "Unfreeze", |
| 92 | + man = "mlr3torch::mlr_callback_set.unfreeze" |
| 93 | + ) |
| 94 | +}) |
| 95 | + |
| 96 | +check_unfreeze_dt = function(x) { |
| 97 | + if (is.null(x) || (is.data.table(x) && nrow(x) == 0)) { |
| 98 | + return(TRUE) |
| 99 | + } |
| 100 | + if (!test_class(x, "data.table")) { |
| 101 | + return("`unfreeze` must be a data.table()") |
| 102 | + } |
| 103 | + if (!test_names(names(x), must.include = "weights")) { |
| 104 | + return("Must contain 2 columns: `weights` and (epoch or batch)") |
| 105 | + } |
| 106 | + if (!xor("epoch" %in% names(x), "batch" %in% names(x))) { |
| 107 | + return("Exactly one of the columns must be named 'epoch' or 'batch'") |
| 108 | + } |
| 109 | + xs = x[["epoch"]] %??% x[["batch"]] |
| 110 | + if (!test_integerish(xs, lower = 0L) || anyDuplicated(xs)) { |
| 111 | + return("Column batch/epoch must be a positive integerish vector without duplicates.") |
| 112 | + } |
| 113 | + if (!test_list(x$weights)) { |
| 114 | + return("The `weights` column should be a list") |
| 115 | + } |
| 116 | + if (some(x$weights, function(input) !test_class(input, classes = "Select"))) { |
| 117 | + return("The `weights` column should be a list of Selects") |
| 118 | + } |
| 119 | + return(TRUE) |
| 120 | +} |
0 commit comments