Error: ModuleNotFoundError: No module named 'keras'
Run `reticulate::py_last_error()` for details.

test_that("E2E: Customizing main arguments works", {
input_block_feat <- function(model, input_shape) {
dense_block_feat <- function(model, units = 16) {
model |> keras3::layer_dense(units = units, activation = "relu")
# Main arguments (like compile_*) should be set in the spec function,
# This should now run without the parsnip warning about removing arguments
fit_obj <- parsnip::fit(spec, mpg ~ ., data = mtcars)
# Also verify the arguments were correctly used during compilation
# Keras might add suffixes or use different casings, so check flexibly
test_that("E2E: Customizing fit arguments works", {
input_block_fit <- function(model, input_shape) {
dense_block_fit <- function(model, units = 16) {
# Fit arguments (like validation_split, callbacks) should be set in the
fit_callbacks = list(keras3::callback_early_stopping(patience = 1)),
# This will run without error if the arguments are passed correctly
expect_no_error(fit_obj <- parsnip::fit(spec, mpg ~ ., data = mtcars))
# Check that the callback was used (model should stop early)
test_that("E2E: Setting num_blocks = 0 works for sequential models", {
input_block_zero <- function(model, input_shape) {
dense_block_zero <- function(model, units = 16) {
spec <- e2e_mlp_zero(num_dense = 0, fit_epochs = 1) |>
# Check that the dense layer is NOT in the model
keras_model <- fit_obj |> extract_keras_model()
expect_equal(length(keras_model$layers), 1) # Output layers only
layer_names <- sapply(keras_model$layers, function(l) l$name)
test_that("E2E: Error handling for reserved names works", {
compile = function(model) model, # "compile" is a reserved name
dense = function(model, u = 1) model |> keras3::layer_dense(units = u)
regexp = "`compile`, `fit` and `optimizer` are protected names"
spec <- e2e_mlp_summary_test(fit_epochs = 1) |>
spec <- e2e_mlp_history_test(fit_epochs = epochs_to_train) |>
input_block_eval <- function(model, input_shape) {
dense_block_eval <- function(model, units = 16) {
spec <- e2e_mlp_evaluate_test(fit_epochs = 1) |>
eval_output <- keras_evaluate(fit_obj, x = mtcars[, -1], y = mtcars$mpg)