Skip to content

Add TPE algorithm into Orion #381

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

Merged
merged 9 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/user/algorithms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Configuration
gamma: 0.25
equal_weight: False
prior_weight: 1.0
full_weight_num: 25


``seed``
Expand All @@ -250,6 +251,11 @@ True to set equal weights for observed points. Default is ``False``.

The weight given to the prior point of the input space. Default is ``1.0``.

``full_weight_num``

The number of the most recent trials which get the full weight where the others will be
applied with a linear ramp from 0 to 1.0. It will only take effect if ``equal_weight``
is ``False``. Default is ``25``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with the paper but not with the implementation, right?

Copy link
Collaborator Author

@donglinjy donglinjy Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suddenly realize seems I misunderstood this ramp up when you said from 1/n to 1. Paper mentions ramp from 0 to 1.0, but the weight of course can not be 0, so get start from 1/n is not that different compared with the paper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's (1/n, 1/n, ... 1/n, growing to 1 through 25 steps), while paper says (0 growing to 1, 1, 1, ... 25 times).



Algorithm Plugins
Expand Down
20 changes: 14 additions & 6 deletions src/orion/algo/tpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def adaptive_parzen_estimator(mus, low, high,
:param high: real value for upper bound of points.
:param prior_weight: real value for the weight of the prior mean.
:param equal_weight: bool value indicating if all points with equal weights.
:param flat_num: int value indicating the number of latest points with equal weights,
it is only valid if `equal_weight` is False.
:param flat_num: int value indicating the number of the most recent trials which
get the full weight where the others will be applied with a linear ramp
from 0 to 1.0. It will only take effect if equal_weight is False.
"""
def update_weights(total_num):
"""Generate weights for all components"""
Expand Down Expand Up @@ -153,20 +154,27 @@ class TPE(BaseAlgorithm):
prior_weight: int
The weight given to the prior point of the input space.
Default: ``1.0``
full_weight_num: int
The number of the most recent trials which get the full weight where the others will be
applied with a linear ramp from 0 to 1.0. It will only take effect if equal_weight
is False.

"""

# pylint:disable=too-many-arguments
def __init__(self, space, seed=None,
n_initial_points=20, n_ei_candidates=24,
gamma=0.25, equal_weight=False, prior_weight=1.0):
gamma=0.25, equal_weight=False,
prior_weight=1.0, full_weight_num=25):

super(TPE, self).__init__(space,
seed=seed,
n_initial_points=n_initial_points,
n_ei_candidates=n_ei_candidates,
gamma=gamma,
equal_weight=equal_weight,
prior_weight=prior_weight)
prior_weight=prior_weight,
full_weight_num=full_weight_num)

for dimension in self.space.values():

Expand Down Expand Up @@ -289,10 +297,10 @@ def _sample_real_point(self, dimension, below_points, above_points):
low, high = dimension.interval()
below_mus, below_sigmas, below_weights = \
adaptive_parzen_estimator(below_points, low, high, self.prior_weight,
self.equal_weight, flat_num=25)
self.equal_weight, flat_num=self.full_weight_num)
above_mus, above_sigmas, above_weights = \
adaptive_parzen_estimator(above_points, low, high, self.prior_weight,
self.equal_weight, flat_num=25)
self.equal_weight, flat_num=self.full_weight_num)

gmm_sampler_below = GMMSampler(self, below_mus, below_sigmas, low, high, below_weights)
gmm_sampler_above = GMMSampler(self, above_mus, above_sigmas, low, high, above_weights)
Expand Down
1 change: 1 addition & 0 deletions tests/functional/algos/tpe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ algorithms:
gamma: 0.25
equal_weight: False
prior_weight: 1.0
full_weight_num: 25

database:
type: 'mongodb'
Expand Down
9 changes: 8 additions & 1 deletion tests/unittests/algo/test_tpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,21 @@ def test_adaptive_parzen_normal_estimator_weight():
# prior weight
mus, sigmas, weights = adaptive_parzen_estimator(obs_mus, low, high, prior_weight=0.5,
equal_weight=False, flat_num=25)
print(weights)

prior_pos = numpy.searchsorted(mus, 2)
assert numpy.all(weights[:30 - 25] == (numpy.linspace(1.0 / 30, 1.0, num=30 - 25) / 31))
assert numpy.all(weights[33 - 25:prior_pos] == 1 / 31)
assert weights[prior_pos] == 0.5 / 31
assert numpy.all(weights[prior_pos + 1:] == 1 / 31)
assert numpy.all(sigmas == 6 / 10)

# full weights number
mus, sigmas, weights = adaptive_parzen_estimator(obs_mus, low, high, prior_weight=1.0,
equal_weight=False, flat_num=15)
assert numpy.all(weights[:30 - 15] == (numpy.linspace(1.0 / 30, 1.0, num=30 - 15) / 31))
assert numpy.all(weights[33 - 15:] == 1 / 31)
assert numpy.all(sigmas == 6 / 10)


def test_adaptive_parzen_normal_estimator_sigma_clip():
"""Test that the magic clip of sigmas for parzen estimator"""
Expand Down