ARM-software / mango

Parallel Hyperparameter Tuning in Python
Apache License 2.0
335 stars 40 forks source link

Negative domain error #79

Closed btyukodi closed 1 year ago

btyukodi commented 1 year ago

I'm trying to find the minimum of x*x+5*x. I'm using the code below:

#mangotest.py

from mango import Tuner, scheduler
from scipy.stats import uniform

# search space
param_space = dict(x=uniform(-10, -1))

@scheduler.parallel(n_jobs=1)
def objective(x):
    return (x * x + 5*x)

tuner = Tuner(param_space, objective)  
results = tuner.minimize()  

However, I'm getting the following error:

Traceback (most recent call last):
  File "mangotest.py", line 16, in <module>
    results = tuner.minimize()  
  File "/usr/local/lib/python3.8/dist-packages/mango/tuner.py", line 153, in minimize
    return self.run()
  File "/usr/local/lib/python3.8/dist-packages/mango/tuner.py", line 140, in run
    self.results = self.runBayesianOptimizer()
  File "/usr/local/lib/python3.8/dist-packages/mango/tuner.py", line 182, in runBayesianOptimizer
    X_list, Y_list, X_tried = self.run_initial()
  File "/usr/local/lib/python3.8/dist-packages/mango/tuner.py", line 162, in run_initial
    X_tried = self.ds.get_random_sample(self.config.initial_random)
  File "/usr/local/lib/python3.8/dist-packages/mango/domain/domain_space.py", line 49, in get_random_sample
    domain_list = list(BatchParameterSampler(self.param_dict, n_iter=size))
  File "/usr/local/lib/python3.8/dist-packages/mango/domain/batch_parameter_sampler.py", line 61, in __iter__
    samples.append(v.rvs(random_state=rng, size=self.n_iter))
  File "/usr/local/lib/python3.8/dist-packages/scipy/stats/_distn_infrastructure.py", line 467, in rvs
    return self.dist.rvs(*self.args, **kwds)
  File "/usr/local/lib/python3.8/dist-packages/scipy/stats/_distn_infrastructure.py", line 1066, in rvs
    raise ValueError(message)
ValueError: Domain error in arguments. The `scale` parameter must be positive for all distributions, and many distributions have restrictions on shape parameters. Please see the `scipy.stats.uniform` documentation for details.

Interestingly, there is no error if I set param_space = dict(x=uniform(-10, 0)) so I'm guessing there is an issue with both limits of the search space being negative. Is this the intended behavior? If so, why? Thank you!

tihom commented 1 year ago

Hi @btyukodi the uniform distribution's second parameter is the scale or the range of the distribution. So to have a uniform distribution between -10 and -1 you need to use uniform(-10, -9). From the scipy docs:

A uniform continuous random variable.

In the standard form, the distribution is uniform on [0, 1]. Using the parameters loc and scale, one obtains the uniform distribution on [loc, loc + scale].
sandeep-iitr commented 1 year ago

Thanks, Mohit, for looking into this. I am closing the issue for now. Maybe, we can update the readme if you think it will better clarify the domain definition using distributions.