R/utils.R

Defines functions py_load_class string_to_function

#' Auxiliar function used to transform some of the used activation functions
#' in the neural network from their string name into an R function.
#'
#' @param af_string_list List of strings containing the predefined possible
#' activation functions, i.e., "softplus", "tanh", "sigmoid" or "linear".
#'
#' @return List of R functions associated with the string names provided.
#'
#' @noRd
string_to_function <- function(af_string_list) {
  lapply(af_string_list, function(i) {
    switch(
      i,
      "softplus" = function(x) log(1 + exp(x)),
      "tanh"     = function(x) tanh(x),
      "sigmoid"  = function(x) 1 / (1 + exp(-x)),
      "linear"   = function(x) x,
      stop("Function '", i, "' not supported", call.=FALSE)
    )
  })
}

# Auxiliar function used in keras constraints and callbacks as Python is needed.
py_load_class <- function(name) {
  file <- system.file(paste0("python/", name, ".py"), package="nn2poly")
  reticulate::py_run_file(file)[name]
}

Try the nn2poly package in your browser

Any scripts or data that you put into this service are public.

nn2poly documentation built on May 29, 2024, 5:08 a.m.