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

NGOpt does not provide a way to ask for picklability

vadim0x60 opened this issue · comments

Steps to reproduce

  1. Create NGOpt optimizer, in my case NGOpt(150, 3000)
  2. Train it for several iterations
  3. Try to save it to a file with optimizer.dump()

Observed Results

Traceback (most recent call last):
  File "opt.py", line 210, in <module>
    checkpoint(final=True)
  File "opt.py", line 202, in checkpoint
    optimizer.dump(optimizer_path)
  File "/home/mcs001/20194474/.local/lib/python3.7/site-packages/nevergrad/optimization/base.py", line 241, in dump
    pickle.dump(self, f)
  File "/home/mcs001/20194474/.local/lib/python3.7/site-packages/nevergrad/optimization/recaster.py", line 296, in __getstate__
    raise ValueError("If you want picklability you should have asked for it")
ValueError: If you want picklability you should have asked for it

But how do I ask for it? I couldn't find any such setting in NGOpt. The error occurs in SequentialRecastOptimizer class which has an .enable_pickling() method, NGOpt has no such method and I don't understand how we get from NGOpt to SequentialRecastOptimizer.

Expected Results

A file that can be used to continue training with optimizer.load()

Relevant Code

Abridged code:

optimizer = OPTIMIZER(parametrization=ng.p.Array(shape=(150,), lower=-6, upper=6), budget=3000)
  
try:
    for idx in range(optimizer.budget):
        candidate = optimizer.ask()
        fitness = evaluate_candidate(candidate)
        optimizer.tell(candidate, - fitness)
        if idx % CHECKPOINT_INTERVAL == 0:
            optimizer.dump(optimizer_path)
finally:
    optimizer.dump(optimizer_path)

See unabridged code in my repo

For the moment, I think you can add the following after the optimizer has been created.

if(isinstance(optimizer, ng.optimizers.NGOptBase) and isinstance(optimizer.optim, ng.optimizers.recaster.SequentialRecastOptimizer)):
    optimizer.optim.enable_pickling()

Thanks, this worked!
UPD: not for all budgets

That commit added a function enable_pickling on all optimizers, so you could just call optimizer.enable_pickling after the optimizer has been created (e.g. from the next version).