Skip to content

Commit a8c5681

Browse files
authored
Fix doctest (#243)
* Fix: Remove test for output of `file.write` * Fix: Issues with docs * Add: Fix docs title and change to code blocks
1 parent 69f7633 commit a8c5681

2 files changed

Lines changed: 44 additions & 34 deletions

File tree

ConfigSpace/read_and_write/json.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,18 @@ def write(configuration_space, indent=2):
295295
:class:`~ConfigSpace.configuration_space.ConfigurationSpace` in json format.
296296
This string can be written to file.
297297
298-
Example
299-
-------
300-
.. doctest::
298+
Example:
301299
302-
>>> from ConfigSpace import ConfigurationSpace
303-
>>> import ConfigSpace.hyperparameters as CSH
304-
>>> from ConfigSpace.read_and_write import json
305-
>>> cs = ConfigurationSpace()
306-
>>> cs.add_hyperparameter(CSH.CategoricalHyperparameter('a', choices=[1, 2, 3]))
307-
a, Type: Categorical, Choices: {1, 2, 3}, Default: 1
300+
..code:: python
301+
302+
from ConfigSpace import ConfigurationSpace
303+
import ConfigSpace.hyperparameters as CSH
304+
from ConfigSpace.read_and_write import json
305+
cs = ConfigurationSpace()
306+
cs.add_hyperparameter(CSH.CategoricalHyperparameter('a', choices=[1, 2, 3]))
308307
309-
>>> with open('configspace.json', 'w') as f:
310-
... f.write(json.write(cs))
311-
299
308+
with open('configspace.json', 'w') as f:
309+
f.write(json.write(cs))
312310
313311
Parameters
314312
----------

docs/source/User-Guide.rst

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -246,36 +246,48 @@ To read it from file
246246

247247

248248
5th Example: Placing priors on the hyperparameters
249-
-------------------------
249+
--------------------------------------------------
250250

251-
If you want to conduct black-box optimization in SMAC (https://arxiv.org/abs/2109.09831), and you have prior knowledge about the which regions of the search space are more likely to contain the optimum, you may include this knowledge when designing the configuration space. More specifically, you place prior distributions over the optimum on the parameters, either by a (log)-normal or (log)-Beta distribution. SMAC then considers the given priors through the optimization by using PiBO (https://openreview.net/forum?id=MMAeCXIa89).
251+
If you want to conduct black-box optimization in SMAC (https://arxiv.org/abs/2109.09831), and you have prior knowledge about the which regions of the search space are more likely to contain the optimum, you may include this knowledge when designing the configuration space. More specifically, you place prior distributions over the optimum on the parameters, either by a (log)-normal or (log)-Beta distribution. SMAC then considers the given priors through the optimization by using PiBO (https://openreview.net/forum?id=MMAeCXIa89).
252252

253253
Consider the case of optimizing the accuracy of an MLP with three hyperparameters: learning rate [1e-5, 1e-1], dropout [0, 0.99] and activation {Tanh, ReLU}. From prior experience, you believe the optimal learning rate to be around 1e-3, a good dropout to be around 0.25, and the optimal activation function to be ReLU about 80% of the time. This can be represented accordingly:
254254

255-
>>> import numpy as np
256-
>>> import ConfigSpace.hyperparameters as CSH
257-
>>> from ConfigSpace.configuration_space import ConfigurationSpace
258-
>>> # convert 10 log to natural log for learning rate, mean 1e-3
259-
>>> logmean = np.log(1e-3)
260-
>>> # two standard deviations on either side of the mean to cover the search space
261-
>>> logstd = np.log(10.0)
262-
>>> learning_rate = CSH.NormalFloatHyperparameter(name='learning_rate', lower=1e-5, upper=1e-1, default_value=1e-3, mu=logmean, sigma=logstd, log=True)
263-
>>> dropout = CSH.BetaFloatHyperparameter(name='dropout', lower=0, upper=0.99, default_value=0.25, alpha=2, beta=4, log=False)
264-
>>> activation = CSH.CategoricalHyperparameter(name='activation', choices=['tanh', 'relu'], weights=[0.2, 0.8])
265-
266-
>>> cs = ConfigurationSpace()
267-
>>> cs.add_hyperparameters([learning_rate, dropout, activation])
268-
[learning_rate, Type: NormalFloat, Mu: -6.907755278982137 Sigma: 2.302585092994046, Range: [1e-05, 0.1], Default: 0.001, on log-scale, dropout, Type: BetaFloat, Alpha: 2.0 Beta: 4.0, Range: [0.0, 0.99], Default: 0.25, activation, Type: Categorical, Choices: {tanh, relu}, Default: tanh, Probabilities: (0.2, 0.8)]
255+
.. code-block:: python
256+
257+
import numpy as np
258+
import ConfigSpace.hyperparameters as CSH
259+
from ConfigSpace.configuration_space import ConfigurationSpace
260+
261+
# convert 10 log to natural log for learning rate, mean 1e-3
262+
logmean = np.log(1e-3)
263+
# two standard deviations on either side of the mean to cover the search space
264+
logstd = np.log(10.0)
265+
266+
learning_rate = CSH.NormalFloatHyperparameter(name='learning_rate', lower=1e-5, upper=1e-1, default_value=1e-3, mu=logmean, sigma=logstd, log=True)
267+
dropout = CSH.BetaFloatHyperparameter(name='dropout', lower=0, upper=0.99, default_value=0.25, alpha=2, beta=4, log=False)
268+
activation = CSH.CategoricalHyperparameter(name='activation', choices=['tanh', 'relu'], weights=[0.2, 0.8])
269+
270+
cs = ConfigurationSpace()
271+
272+
cs.add_hyperparameters([learning_rate, dropout, activation])
273+
# [learning_rate, Type: NormalFloat, Mu: -6.907755278982137 Sigma: 2.302585092994046, Range: [1e-05, 0.1], Default: 0.001, on log-scale, dropout, Type: BetaFloat, Alpha: 2.0 Beta: 4.0, Range: [0.0, 0.99], Default: 0.25, activation, Type: Categorical, Choices: {tanh, relu}, Default: tanh, Probabilities: (0.2, 0.8)]
269274
270275
To check that your prior makes sense for each hyperparameter, you can easily do so with the __pdf__ method. There, you will see that the probability of the optimal learning rate peaks at 10^-3, and decays as we go further away from it:
271276

272-
>>> test_points = np.logspace(-5, -1, 5)
273-
>>> test_points
274-
array([1.e-05, 1.e-04, 1.e-03, 1.e-02, 1.e-01])
277+
.. code-block:: python
278+
279+
test_points = np.logspace(-5, -1, 5)
280+
281+
print(test_points)
282+
# array([1.e-05, 1.e-04, 1.e-03, 1.e-02, 1.e-01])
283+
284+
The pdf function accepts an (N, ) numpy array as input.
285+
286+
.. code-block:: python
275287
276-
# the pdf function accepts an (N, ) numpy array as input.
277-
>>> learning_rate.pdf(test_points)
278-
array([0.02456573, 0.11009594, 0.18151753, 0.11009594, 0.02456573])
288+
test_points_pdf = learning_rate.pdf(test_points)
289+
print(test_points_pdf)
290+
# array([0.02456573, 0.11009594, 0.18151753, 0.11009594, 0.02456573])
279291
280292
281293

0 commit comments

Comments
 (0)