Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def narrow_space_from_best_guess(self, best_guess, kept_space_ratio: float = 0.0) -> HyperparameterDistribution:
"""
Will narrow the space. If the cumulative kept_space_ratio gets to be under or equal to 1/len(choice_list),
then the list is crunched to a single item as a FixedHyperparameter to reflect this narrowing.
So once a small enough kept_space_ratio is reached, the list becomes a fixed unique item from the best guess.
Otherwise, a deepcopy of self is returned.
:param best_guess: the best item of the list to keep if truly narrowing.
:param kept_space_ratio: the ratio of the space to keep.
:return: a deepcopy of self, or else a FixedHyperparameter of the best_guess.
"""
new_narrowing = self.get_current_narrowing_value() * kept_space_ratio
if len(self.choice_list) == 0 or len(self.choice_list) == 1 or new_narrowing <= 1.0 / len(self.choice_list):
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return copy.deepcopy(self).was_narrowed_from(kept_space_ratio, self)
def narrow_space_from_best_guess(self, best_guess, kept_space_ratio: float = 0.5) -> HyperparameterDistribution:
"""
Will narrow the underlying distribution towards the best guess.
:param best_guess: the value towards which we want to narrow down the space. Should be between 0.0 and 1.0.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return: a new HyperparameterDistribution that has been narrowed down.
"""
lost_space_ratio = 1.0 - kept_space_ratio
new_min_included = round(self.min_included * kept_space_ratio + best_guess * lost_space_ratio)
new_max_included = round(self.max_included * kept_space_ratio + best_guess * lost_space_ratio)
if new_max_included <= new_min_included or kept_space_ratio == 0.0:
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return RandInt(new_min_included, new_max_included).was_narrowed_from(kept_space_ratio, self)
Will narrow the distribution towards the new best_guess.
The mean will move towards the new best guess, and the standard deviation
will be multiplied by the kept_space_ratio.
The hard clip limit is unchanged.
:param best_guess: the value towards which we want to narrow down the space's mean. Should be between 0.0 and 1.0.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return: a new HyperparameterDistribution that has been narrowed down.
"""
lost_space_ratio = 1.0 - kept_space_ratio
if isinstance(self.mean, tuple):
self.mean = self.mean[0]
new_mean = self.mean * kept_space_ratio + best_guess * lost_space_ratio
new_std = self.std * kept_space_ratio
if new_std <= 0.0:
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return Normal(
new_mean, new_std, self.hard_clip_min, self.hard_clip_max
).was_narrowed_from(kept_space_ratio, self)
def narrow_space_from_best_guess(self, best_guess, kept_space_ratio: float = 0.5) -> HyperparameterDistribution:
"""
Will narrow the underlying distribution towards the best guess.
:param best_guess: the value towards which we want to narrow down the space. Should be between 0.0 and 1.0.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return: a new HyperparameterDistribution that has been narrowed down.
"""
lost_space_ratio = 1.0 - kept_space_ratio
new_min_included = self.min_included * kept_space_ratio + best_guess * lost_space_ratio
new_max_included = self.max_included * kept_space_ratio + best_guess * lost_space_ratio
if new_max_included <= new_min_included or kept_space_ratio == 0.0:
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return Uniform(new_min_included, new_max_included).was_narrowed_from(kept_space_ratio, self)
def narrow_space_from_best_guess(self, best_guess, kept_space_ratio: float = 0.5) -> HyperparameterDistribution:
"""
Will narrow, in log space, the distribution towards the new best_guess.
:param best_guess: the value towards which we want to narrow down the space. Should be between 0.0 and 1.0.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return: a new HyperparameterDistribution that has been narrowed down.
"""
log2_best_guess = math.log2(best_guess)
lost_space_ratio = 1.0 - kept_space_ratio
new_min_included = self.log2_min_included * kept_space_ratio + log2_best_guess * lost_space_ratio
new_max_included = self.log2_max_included * kept_space_ratio + log2_best_guess * lost_space_ratio
if new_max_included <= new_min_included or kept_space_ratio == 0.0:
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return LogUniform(2 ** new_min_included, 2 ** new_max_included).was_narrowed_from(kept_space_ratio, self)
def narrow_space_from_best_guess(self, best_guess, kept_space_ratio: float = 0.5) -> HyperparameterDistribution:
"""
Will narrow, in log space, the distribution towards the new best_guess.
:param best_guess: the value towards which we want to narrow down the space. Should be between 0.0 and 1.0.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return: a new HyperparameterDistribution that has been narrowed down.
"""
log2_best_guess = math.log2(best_guess)
lost_space_ratio = 1.0 - kept_space_ratio
new_min_included = self.log2_min_included * kept_space_ratio + log2_best_guess * lost_space_ratio
new_max_included = self.log2_max_included * kept_space_ratio + log2_best_guess * lost_space_ratio
if new_max_included <= new_min_included or kept_space_ratio == 0.0:
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return LogUniform(2 ** new_min_included, 2 ** new_max_included).was_narrowed_from(kept_space_ratio, self)
if (
len(self.choice_list) == 0
or len(self.choice_list) == 1
or new_size <= 1
or kept_space_ratio <= 1.0 / len(self.choice_list)
):
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
# Bring best_guess to front
idx = self.choice_list.index(best_guess)
del self.choice_list[idx]
self.choice_list = [best_guess] + self.choice_list
# Narrowing of the list.
maybe_reduced_list = self.choice_list[:new_size]
return PriorityChoice(maybe_reduced_list).was_narrowed_from(kept_space_ratio, self)
The mean will move towards the new best guess, and the standard deviation
will be multiplied by the kept_space_ratio.
The hard clip limit is unchanged.
:param best_guess: the value towards which we want to narrow down the space's mean. Should be between 0.0 and 1.0.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return: a new HyperparameterDistribution that has been narrowed down.
"""
lost_space_ratio = 1.0 - kept_space_ratio
if isinstance(self.mean, tuple):
self.mean = self.mean[0]
new_mean = self.mean * kept_space_ratio + best_guess * lost_space_ratio
new_std = self.std * kept_space_ratio
if new_std <= 0.0:
return FixedHyperparameter(best_guess).was_narrowed_from(kept_space_ratio, self)
return Normal(
new_mean, new_std, self.hard_clip_min, self.hard_clip_max
).was_narrowed_from(kept_space_ratio, self)
def narrow_space_from_best_guess(self, best_guess, kept_space_ratio: float = 0.5) -> 'Quantized':
"""
Will narrow the underlying distribution and re-wrap it under a Quantized.
:param best_guess: the value towards which we want to narrow down the space.
:param kept_space_ratio: what proportion of the space is kept. Default is to keep half the space (0.5).
:return:
"""
return Quantized(
self.hd.narrow_space_from_best_guess(best_guess, kept_space_ratio)
).was_narrowed_from(kept_space_ratio, self)