Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning thrown when using register_cheap_constraint with lambda function #1501

Closed
SH2282000 opened this issue Feb 28, 2023 · 2 comments
Closed

Comments

@SH2282000
Copy link

Steps to reproduce

When finding the minimum x that fulfill a specific constraint: if this last constraint is defined as a lambda function, the following warning is thrown:

[...].venv/lib/python3.8/site-packages/nevergrad/parametrization/core.py:291: UserWarning: Lambda as constraint is not advised because it may not be picklable.
  warnings.warn("Lambda as constraint is not advised because it may not be picklable.")

Therefore, I replaced my lambda definition:

    constraint = lambda degree: original_pred != max_pred(degree)

...by a function definition...

    def constraint(degree): return original_pred != max_pred(degree)

However, as specified in the documentation, I should be able to use a lambda function for constraining my optimization.
I need more information on that, to know if this is a bug or am I misusing NGOpt.

    def constraint(degree): return original_pred != max_pred(degree)

Observed Results

When the lambda definition is replaced by a function definition. No warnings are thrown.

Expected Results

No warnings thrown as in the documentation.

Relevant Code

import nevergrad as ng 

def nevergrad_discrete_search(img, degree_range, perturbation):
  original_pred = np.argmax(certification.predictions_image(img))
  max_pred = lambda degree: np.argmax(certification.predictions_image(img, perturbation, degree))
  constraint = lambda degree: original_pred != max_pred(degree)

  # Discrete, ordered
  param = ng.p.TransitionChoice(degree_range)
  optimizer = ng.optimizers.NGOpt(parametrization=param, budget=100, num_workers=1)
  optimizer.parametrization.register_cheap_constraint(constraint)

  recommendation = optimizer.provide_recommendation()
  for _ in range(optimizer.budget):
      x = optimizer.ask()
      optimizer.tell(x, x.value)

  recommendation = optimizer.provide_recommendation()

  return recommendation.value
@teytaud
Copy link
Contributor

teytaud commented Sep 20, 2023

Maybe just

import warnings
warnings.filterwarnings("ignore", message=".Lambda as constraint is not advised because it may not be picklable.")

@teytaud teytaud closed this as completed Sep 20, 2023
@teytaud
Copy link
Contributor

teytaud commented Sep 20, 2023

By the way, note that "register_cheap_constraint" is useful only when the objective function is computationally much more expensive that the constraints. Otherwise the other tool is "constraint_violation". In most applications I use "constraint_violation", but "register_cheap_constraint" is better when the objective function is very expensive and/or does not work when constraints are violated. Reopen the issue or post in https://www.facebook.com/groups/nevergradusers if you need help with constraints, I know that we have not enough doc and users suffer a bit with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants