R/bnns_parsnip_helpers.R

Defines functions detect_output_activation translate_out_activation translate_activation

Documented in detect_output_activation translate_activation translate_out_activation

#' Translate parsnip activation name to bnns code
#' @keywords internal
translate_activation <- function(activation) {
  mapping <- c(
    "tanh"     = 1L,
    "sigmoid"  = 2L,
    "softplus" = 3L,
    "relu"     = 4L,
    "linear"   = 5L
  )
  if (is.character(activation)) {
    activation <- tolower(activation)
    if (any(!activation %in% names(mapping))) {
      rlang::abort(paste0(
        "Unknown activation. ",
        "bnns supports: ", paste(names(mapping), collapse = ", ")
      ))
    }
    return(unname(mapping[activation]))
  }
  as.integer(activation)
}

#' Translate output activation name to bnns code
#' @keywords internal
translate_out_activation <- function(out_act_fn) {
  mapping <- c(
    "linear"  = 1L,
    "sigmoid" = 2L,
    "softmax" = 3L
  )
  if (is.character(out_act_fn)) {
    out_act_fn <- tolower(out_act_fn)
    if (any(!out_act_fn %in% names(mapping))) {
      rlang::abort(paste0(
        "Unknown out_act_fn. ",
        "bnns supports: ", paste(names(mapping), collapse = ", ")
      ))
    }
    return(unname(mapping[out_act_fn]))
  }
  as.integer(out_act_fn)
}

#' Auto-detect out_act_fn from response variable
#' @keywords internal
detect_output_activation <- function(y) {
  if (is.numeric(y) && !is.factor(y)) {
    return(1L)  # regression
  }
  lvls <- if (is.factor(y)) levels(y) else unique(y)
  if (length(lvls) == 2) {
    return(2L)  # binary
  }
  return(3L)    # multiclass
}

Try the bnns package in your browser

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

bnns documentation built on June 8, 2026, 1:06 a.m.