#' This function generates parameters for learning rate callbacks defined by user input
#' of number of epochs, the max learning rate willing, tensor used as input for ML or DL model, growth constant, and batch_size
#'
#' @param epochz number of epochs the user plans to set for model they wish to train data on
#' @param max_lr maximum allowed value for learning rate
#' @param training_data A tensor of specific dimension
#' @param growth_constant growth_constant for modeling exponential increase
#' @param process_batch_size number of batches set when training model
#' @return l_rate and n_iter
#' @export
create_lr_rate_parameters_exp <- function(epochz, max_lr, training_data, growth_constant, process_batch_size){
n_iter <- ceiling(epochz * (NROW(training_data)/process_batch_size))
l_rate <- exp(seq(0, growth_constant, length.out = n_iter))
l_rate <- l_rate/max(l_rate)
l_rate <- l_rate * max_lr
parameters <- list(n_iter, l_rate)
return(parameters)
}
#' This function should be used with keras callback_lambda()
#' Use for metrics on each epoch, iteration, batch
#' When training a model, each epoch has 'n' iterations, where each iteration has 'k' batches
#'
#' This will clear the learning rate history and iteration hist
#'
#'
#' @param logs metrics contained in a list generated by R6 class
#' @return iteration information relating to learning rate
#' @export
callback_lr_init <- function(logs){
iter <<- 0
lr_hist <<- c()
iter_hist <<- c()
}
#' This function should be used with keras callback_lambda
#' Use for metrics on each epoch, iteration, batch
#' When training a model, each epoch has 'n' iterations, where each iteration has 'k' batches
#'
#' This function sets the learning rate accordingto the learning for each batch of data.
#'
#' @param batch created from keras callbacks, logs 'which batch'
#' @param logs metrics contained in a list generated by R6 class
#' @return updated iter, LR
#' @export
callback_lr_set <- function(batch, logs){
iter <<- iter + 1
LR <- l_rate[iter]
if(is.na(LR)) LR <- l_rate[length(l_rate)]
k_set_value(cnn_model$optimizer$lr, LR)
}
#' This function should be used with keras callback_lambda
#' Use for metrics on each epoch, iteration, batch
#' When training a model, each epoch has 'n' iterations, where each iteration has 'k' batches
#'
#' This function logs the learning rate value and iteration number for _hist in global enviroment
#'
#' @param batch created from keras callbacks, logs 'which batch'
#' @param logs metrics contained in a list generated by R6 class
#' @return lists for lr_hist and iter_hist
#' @export
callback_lr_log <- function(batch, logs){
lr_hist <<- c(lr_hist, k_get_value(cnn_model$optimizer$lr))
iter_hist <<- c(iter_hist, k_get_value(cnn_model$optimizer$iterations))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.