-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C. Benjamins: [MF] Determine number of trials for a total optimizatio…
…n budget (#1121)
- Loading branch information
Github Actions
committed
Jul 16, 2024
1 parent
98490d5
commit a7591e4
Showing
596 changed files
with
13,804 additions
and
334 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/1_basics/1_quadratic_function.doctree
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/1_basics/3_ask_and_tell.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/1_basics/sg_execution_times.doctree
Binary file not shown.
Binary file modified
BIN
-462 Bytes
(99%)
development/.doctrees/examples/2_multi_fidelity/1_mlp_epochs.doctree
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/2_multi_fidelity/2_sgd_datasets.doctree
Binary file not shown.
Binary file added
BIN
+28.2 KB
development/.doctrees/examples/2_multi_fidelity/3_specify_HB_via_total_budget.doctree
Binary file not shown.
Binary file modified
BIN
+1.74 KB
(120%)
development/.doctrees/examples/2_multi_fidelity/index.doctree
Binary file not shown.
Binary file modified
BIN
+1.43 KB
(120%)
development/.doctrees/examples/2_multi_fidelity/sg_execution_times.doctree
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/3_multi_objective/1_schaffer.doctree
Binary file not shown.
Binary file modified
BIN
-763 Bytes
(97%)
development/.doctrees/examples/3_multi_objective/2_parego.doctree
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/3_multi_objective/sg_execution_times.doctree
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/5_commandline/1_call_target_function_script.doctree
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
development/.doctrees/examples/5_commandline/sg_execution_times.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
development/_downloads/60791edaa81ce9889d1de4a67940b6d4/3_specify_HB_via_total_budget.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n# Specify Number of Trials via a Total Budget in Hyperband\nThis example uses a dummy function but illustrates how to setup Hyperband if you \nwant to specify a total optimization budget in terms of fidelity units.\n\nIn Hyperband, normally SMAC calculates a typical Hyperband round.\nIf the number of trials is not used up by one single round, the next round is started.\nInstead of specifying the number of trial beforehand, specify the total budget\nin terms of the fidelity units and let SMAC calculate how many trials that would be.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from __future__ import annotations\n\nimport numpy as np\nfrom ConfigSpace import Configuration, ConfigurationSpace, Float\nfrom matplotlib import pyplot as plt\n\nfrom smac import MultiFidelityFacade, RunHistory, Scenario\nfrom smac.intensifier.hyperband_utils import get_n_trials_for_hyperband_multifidelity\n\n__copyright__ = \"Copyright 2021, AutoML.org Freiburg-Hannover\"\n__license__ = \"3-clause BSD\"\n\n\nclass QuadraticFunction:\n max_budget = 500\n\n @property\n def configspace(self) -> ConfigurationSpace:\n cs = ConfigurationSpace(seed=0)\n x = Float(\"x\", (-5, 5), default=-5)\n cs.add_hyperparameters([x])\n\n return cs\n\n def train(self, config: Configuration, seed: int = 0, budget: float | None = None) -> float:\n \"\"\"Returns the y value of a quadratic function with a minimum we know to be at x=0.\"\"\"\n x = config[\"x\"]\n\n if budget is None:\n multiplier = 1\n else:\n multiplier = 1 + budget / self.max_budget\n\n return x**2 * multiplier\n\n\ndef plot(runhistory: RunHistory, incumbent: Configuration) -> None:\n plt.figure()\n\n # Plot ground truth\n x = list(np.linspace(-5, 5, 100))\n y = [xi * xi for xi in x]\n plt.plot(x, y)\n\n # Plot all trials\n for k, v in runhistory.items():\n config = runhistory.get_config(k.config_id)\n x = config[\"x\"]\n y = v.cost # type: ignore\n plt.scatter(x, y, c=\"blue\", alpha=0.1, zorder=9999, marker=\"o\")\n\n # Plot incumbent\n plt.scatter(incumbent[\"x\"], incumbent[\"x\"] * incumbent[\"x\"], c=\"red\", zorder=10000, marker=\"x\")\n\n plt.show()\n\n\nif __name__ == \"__main__\":\n model = QuadraticFunction()\n\n min_budget = 10 # minimum budget per trial\n max_budget = 500 # maximum budget per trial\n eta = 3 # standard HB parameter influencing the number of stages\n\n # Let's calculate how many trials we need to exhaust the total optimization budget (in terms of\n # fidelity units)\n n_trials = get_n_trials_for_hyperband_multifidelity(\n total_budget=10000, # this is the total optimization budget we specify in terms of fidelity units\n min_budget=min_budget, # This influences the Hyperband rounds, minimum budget per trial\n max_budget=max_budget, # This influences the Hyperband rounds, maximum budget per trial\n eta=eta, # This influences the Hyperband rounds\n print_summary=True,\n )\n\n # Scenario object specifying the optimization \"environment\"\n scenario = Scenario(\n model.configspace, deterministic=True, n_trials=n_trials, min_budget=min_budget, max_budget=max_budget\n )\n\n # Now we use SMAC to find the best hyperparameters\n smac = MultiFidelityFacade(\n scenario,\n model.train, # We pass the target function here\n overwrite=True, # Overrides any previous results that are found that are inconsistent with the meta-data\n intensifier=MultiFidelityFacade.get_intensifier(scenario=scenario, eta=eta),\n )\n\n incumbent = smac.optimize()\n\n # Get cost of default configuration\n default_cost = smac.validate(model.configspace.get_default_configuration())\n print(f\"Default cost: {default_cost}\")\n\n # Let's calculate the cost of the incumbent\n incumbent_cost = smac.validate(incumbent)\n print(f\"Incumbent cost: {incumbent_cost}\")\n\n # Let's plot it too\n plot(smac.runhistory, incumbent)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
112 changes: 112 additions & 0 deletions
112
development/_downloads/a94f3a529918fc1008873056ca54f5d0/3_specify_HB_via_total_budget.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
Specify Number of Trials via a Total Budget in Hyperband | ||
^^^^^^^^^^^^^^^^^^ | ||
This example uses a dummy function but illustrates how to setup Hyperband if you | ||
want to specify a total optimization budget in terms of fidelity units. | ||
In Hyperband, normally SMAC calculates a typical Hyperband round. | ||
If the number of trials is not used up by one single round, the next round is started. | ||
Instead of specifying the number of trial beforehand, specify the total budget | ||
in terms of the fidelity units and let SMAC calculate how many trials that would be. | ||
""" | ||
from __future__ import annotations | ||
|
||
import numpy as np | ||
from ConfigSpace import Configuration, ConfigurationSpace, Float | ||
from matplotlib import pyplot as plt | ||
|
||
from smac import MultiFidelityFacade, RunHistory, Scenario | ||
from smac.intensifier.hyperband_utils import get_n_trials_for_hyperband_multifidelity | ||
|
||
__copyright__ = "Copyright 2021, AutoML.org Freiburg-Hannover" | ||
__license__ = "3-clause BSD" | ||
|
||
|
||
class QuadraticFunction: | ||
max_budget = 500 | ||
|
||
@property | ||
def configspace(self) -> ConfigurationSpace: | ||
cs = ConfigurationSpace(seed=0) | ||
x = Float("x", (-5, 5), default=-5) | ||
cs.add_hyperparameters([x]) | ||
|
||
return cs | ||
|
||
def train(self, config: Configuration, seed: int = 0, budget: float | None = None) -> float: | ||
"""Returns the y value of a quadratic function with a minimum we know to be at x=0.""" | ||
x = config["x"] | ||
|
||
if budget is None: | ||
multiplier = 1 | ||
else: | ||
multiplier = 1 + budget / self.max_budget | ||
|
||
return x**2 * multiplier | ||
|
||
|
||
def plot(runhistory: RunHistory, incumbent: Configuration) -> None: | ||
plt.figure() | ||
|
||
# Plot ground truth | ||
x = list(np.linspace(-5, 5, 100)) | ||
y = [xi * xi for xi in x] | ||
plt.plot(x, y) | ||
|
||
# Plot all trials | ||
for k, v in runhistory.items(): | ||
config = runhistory.get_config(k.config_id) | ||
x = config["x"] | ||
y = v.cost # type: ignore | ||
plt.scatter(x, y, c="blue", alpha=0.1, zorder=9999, marker="o") | ||
|
||
# Plot incumbent | ||
plt.scatter(incumbent["x"], incumbent["x"] * incumbent["x"], c="red", zorder=10000, marker="x") | ||
|
||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
model = QuadraticFunction() | ||
|
||
min_budget = 10 # minimum budget per trial | ||
max_budget = 500 # maximum budget per trial | ||
eta = 3 # standard HB parameter influencing the number of stages | ||
|
||
# Let's calculate how many trials we need to exhaust the total optimization budget (in terms of | ||
# fidelity units) | ||
n_trials = get_n_trials_for_hyperband_multifidelity( | ||
total_budget=10000, # this is the total optimization budget we specify in terms of fidelity units | ||
min_budget=min_budget, # This influences the Hyperband rounds, minimum budget per trial | ||
max_budget=max_budget, # This influences the Hyperband rounds, maximum budget per trial | ||
eta=eta, # This influences the Hyperband rounds | ||
print_summary=True, | ||
) | ||
|
||
# Scenario object specifying the optimization "environment" | ||
scenario = Scenario( | ||
model.configspace, deterministic=True, n_trials=n_trials, min_budget=min_budget, max_budget=max_budget | ||
) | ||
|
||
# Now we use SMAC to find the best hyperparameters | ||
smac = MultiFidelityFacade( | ||
scenario, | ||
model.train, # We pass the target function here | ||
overwrite=True, # Overrides any previous results that are found that are inconsistent with the meta-data | ||
intensifier=MultiFidelityFacade.get_intensifier(scenario=scenario, eta=eta), | ||
) | ||
|
||
incumbent = smac.optimize() | ||
|
||
# Get cost of default configuration | ||
default_cost = smac.validate(model.configspace.get_default_configuration()) | ||
print(f"Default cost: {default_cost}") | ||
|
||
# Let's calculate the cost of the incumbent | ||
incumbent_cost = smac.validate(incumbent) | ||
print(f"Incumbent cost: {incumbent_cost}") | ||
|
||
# Let's plot it too | ||
plot(smac.runhistory, incumbent) |
Binary file modified
BIN
+4.01 KB
(110%)
development/_downloads/bc82bea3a5dd7bdba60b65220891d9e5/examples_python.zip
Binary file not shown.
Binary file modified
BIN
+4.9 KB
(110%)
development/_downloads/fb625db3c50d423b1b7881136ffdeec8/examples_jupyter.zip
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.