Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion R/mlp.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ keras_mlp <-
{.val {activation}}."
)
}
activation <- get_activation_fn(activation)

if (penalty > 0 & dropout > 0) {
cli::cli_abort("Please use either dropout or weight decay.", call = NULL)
Expand Down Expand Up @@ -351,7 +352,7 @@ mlp_num_weights <- function(p, hidden_units, classes) {
}

allowed_keras_activation <-
c("elu", "exponential", "gelu", "hard_sigmoid", "linear", "relu", "selu",
c("elu", "exponential", "gelu", "hardsigmoid", "linear", "relu", "selu",
"sigmoid", "softmax", "softplus", "softsign", "swish", "tanh")

#' Activation functions for neural networks in keras
Expand All @@ -363,6 +364,13 @@ keras_activations <- function() {
allowed_keras_activation
}

get_activation_fn <- function(arg, ...) {
if (arg == "hardsigmoid") {
arg <- "hard_sigmoid"
}
arg
}

## -----------------------------------------------------------------------------

#' @importFrom purrr map
Expand Down
11 changes: 10 additions & 1 deletion R/tunable.R
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,18 @@ tunable.mlp <- function(x, ...) {
list(list(pkg = "dials", fun = "learn_rate", range = c(-3, -1/2)))
res$call_info[res$name == "epochs"] <-
list(list(pkg = "dials", fun = "epochs", range = c(5L, 500L)))
activation_values <- rlang::eval_tidy(
rlang::call2("brulee_activations", .ns = "brulee")
)
res$call_info[res$name == "activation"] <-
list(list(pkg = "dials", fun = "activation", values = activation_values))
} else if (x$engine == "keras") {
activation_values <- parsnip::keras_activations()
res$call_info[res$name == "activation"] <-
list(list(pkg = "dials", fun = "activation", values = activation_values))
}
res
}
}

#' @export
tunable.survival_reg <- function(x, ...) {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/mlp_keras.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@
Error:
! object 'novar' not found

# all keras activation functions

Code
mlp(mode = "classification", hidden_units = 2, penalty = 0.01, epochs = 2,
activation = "invalid") %>% set_engine("keras", verbose = 0) %>% parsnip::fit(
Class ~ A + B, data = modeldata::two_class_dat)
Condition
Error in `parsnip::keras_mlp()`:
! `activation` should be one of: elu, exponential, gelu, hardsigmoid, linear, relu, selu, sigmoid, softmax, softplus, softsign, swish, and tanh, not "invalid".

1 change: 1 addition & 0 deletions tests/testthat/test-tunable.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that('brulee has mixture object', {
skip_if_not_installed("brulee")
# for issue 1236
mlp_spec <-
mlp(
Expand Down
Loading