View source: R/remove_keras_spec.R
| remove_keras_spec | R Documentation |
This function completely removes a model specification that was previously
created by create_keras_sequential_spec() or create_keras_functional_spec().
It cleans up both the function in the user's environment and all associated
registrations within the parsnip package.
remove_keras_spec(model_name, env = parent.frame())
model_name |
A character string giving the name of the model specification function to remove (e.g., "my_mlp"). |
env |
The environment from which to remove the function and its |
This function is essential for cleanly unloading a dynamically created model. It performs three main actions:
It removes the model specification function (e.g., my_mlp()) and its
corresponding update() method from the specified environment.
It searches parsnip's internal model environment for all objects
whose names start with the model_name and removes them. This purges
the fit methods, argument definitions, and other registrations.
It removes the model's name from parsnip's master list of models.
This function uses the un-exported get_model_env() to perform
the cleanup.
Invisibly returns TRUE after attempting to remove the objects.
create_keras_sequential_spec(), create_keras_functional_spec()
if (requireNamespace("keras3", quietly = TRUE)) {
# First, create a dummy spec
input_block <- function(model, input_shape) {
keras3::keras_model_sequential(input_shape = input_shape)
}
dense_block <- function(model, units = 16) {
model |> keras3::layer_dense(units = units)
}
create_keras_sequential_spec(
"my_temp_model",
list(
input = input_block,
dense = dense_block
),
"regression"
)
# Check it exists in the environment and in parsnip
exists("my_temp_model")
"my_temp_model" %in% parsnip::show_engines("my_temp_model")$model
# Now remove it
remove_keras_spec("my_temp_model")
# Check it's gone
!exists("my_temp_model")
!model_exists("my_temp_model")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.