{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Restart calculations by reading existing calculation results\n", "\n", "You can read existing action IDs (parameters) and their evaluation values and run PHYSBO in the following flow. \n", "\n", "1. Load an external file and read the existing action IDs (parameters) and their evaluation values. \n", "2. Register the action ID (parameter) and evaluation value to PHYSBO. \n", "3. Get the parameters for the next execution from PHYSBO.\n", "\n", "This can be used in cases where PHYSBO cannot be left open for a long time due to time constraints, and thus cannot be executed interactively.\n", "\n", "## Prepare the search candidate data\n", "\n", "As the previous tutorials, save the dataset file [s5-210.csv](https://raw.githubusercontent.com/issp-center-dev/PHYSBO/master/examples/grain_bound/data/s5-210.csv) into the subdirectory `data`, and load dataset from this file as the following:" ] }, { "cell_type": "code", "execution_count": 2, "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": [ "## Preparing the precomputed data\n", "\n", "In the `load_data` function above, all X and t are stored. Here, as precomputed, we get a random list of 20 actoin IDs and their evaluation values." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[12623, 13781, 1326, 8484, 16753, 15922, 13268, 9938, 15617, 11732, 7157, 16537, 4563, 9235, 4579, 3107, 8208, 17451, 4815, 10162]\n" ] } ], "source": [ "import random\n", "random.seed(0)\n", "calculated_ids = random.sample(range(t.size), 20)\n", "print(calculated_ids)\n", "t_initial = t[calculated_ids]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Register action ID (parameter) and evaluation value to PHYSBO.\n", "\n", "Register `calculated_ids` and `t[calculated_ids]` as a list in the initial variable `initial_data` of policy." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# set policy \n", "policy = physbo.search.discrete.policy(test_X=X, initial_data=[calculated_ids, t_initial])\n", "\n", "# set seed \n", "policy.set_seed( 0 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get the next parameter to be executed from PHYSBO\n", "\n", "Perform Bayesian optimization to obtain the next candidate point." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.674407Z", "start_time": "2020-12-04T06:17:28.669875Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Start the initial hyper parameter searching ...\n", "Done\n", "\n", "Start the hyper parameter learning ...\n", "0 -th epoch marginal likelihood -20.09302189053099\n", "50 -th epoch marginal likelihood -23.11964735598211\n", "100 -th epoch marginal likelihood -24.83020118385076\n", "150 -th epoch marginal likelihood -25.817906570042602\n", "200 -th epoch marginal likelihood -26.42342027124426\n", "250 -th epoch marginal likelihood -26.822598600211865\n", "300 -th epoch marginal likelihood -27.10872736571494\n", "350 -th epoch marginal likelihood -27.331572599126865\n", "400 -th epoch marginal likelihood -27.517235815448124\n", "450 -th epoch marginal likelihood -27.67892333553869\n", "500 -th epoch marginal likelihood -27.82299469827059\n", "Done\n", "\n", "[73] [[-1.6680279 -1.46385011 1.68585446]]\n" ] } ], "source": [ "actions = policy.bayes_search(max_num_probes=1, simulator=None, score=\"TS\", interval=0, num_rand_basis = 5000)\n", "print(actions, X[actions])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform external calculations on the obtained candidate points, and register the actions and their scores in a file. The process of reading the file again, running the Bayesian optimization, and obtaining the next candidate point is repeated to advance the Bayesian optimization." ] } ], "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 }