Sarim Zafar
06/30/2025, 8:49 AMdef save_best_callback(study: optuna.study.Study, trial: optuna.trial.FrozenTrial):
"""
Callback to save the AutoMLForecast object when a new best trial is found.
"""
# Check if the current trial is the best one so far
if study.best_trial.number == trial.number:
print(f"\n:tada: New best trial found; Saving it now! Trial #{trial.number}")
# The 'auto_mlf' object is accessible from the parent scope
auto_mlf.save('AutoLightGBM')
horizon = 31
my_lgb = AutoModel(
model=lgb.LGBMRegressor(),
config=my_lgb_config,
)
auto_mlf = AutoMLForecast(
models={'lgb':my_lgb},
freq='1D',
season_length=7,
init_config=my_init_config,
fit_config = my_fit_config,
num_threads=3
)
auto_mlf.fit(
processed_df,
n_windows=12,
h=horizon,
num_samples=1000, # number of trials to run
loss=custom_loss,
optimize_kwargs={'callbacks': [save_best_callback]}
)
Max (Nixtla)
07/01/2025, 9:37 AMdef save_best_callback(study: optuna.study.Study, trial: optuna.trial.FrozenTrial):
"""
Callback to save the AutoMLForecast object when a new best trial is found.
"""
# Check if the current trial is the best one so far
if study.best_trial.number == trial.number:
print(f"\n:tada: New best trial found; Saving it now! Trial #{trial.number}")
# The 'auto_mlf' object is accessible from the parent scope
auto_mlf.save('AutoLightGBM')
horizon = 31
my_lgb = AutoModel(
model=lgb.LGBMRegressor(),
config=my_lgb_config,
)
auto_mlf = AutoMLForecast(
models={'lgb':my_lgb},
freq='1D',
season_length=7,
init_config=my_init_config,
fit_config = my_fit_config,
num_threads=3
)
auto_mlf.fit(
processed_df,
n_windows=12,
h=horizon,
num_samples=1000, # number of trials to run
loss=custom_loss,
optimize_kwargs={'callbacks': [save_best_callback]}
)
Ask Inkeep
07/01/2025, 9:37 AMAsk Inkeep
07/01/2025, 9:37 AMAutoMLForecast
class (1) . The issue with your callback approach is that during the optimization process, the AutoMLForecast
object hasn't completed its training yet, so calling save()
within the callback won't save a fully trained model .
The AutoMLForecast
class has a save
method that works after the optimization is complete :
AutoMLForecast.save (path:Union[str,pathlib.Path])
The proper workflow is to save the AutoMLForecast
object after the fit
method completes :
auto_mlf.fit(
processed_df,
n_windows=12,
h=horizon,
num_samples=1000,
loss=custom_loss,
optimize_kwargs={'callbacks': [save_best_callback]}
)
# Save after optimization is complete
auto_mlf.save('path/to/save/directory')
The AutoMLForecast.fit
method carries out the optimization process and trains the best model on all data . Only after this process is complete will you have a fully trained model that can be meaningfully saved .
If you need to track progress during optimization, consider using Optuna's built-in study persistence features or logging mechanisms instead of trying to save incomplete model states .Ask Inkeep
07/01/2025, 9:37 AMAsk Inkeep
07/01/2025, 10:11 AMSarim Zafar
07/01/2025, 10:12 AMMax (Nixtla)
07/01/2025, 1:09 PMMax (Nixtla)
07/01/2025, 1:10 PMAsk Inkeep
07/01/2025, 1:10 PMAsk Inkeep
07/01/2025, 1:11 PMAutoMLForecast
class, there isn't a way to save the model during the optimization process as you've attempted (1) .
The trained models aren't accessible during the callback execution because the optimization process hasn't completed yet . The AutoMLForecast.save
method only works after the entire fit
process is finished .
During optimization, the individual trial models are temporary and not stored in the main AutoMLForecast
object until the process completes and the best model is retrained on all data .Ask Inkeep
07/01/2025, 1:11 PMSarim Zafar
07/04/2025, 1:25 PMMax (Nixtla)
07/04/2025, 3:01 PMOlivier
07/04/2025, 3:37 PMMax (Nixtla)
07/04/2025, 3:49 PM