Skip to content

callback function error! #1569

@Daniel-Trung-Nguyen

Description

@Daniel-Trung-Nguyen

I was trying to apply the early stopping example for a regression problem and got this error:

[ERROR] [2022-08-18 04:33:27,482:Client-AutoML(1):botanal] Cannot register callback of type <class 'function'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/autosklearn/automl.py", line 931, in fit
    _proc_smac.run_smbo()
  File "/usr/local/lib/python3.7/dist-packages/autosklearn/smbo.py", line 496, in run_smbo
    smac.register_callback(self.trials_callback)
  File "/usr/local/lib/python3.7/dist-packages/smac/facade/smac_ac_facade.py", line 850, in register_callback
    raise ValueError("Cannot register callback of type %s" % type(callback))
ValueError: Cannot register callback of type <class 'function'>
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-30-b2f7d005a0d7>](https://localhost:8080/#) in <module>
      8                                                         get_trials_callback=callback
      9                                                     )
---> 10 automl.fit(X_train, y_train, dataset_name="botanal")

5 frames
[/usr/local/lib/python3.7/dist-packages/smac/facade/smac_ac_facade.py](https://localhost:8080/#) in register_callback(self, callback)
    848                 break
    849         if key is None:
--> 850             raise ValueError("Cannot register callback of type %s" % type(callback))
    851         self.solver._callbacks[key].append(callback)

ValueError: Cannot register callback of type <class 'function'>
from smac.optimizer.smbo import SMBO
from smac.runhistory.runhistory import RunInfo, RunValue
def callback(
    smbo: SMBO,
    run_info: RunInfo,
    result: RunValue,
    time_left: float):
    """Stop early if we get a very low cost value for a single run
    The return value indicates to SMAC whether to stop or not. False will
    stop the search process while any other value will mean it continues.
    """
    # You can find out the parameters in the SMAC documentation
    # https://automl.github.io/SMAC3/main/
    if result.cost <= 0.02:
        print("Stopping!")
        print(run_info)
        print(result)
        return False

automl = autosklearn.regression.AutoSklearnRegressor(time_left_for_this_task=120,
                                                        per_run_time_limit=30,
                                                        tmp_folder="/tmp/autosklearn_regression_example_tmp",
                                                        get_trials_callback=callback
                                                    )
automl.fit(X_train, y_train, dataset_name="botanal")

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions