|
| 1 | +test_that("E2E: Customizing main arguments works", { |
| 2 | + skip_if_no_keras() |
| 3 | + |
| 4 | + input_block_feat <- function(model, input_shape) { |
| 5 | + keras3::keras_model_sequential(input_shape = input_shape) |
| 6 | + } |
| 7 | + dense_block_feat <- function(model, units = 16) { |
| 8 | + model |> keras3::layer_dense(units = units, activation = "relu") |
| 9 | + } |
| 10 | + output_block_feat <- function(model) { |
| 11 | + model |> keras3::layer_dense(units = 1) |
| 12 | + } |
| 13 | + |
| 14 | + create_keras_spec( |
| 15 | + model_name = "e2e_mlp_feat", |
| 16 | + layer_blocks = list( |
| 17 | + input = input_block_feat, |
| 18 | + dense = dense_block_feat, |
| 19 | + output = output_block_feat |
| 20 | + ), |
| 21 | + mode = "regression" |
| 22 | + ) |
| 23 | + |
| 24 | + # Main arguments (like compile_*) should be set in the spec function, |
| 25 | + # not in set_engine(). |
| 26 | + spec <- e2e_mlp_feat( |
| 27 | + epochs = 2, |
| 28 | + compile_optimizer = "sgd", |
| 29 | + compile_loss = "mae", |
| 30 | + compile_metrics = c("mean_squared_error") |
| 31 | + ) |> |
| 32 | + parsnip::set_engine("keras") |
| 33 | + |
| 34 | + # This should now run without the parsnip warning about removing arguments |
| 35 | + fit_obj <- NULL |
| 36 | + expect_no_warning( |
| 37 | + fit_obj <- parsnip::fit(spec, mpg ~ ., data = mtcars) |
| 38 | + ) |
| 39 | + |
| 40 | + # Also verify the arguments were correctly used during compilation |
| 41 | + keras_model <- fit_obj$fit$fit |
| 42 | + compiled_loss <- keras_model$loss |
| 43 | + compiled_optimizer <- tolower(keras_model$optimizer$name) |
| 44 | + compiled_metrics <- sapply( |
| 45 | + keras_model$metrics[[2]]$metrics, |
| 46 | + function(m) { |
| 47 | + m$name |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + # Keras might add suffixes or use different casings, so check flexibly |
| 52 | + expect_true(grepl("mae", compiled_loss)) |
| 53 | + expect_true(grepl("sgd", tolower(compiled_optimizer))) |
| 54 | + expect_true("mean_squared_error" %in% compiled_metrics) |
| 55 | +}) |
| 56 | + |
| 57 | +test_that("E2E: Customizing fit arguments works", { |
| 58 | + skip_if_no_keras() |
| 59 | + |
| 60 | + input_block_fit <- function(model, input_shape) { |
| 61 | + keras3::keras_model_sequential(input_shape = input_shape) |
| 62 | + } |
| 63 | + dense_block_fit <- function(model, units = 16) { |
| 64 | + model |> keras3::layer_dense(units = units, activation = "relu") |
| 65 | + } |
| 66 | + output_block_fit <- function(model) { |
| 67 | + model |> keras3::layer_dense(units = 1) |
| 68 | + } |
| 69 | + |
| 70 | + create_keras_spec( |
| 71 | + model_name = "e2e_mlp_fit", |
| 72 | + layer_blocks = list( |
| 73 | + input = input_block_fit, |
| 74 | + dense = dense_block_fit, |
| 75 | + output = output_block_fit |
| 76 | + ), |
| 77 | + mode = "regression" |
| 78 | + ) |
| 79 | + |
| 80 | + # Fit arguments (like validation_split, callbacks) should be set in the |
| 81 | + # spec function, not in set_engine(). |
| 82 | + spec <- e2e_mlp_fit( |
| 83 | + fit_validation_split = 0.2, |
| 84 | + fit_callbacks = list(keras3::callback_early_stopping(patience = 1)), |
| 85 | + fit_epochs = 3, |
| 86 | + compile_metrics = "mean_squared_error" |
| 87 | + ) |> |
| 88 | + parsnip::set_engine("keras") |
| 89 | + |
| 90 | + # This will run without error if the arguments are passed correctly |
| 91 | + fit_obj <- NULL |
| 92 | + expect_no_error(fit_obj <- parsnip::fit(spec, mpg ~ ., data = mtcars)) |
| 93 | + |
| 94 | + # Check that the callback was used (model should stop early) |
| 95 | + expect_lt(length(fit_obj$fit$history$metrics$loss), 5) |
| 96 | +}) |
| 97 | + |
| 98 | +test_that("E2E: Setting num_blocks = 0 works", { |
| 99 | + skip_if_no_keras() |
| 100 | + |
| 101 | + input_block_zero <- function(model, input_shape) { |
| 102 | + keras3::keras_model_sequential(input_shape = input_shape) |
| 103 | + } |
| 104 | + dense_block_zero <- function(model, units = 16) { |
| 105 | + model |> keras3::layer_dense(units = units, activation = "relu") |
| 106 | + } |
| 107 | + output_block_zero <- function(model) { |
| 108 | + model |> keras3::layer_dense(units = 1) |
| 109 | + } |
| 110 | + |
| 111 | + create_keras_spec( |
| 112 | + model_name = "e2e_mlp_zero", |
| 113 | + layer_blocks = list( |
| 114 | + input = input_block_zero, |
| 115 | + dense = dense_block_zero, |
| 116 | + output = output_block_zero |
| 117 | + ), |
| 118 | + mode = "regression" |
| 119 | + ) |
| 120 | + |
| 121 | + spec <- e2e_mlp_zero(num_dense = 0, epochs = 2) |> |
| 122 | + parsnip::set_engine("keras") |
| 123 | + # This should fit a model with only an input and output layer |
| 124 | + expect_no_error(parsnip::fit(spec, mpg ~ ., data = mtcars)) |
| 125 | +}) |
| 126 | + |
| 127 | +test_that("E2E: Error handling for reserved names works", { |
| 128 | + bad_blocks <- list( |
| 129 | + compile = function(model) model, # "compile" is a reserved name |
| 130 | + dense = function(model, u = 1) model |> keras3::layer_dense(units = u) |
| 131 | + ) |
| 132 | + |
| 133 | + expect_error( |
| 134 | + create_keras_spec("bad_spec", bad_blocks), |
| 135 | + regexp = "`compile` and `optimizer` are protected names" |
| 136 | + ) |
| 137 | +}) |
0 commit comments