in bayesmark/builtin_opt/opentuner_optimizer.py [0:0]
def suggest(self, n_suggestions=1):
"""Make `n_suggestions` suggestions for what to evaluate next.
This requires the user observe all previous suggestions before calling
again.
Parameters
----------
n_suggestions : int
The number of suggestions to return.
Returns
-------
next_guess : list of dict
List of `n_suggestions` suggestions to evaluate the objective
function. Each suggestion is a dictionary where each key
corresponds to a parameter being optimized.
"""
assert n_suggestions >= 1, "invalid value for n_suggestions"
# Update the n_suggestions if it is different from the current setting.
if self.api.search_driver.args.parallelism != n_suggestions:
self.api.search_driver.args.parallelism = n_suggestions
warnings.warn("n_suggestions changed across suggest calls")
# Require the user to already observe all previous suggestions.
# Otherwise, opentuner will just recycle old suggestions.
assert len(self.x_to_dr) == 0, "all the previous suggestions should have been observed by now"
# The real meat of suggest from opentuner: Get next `n_suggestions`
# unique suggestions.
desired_results = [self.api.get_next_desired_result() for _ in range(n_suggestions)]
# Save DesiredResult object in dict since observe will need it.
X = []
using_dummy_suggest = False
for ii in range(n_suggestions):
# Opentuner can give up, but the API requires guessing forever.
if desired_results[ii] is None:
assert self.dummy_suggest is not None, "opentuner gave up on the first call!"
# Use the dummy suggestion in this case.
X.append(self.dummy_suggest)
using_dummy_suggest = True
continue
# Get the simple dict equivalent to suggestion.
x_guess = desired_results[ii].configuration.data
X.append(x_guess)
# Now save the desired result for future use in observe.
x_guess_ = OpentunerOptimizer.hashable_dict(x_guess)
assert x_guess_ not in self.x_to_dr, "the suggestions should not already be in the x_to_dr dict"
self.x_to_dr[x_guess_] = desired_results[ii]
# This will also catch None from opentuner.
assert isinstance(self.x_to_dr[x_guess_], DesiredResult)
assert len(X) == n_suggestions, "incorrect number of suggestions provided by opentuner"
# Log suggestion for repeating if opentuner gives up next time. We can
# only do this when it is not already being used since it we will be
# checking guesses against dummy_suggest in observe.
if not using_dummy_suggest:
self.dummy_suggest = X[-1]
return X