Search code examples
pythonscikit-learnregressionsupervised-learningparticle-swarm

How to use SwarmPackagePy in regression in python code of sklearn?


I'm able to draw a 3D animation of Firefly algorithm using SwarmPackagePy library.

3D animation of Firefly algoirthm

I want to use this algorithm to optimize the hyperparameter in Gaussian Process Regression(GPR). For this, I defined the optimizer of GPR as:

alh = SwarmPackagePy.fa(50, tf.easom_function, 0, 16, 2, 10, 1, 1, 1, 0.1, 0, 0.1)

animation3D(alh.get_agents(),tf.easom_function, 10,-10)

Then I used this optimizer (alh) in GPR as follows:

gp = GaussianProcessRegressor(kernel=kernel, alpha=1.5, optimizer=alh, n_restarts_optimizer=5)

However, after running the python code, I get an error as follows:

ValueError: Unknown optimizer <SwarmPackagePy.fa.fa object at 0x0982A3B0>.

Am I doing the wrong way? What could be the cause of the error?

Thank you!


Solution

  • As the documentation of sklearn says, optimizer parameter expects a callable. However, SwarmPackagePy.fa is not a callable. Because it is neither a method, nor a class that implements __call__ method as can be seen from here:

    https://github.com/SISDevelop/SwarmPackagePy/blob/master/SwarmPackagePy/fa.py

    You can write your own __call__ method in that file, by using the same signature as:

    def __call__(obj_func, initial_theta, bounds):
    # you need to write
    # * 'obj_func' is the objective function to be maximized, which
    #   takes the hyperparameters theta as parameter and an
    #   optional flag eval_gradient, which determines if the
    #   gradient is returned additionally to the function value
    # * 'initial_theta': the initial value for theta, which can be
    #   used by local optimizers
    # * 'bounds': the bounds on the values of theta
    ....
    # Returned are the best found hyperparameters theta and
    # the corresponding value of the target function.
        return theta_opt, func_min