facebookresearch / nevergrad

A Python toolbox for performing gradient-free optimization

Home Page:https://facebookresearch.github.io/nevergrad/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When using NGOpt, Print the selected Optimizer

kayuksel opened this issue · comments

Hello everyone, is there an easy way of printing the name of the utilized optimizer for the problem, when using any of the NGOpt?

Try this:

import nevergrad as ng

optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100)
print(optimizer.optim)

Result:

Instance of MetaModel(parametrization=Array{(2,)}, budget=100, num_workers=1)

Note that some of the multi-choice optimizers (e.g. CMandAS3) have multiple optimizers, which are accessed via "optims" (plural) instead.

Hello again,

It gives me the following error: '_DE' object has no attribute 'optim'

My code is as follows. I am optimizing using every optimizer available.

I guess that optim only exists for combined optimizers like NGOpt?

for optimizer_name in [x for x in dir(ng.optimization.optimizerlib) if x[0].isupper() and not x.startswith("_")]:
    try:
        if optimizer_name in ['BO', 'BOSplit', 'EDA', 'MEDA', 'HyperOpt', 'MPCEDA', 'PCEDA']: continue
        best_reward = 1000000000000000000
        optimizer_class = getattr(ng.optimization.optimizerlib, optimizer_name)
        optimizer = optimizer_class(parametrization=ng.p.Array(shape=(len(assets,),)), budget=60)
        x = optimizer.minimize(train).value
        print('%s %f' % (optimizer_name, test(x)))
    except Exception as e:
        print(e)
        continue

You are correct. "optim" and "optims" only work for combined optimizers.
For all optimizers you can use optimizer.name:

import nevergrad as ng

optimizer = ng.optimizers.Cobyla(parametrization=2, budget=100)
print(optimizer.name)

Result:
Cobyla

Note that this will return "NGOpt" for NGOpt instead of the optimizer that it selects.

Thank you.