{ "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](https://raw.githubusercontent.com/issp-center-dev/PHYSBO/master/examples/grain_bound/data/s5-210.csv) を `data` ディレクトリ以下に保存し、次のように読み出します。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.657314Z", "start_time": "2020-12-04T06:17:27.967614Z" } }, "outputs": [], "source": [ "import physbo\n", "\n", "import numpy as np\n", "\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": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.663215Z", "start_time": "2020-12-04T06:17:28.658656Z" } }, "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": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.674407Z", "start_time": "2020-12-04T06:17:28.669875Z" } }, "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": { "ExecuteTime": { "end_time": "2020-12-04T06:17:47.726433Z", "start_time": "2020-12-04T06:17:28.677429Z" }, "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": { "ExecuteTime": { "end_time": "2020-12-04T06:17:48.722691Z", "start_time": "2020-12-04T06:17:47.728006Z" } }, "outputs": [], "source": [ "policy.save(file_history='history.npz', file_training='training.npz', file_predictor='predictor.dump')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:18:05.746742Z", "start_time": "2020-12-04T06:17:48.724101Z" }, "scrolled": true }, "outputs": [], "source": [ "# policy を削除\n", "del policy\n", "\n", "# 保存した policy をロード \n", "policy = physbo.search.discrete.policy(test_X=X)\n", "policy.load(file_history='history.npz', file_training='training.npz', file_predictor='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) " ] }, { "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.8.5" } }, "nbformat": 4, "nbformat_minor": 1 }