SMAC3 icon indicating copy to clipboard operation
SMAC3 copied to clipboard

How to save the Surrogate Model

Open fang-tech opened this issue 1 year ago • 1 comments

Description

I want to use SMAC for "Database Tuning", and my teacher need I submit the model file, but I can't find how to save and load the model in SMAC. My Surrogate Model choose random_forst. I just find the runHistory in Documentation.

Code

objective_function = Objective().set_trials(30)

    scenario = Scenario(
        objective_function.configspace,
        n_trials=objective_function.get_trials(),
    )

    rf_r = RandomForest(configspace=objective_function.configspace)

    smac = HyperparameterOptimizationFacade(
        scenario,
        objective_function.train,
        overwrite=True,
        model=rf_r
    )

    incumbent = smac.optimize()

Versions

2.1.0

fang-tech avatar Jun 21 '24 14:06 fang-tech

Hi @fang-tech (I hope the answer is not too late) there are two ways: (1) you could pickle the random forest and reload it. That requires that the person reloading it needs to have the same packages installed. (2) You could read in the evaluated configurations and retrain the RF

objective_function = Objective().set_trials(30)

scenario = Scenario(
    objective_function.configspace,
    n_trials=objective_function.get_trials(),
)

rf_r = RandomForest(configspace=objective_function.configspace)

smac = HyperparameterOptimizationFacade(
    scenario,
    objective_function.train,
    overwrite=True,
    model=rf_r
)

# reload data
smac._runhistory.load(FILENAME_TO_RUNHISTORY)

# retrain
config_selector = smac._config_selector
X, Y, X_configurations = config_selector ._collect_data()
config_selector ._model.train(X, Y)
model = config_selector._model

This should work but is untested.

benjamc avatar Jul 16 '24 09:07 benjamc

I will close this issue due to inactivity, feel free to reopen! :sunflower:

benjamc avatar Nov 27 '24 14:11 benjamc