Skip to content

Commit

Permalink
Adding hyperopt notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
yngtodd committed Mar 11, 2019
1 parent f1e8321 commit 4c52d73
Show file tree
Hide file tree
Showing 2 changed files with 564 additions and 0 deletions.
282 changes: 282 additions & 0 deletions notebooks/.ipynb_checkpoints/hyperopt-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"import time \n",
"import pprint\n",
"import pickle\n",
"import numpy as np\n",
"\n",
"from hyperopt import fmin, tpe, hp, STATUS_OK, Trials\n",
"from hyperspace.benchmarks import StyblinskiTang\n",
"from skopt import gp_minimize\n",
"\n",
"from scipy.optimize import OptimizeResult\n",
"\n",
"\n",
"pp = pprint.PrettyPrinter(indent=4)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100%|██████████| 100/100 [00:00<00:00, 312.22it/s, best loss: -77.18972901241136]\n",
"{'0': -2.729885415079413, '1': -2.702710636326808}\n"
]
}
],
"source": [
"objective = StyblinskiTang(dims=2)\n",
"\n",
"trials = Trials()\n",
"\n",
"space = [\n",
" hp.uniform('0', -5, 5),\n",
" hp.uniform('1', -5, 5),\n",
"]\n",
"\n",
"best = fmin(objective,\n",
" space=space,\n",
" algo=tpe.suggest,\n",
" trials=trials,\n",
" max_evals=100)\n",
"\n",
"print(best)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 'book_time': datetime.datetime(2019, 3, 11, 17, 56, 33, 649000),\n",
" 'exp_key': None,\n",
" 'misc': { 'cmd': ('domain_attachment', 'FMinIter_Domain'),\n",
" 'idxs': {'x': [0], 'y': [0], 'z': [0]},\n",
" 'tid': 0,\n",
" 'vals': { 'x': [1.9185834595646973],\n",
" 'y': [-7.441043187200959],\n",
" 'z': [-7.64457512965655]},\n",
" 'workdir': None},\n",
" 'owner': None,\n",
" 'refresh_time': datetime.datetime(2019, 3, 11, 17, 56, 33, 650000),\n",
" 'result': {'loss': 2274.399434544481, 'status': 'ok'},\n",
" 'spec': None,\n",
" 'state': 2,\n",
" 'tid': 0,\n",
" 'version': 0}\n"
]
}
],
"source": [
"pp.pprint(trials.trials[0])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(trials.trials)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(10):\n",
" print(f'\\nIteration {i}')\n",
" pp.pprint(trials.trials[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comparison with Bayesian Optimization"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"bounds = np.tile((-5., 5.), (2, 1))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"res = gp_minimize(objective, bounds, n_calls=100)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-181.64915626952801342"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res.fun"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create better format for results"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"def create_result(trials, maximize=False):\n",
" \"\"\"Create a Scipy OptimizeResult object for hyperopt.\"\"\"\n",
" res = OptimizeResult()\n",
" \n",
" x_iters = []\n",
" func_vals = []\n",
" for i in range(len(trials)):\n",
" trial = trials.trials[i]\n",
" x_iters.append(trial['misc']['vals'])\n",
" func_vals.append(trial['result']['loss'])\n",
" \n",
" res.x_iters = x_iters\n",
" res.func_vals = func_vals\n",
" \n",
" if maximize:\n",
" res.fun = np.max(func_vals)\n",
" max_idx = np.argmax(func_vals)\n",
" res.x = res.x_iters[max_idx]\n",
" else:\n",
" res.fun = np.min(func_vals)\n",
" min_idx = np.argmin(func_vals)\n",
" res.x = res.x_iters[min_idx]\n",
" \n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"res = create_result(trials, maximize=False)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-77.18972901241136"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res.fun"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'0': [-2.729885415079413], '1': [-2.702710636326808]}"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res.x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 4c52d73

Please sign in to comment.