{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# インタラクティブに実行する\n", "\n", "以下の流れで、PHYSBO をインタラクティブに実行することができます。\n", "\n", "1. PHYSBO から次に実行するパラメータを得ます。\n", "2. PHYSBO の外部で評価値を得ます。\n", "3. 評価値をPHYSBOに登録します。\n", "\n", "例えば、以下の様な場合に適しています。\n", "\n", "- 人手による実験を行い、その評価値をPHYSBOに与えたい。\n", "- simulator の実行を別プロセスで行うなど、柔軟に実行制御を行いたい。\n", "\n", "## 探索候補データの準備\n", "\n", "これまでのマニュアルでも利用していたs5-210.csv を用いてガウス過程を実行します。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import physbo\n", "\n", "import os\n", "import urllib2\n", "import ssl\n", "import numpy as np\n", "\n", "ssl._create_default_https_context = ssl._create_unverified_context\n", "\n", "def load_data():\n", " A = np.asarray(np.loadtxt('data/s5-210.csv',skiprows=1, delimiter=',') )\n", " X = A[:,0:3]\n", " t = -A[:,3]\n", " return X, t\n", "\n", "X, t = load_data()\n", "X = physbo.misc.centering(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## simulator の定義" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class simulator:\n", " def __init__( self ):\n", " _, self.t = load_data()\n", " \n", " def __call__( self, action ):\n", " return self.t[action]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 最適化の実行" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# policy のセット \n", "policy = physbo.search.discrete.policy(test_X=X)\n", "\n", "# シード値のセット \n", "policy.set_seed( 0 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "各探索ステップでは以下の処理を行っています。\n", "\n", "1. `max_num_probes=1, simulator=None` として random_search または bayes_search を実行して action ID (パラメータ)を得る。\n", "2. `t = simulator(actions)` により評価値(の array) を得る。\n", "3. `policy.write(actions, t)`により action ID (パラメータ) に対する評価値を登録する。\n", "4. `physbo.search.utility.show_search_results` により履歴を表示する。\n", "\n", "以下では、ランダムサンプリングを2回(1st, and 2nd steps)、ベイズ最適化による提案を2回(3rd, and 4th steps)を行います。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "simulator = simulator()\n", "\n", "''' 1st step (random sampling) '''\n", "actions = policy.random_search(max_num_probes=1, simulator=None)\n", "t = simulator(actions)\n", "policy.write(actions, t)\n", "physbo.search.utility.show_search_results(policy.history, 10)\n", "\n", "''' 2nd step (random sampling) '''\n", "actions = policy.random_search(max_num_probes=1, simulator=None)\n", "t = simulator(actions)\n", "policy.write(actions, t)\n", "physbo.search.utility.show_search_results(policy.history, 10)\n", "\n", "''' 3rd step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t) \n", "physbo.search.utility.show_search_results(policy.history, 10) \n", "\n", "''' 4-th step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t)\n", "physbo.search.utility.show_search_results(policy.history, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 中断と再開\n", "\n", "以下の predictor, training, history を外部ファイルに保存することで、最適化プロセスを中断し、途中から再開することができます。\n", "\n", "- predictor: 目的関数の予測モデル\n", "- training: predictor の学習に用いるデータ (`physbo.variable` オブジェクト)\n", "- history: 最適化実行の履歴 (`physbo.search.discrete.results.history` オブジェクト)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import pickle\n", "\n", "with open('predictor.dump', 'wb') as f:\n", " pickle.dump(policy.predictor, f)\n", "policy.training.save('training.npz')\n", "policy.history.save('history.npz')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# policy を削除\n", "del policy\n", "\n", "# 保存した policy をロード \n", "policy = physbo.search.discrete.policy(test_X=X)\n", "policy.load('history.npz', 'training.npz', 'predictor.dump')\n", "\n", "''' 5-th step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t) \n", "physbo.search.utility.show_search_results(policy.history, 10) \n", "\n", "# predictor と training を個別に指定することも可\n", "''' 6-th step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, \n", " predictor=policy.predictor, training=policy.training,\n", " simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t) \n", "physbo.search.utility.show_search_results(policy.history, 10) " ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.16" } }, "nbformat": 4, "nbformat_minor": 1 }