Search code examples
pythonmachine-learningvalueerrornevergrad

Suggesting Values in Nevergrad Package


Steps to reproduce

import nevergrad as ng
import numpy as np

loc = ng.p.Scalar(lower=-5,upper=5)
scale = ng.p.Scalar(lower=0, upper=5)
s = ng.p.Scalar(lower=0, upper=10)
k = ng.p.Choice(list(range(2,6)))
w = ng.p.Array(shape=(self.times.shape[0],)).set_bounds(-10,10)
instru = ng.p.Instrumentation(loc=loc,
                          scale = scale,
                          s=s,
                          k=k,
                          w = w)
optimizer = ng.optimizers.DE(parametrization=instru,
                                      budget=budget)
optimizer.suggest((),{'k':3,'loc':-2,'s':2,'scale':2,'w':np.ones(self.times.shape[0])})

Observed Results

ValueError: Tuple value must be a tuple of size 0, got: ((), {'k': 3, 'loc': -2, 's': 2, 'scale': 2, 'w': array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1.])}).
Current value: ()

Expected Results

For initial values to be set in an optimizer run

Has anyone had success using the suggest method in Nevergrad?

If so, would you mind copying/pasting working code? I've been trying different forms of the example in the documentation, but cannot seem to get it to work.


Solution

  • The question was answered in a relevant Github thread:

    Basically, suggest should be called the same way as the function to optimize, in your case, given you are using an Instrumentation, I guess it should be:

    optimizer.suggest(k=3, loc=-2, s=2, scale=2, w=mp.ones(self.times.shape[0]))
    

    Another option, which can work for all but the Choice parameter would be to use the init option of Array and Scalar (eg: loc = ng.p.Scalar(init=-2, lower=-5, upper=5))