Skip to content

Instantly share code, notes, and snippets.

@ltiao
Created June 19, 2019 16:25
Show Gist options
  • Save ltiao/fba4f0751dca515d4a72bcacd8685de1 to your computer and use it in GitHub Desktop.
Save ltiao/fba4f0751dca515d4a72bcacd8685de1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def hyperband(b_max, b_min=1, eta=3):\n",
"\n",
" # ln (eta)\n",
" ln_eta = np.log(eta)\n",
"\n",
" # ln (b_max/b_min)\n",
" ln_ratio = np.log(b_max) - np.log(b_min)\n",
"\n",
" # log_eta (b_max/b_min)\n",
" # lg_eta_ratio = ln_ratio / ln_eta\n",
"\n",
" # division for change of base then floor\n",
" s_max = int(np.floor_divide(ln_ratio, ln_eta))\n",
"\n",
" for s in reversed(range(s_max+1)):\n",
"\n",
" n = int(np.ceil(eta**s * (s_max + 1) / (s + 1)))\n",
" b = b_max // eta**s\n",
"\n",
" yield n, b"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def successive_halving(n, b, b_max, eta=3):\n",
"\n",
" yield n, b\n",
"\n",
" if b * eta <= b_max:\n",
" yield from successive_halving(n // eta, b * eta, b_max, eta)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[[(81, 1), (27, 3), (9, 9), (3, 27), (1, 81)],\n",
" [(34, 3), (11, 9), (3, 27), (1, 81)],\n",
" [(15, 9), (5, 27), (1, 81)],\n",
" [(8, 27), (2, 81)],\n",
" [(5, 81)]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[list(successive_halving(n, b, b_max=81)) for n, b in hyperband(b_max=81)]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[(243, 1), (81, 3), (27, 9), (9, 27), (3, 81), (1, 243)],\n",
" [(98, 3), (32, 9), (10, 27), (3, 81), (1, 243)],\n",
" [(41, 9), (13, 27), (4, 81), (1, 243)],\n",
" [(18, 28), (6, 84), (2, 252)],\n",
" [(9, 85), (3, 255)],\n",
" [(6, 256)]]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[list(successive_halving(n, b, b_max=256)) for n, b in hyperband(b_max=256)]"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment