{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:16:27.204702Z", "start_time": "2020-12-04T07:16:26.685129Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import physbo\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:16:27.209224Z", "start_time": "2020-12-04T07:16:27.205907Z" } }, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:16:27.327094Z", "start_time": "2020-12-04T07:16:27.212499Z" } }, "outputs": [], "source": [ "# Load the data\n", "# X is the N x d dimensional matrix. Each row of X denotes the d-dimensional feature vector of search candidate.\n", "# t is the N-dimensional vector that represents the corresponding negative energy of search candidates.\n", "# ( It is of course unknown in practice. )\n", "X, t = load_data()\n", "\n", "# Normalize the mean and standard deviation along the each column of X to 0 and 1, respectively\n", "X = physbo.misc.centering(X)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:16:27.331486Z", "start_time": "2020-12-04T07:16:27.328472Z" } }, "outputs": [], "source": [ "# Declare the class for calling the simulator.\n", "# In this tutorial, we simply refer to the value of t.\n", "# If you want to apply physbo to other problems, you have to customize this class.\n", "class simulator:\n", " def __init__(self):\n", " _, self.t = load_data()\n", "\n", " def __call__(self, action):\n", " return self.t[action]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:16:27.335749Z", "start_time": "2020-12-04T07:16:27.333084Z" } }, "outputs": [], "source": [ "# Design of policy\n", "\n", "# Declaring the policy by\n", "policy = physbo.search.discrete.policy(test_X=X)\n", "# test_X is the set of candidates which is represented by numpy.array.\n", "# Each row vector represents the feature vector of the corresponding candidate\n", "\n", "# set the seed parameter\n", "policy.set_seed(0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:17:35.483766Z", "start_time": "2020-12-04T07:16:27.336999Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0001-th multiple probe search (random) \n", "\n", "current best f(x) = -0.980054 (best action = 4547) \n", "list of simulation results\n", "f(x)=-1.070602 (action = 15673)\n", "f(x)=-1.009056 (action = 9559)\n", "f(x)=-1.195844 (action = 16927)\n", "f(x)=-0.980054 (action = 4547)\n", "f(x)=-0.992820 (action = 2553)\n", "f(x)=-1.146676 (action = 13144)\n", "f(x)=-1.006255 (action = 10827)\n", "f(x)=-0.999862 (action = 1995)\n", "f(x)=-1.055445 (action = 10763)\n", "f(x)=-1.100970 (action = 16450)\n", "\n", "\n", "0002-th multiple probe search (random) \n", "\n", "current best f(x) = -0.980054 (best action = 4547) \n", "list of simulation results\n", "f(x)=-1.208666 (action = 13085)\n", "f(x)=-1.069404 (action = 15133)\n", "f(x)=-1.031642 (action = 1706)\n", "f(x)=-1.016702 (action = 2464)\n", "f(x)=-1.172569 (action = 17812)\n", "f(x)=-1.082219 (action = 16533)\n", "f(x)=-1.025272 (action = 1336)\n", "f(x)=-1.031761 (action = 10076)\n", "f(x)=-0.984972 (action = 8876)\n", "f(x)=-1.107730 (action = 15577)\n", "\n", "\n", "Start the initial hyper parameter searching ...\n", "Done\n", "\n", "Start the hyper parameter learning ...\n", "0 -th epoch marginal likelihood -22.839738479789983\n", "50 -th epoch marginal likelihood -24.177617139908286\n", "100 -th epoch marginal likelihood -24.831018285821862\n", "150 -th epoch marginal likelihood -25.15416148652374\n", "200 -th epoch marginal likelihood -25.334465627988664\n", "250 -th epoch marginal likelihood -25.455155010625028\n", "300 -th epoch marginal likelihood -25.550091453614577\n", "350 -th epoch marginal likelihood -25.631301279338544\n", "400 -th epoch marginal likelihood -25.702387457449046\n", "450 -th epoch marginal likelihood -25.76434767734444\n", "500 -th epoch marginal likelihood -25.81769784480629\n", "Done\n", "\n", "0003-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.961238 (best action = 7714) \n", "list of simulation results\n", "f(x)=-1.080313 (action = 8128)\n", "f(x)=-0.961238 (action = 7714)\n", "f(x)=-1.087480 (action = 11460)\n", "f(x)=-1.069888 (action = 6744)\n", "f(x)=-1.015223 (action = 5474)\n", "f(x)=-1.044681 (action = 4575)\n", "f(x)=-1.045041 (action = 1216)\n", "f(x)=-1.019301 (action = 11987)\n", "f(x)=-1.018695 (action = 8534)\n", "f(x)=-1.011622 (action = 8978)\n", "\n", "\n", "0004-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.961238 (best action = 7714) \n", "list of simulation results\n", "f(x)=-1.025105 (action = 8117)\n", "f(x)=-0.977188 (action = 10857)\n", "f(x)=-0.971766 (action = 7381)\n", "f(x)=-0.984584 (action = 7177)\n", "f(x)=-1.017794 (action = 5713)\n", "f(x)=-1.145709 (action = 5986)\n", "f(x)=-1.283628 (action = 12002)\n", "f(x)=-1.015117 (action = 7782)\n", "f(x)=-0.978117 (action = 8525)\n", "f(x)=-0.963697 (action = 7086)\n", "\n", "\n", "Start the initial hyper parameter searching ...\n", "Done\n", "\n", "Start the hyper parameter learning ...\n", "0 -th epoch marginal likelihood -3.2889889864784436\n", "50 -th epoch marginal likelihood -19.796771826657235\n", "100 -th epoch marginal likelihood -29.94336691231245\n", "150 -th epoch marginal likelihood -36.238300327338806\n", "200 -th epoch marginal likelihood -40.26364539036532\n", "250 -th epoch marginal likelihood -42.912255396069874\n", "300 -th epoch marginal likelihood -44.70280381595245\n", "350 -th epoch marginal likelihood -45.94613619537765\n", "400 -th epoch marginal likelihood -46.833951803242726\n", "450 -th epoch marginal likelihood -47.487448033889116\n", "500 -th epoch marginal likelihood -47.984818843457695\n", "Done\n", "\n", "0005-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.961238 (best action = 7714) \n", "list of simulation results\n", "f(x)=-0.964688 (action = 8897)\n", "f(x)=-0.978295 (action = 6421)\n", "f(x)=-0.980979 (action = 4735)\n", "f(x)=-0.974453 (action = 11931)\n", "f(x)=-1.046213 (action = 3503)\n", "f(x)=-0.991675 (action = 5957)\n", "f(x)=-1.051965 (action = 7096)\n", "f(x)=-0.963765 (action = 8583)\n", "f(x)=-0.969526 (action = 3477)\n", "f(x)=-1.049169 (action = 8960)\n", "\n", "\n", "0006-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.961238 (best action = 7714) \n", "list of simulation results\n", "f(x)=-0.965994 (action = 4032)\n", "f(x)=-0.974784 (action = 6141)\n", "f(x)=-1.013053 (action = 36)\n", "f(x)=-1.056037 (action = 2965)\n", "f(x)=-0.997708 (action = 3033)\n", "f(x)=-0.975559 (action = 5291)\n", "f(x)=-0.998284 (action = 1829)\n", "f(x)=-0.996815 (action = 8990)\n", "f(x)=-0.968529 (action = 7918)\n", "f(x)=-1.124811 (action = 15021)\n", "\n", "\n", "Start the initial hyper parameter searching ...\n", "Done\n", "\n", "Start the hyper parameter learning ...\n", "0 -th epoch marginal likelihood 17.151172197489586\n", "50 -th epoch marginal likelihood -16.815414216122065\n", "100 -th epoch marginal likelihood -38.47165271282692\n", "150 -th epoch marginal likelihood -52.35633066186492\n", "200 -th epoch marginal likelihood -61.47795734321636\n", "250 -th epoch marginal likelihood -67.60088153452932\n", "300 -th epoch marginal likelihood -71.7922591501419\n", "350 -th epoch marginal likelihood -74.71564792829062\n", "400 -th epoch marginal likelihood -76.79315778066228\n", "450 -th epoch marginal likelihood -78.298652623892\n", "500 -th epoch marginal likelihood -79.41294714759059\n", "Done\n", "\n", "0007-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.959137 (best action = 7437) \n", "list of simulation results\n", "f(x)=-0.974873 (action = 4662)\n", "f(x)=-0.992227 (action = 8362)\n", "f(x)=-0.983504 (action = 11248)\n", "f(x)=-1.013012 (action = 0)\n", "f(x)=-0.959578 (action = 6640)\n", "f(x)=-1.030374 (action = 6548)\n", "f(x)=-0.985278 (action = 10656)\n", "f(x)=-2.497328 (action = 17945)\n", "f(x)=-1.071495 (action = 11)\n", "f(x)=-0.959137 (action = 7437)\n", "\n", "\n", "0008-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.959137 (best action = 7437) \n", "list of simulation results\n", "f(x)=-0.977957 (action = 10952)\n", "f(x)=-0.972339 (action = 4070)\n", "f(x)=-1.079413 (action = 17968)\n", "f(x)=-0.978644 (action = 11524)\n", "f(x)=-0.965976 (action = 7658)\n", "f(x)=-0.999770 (action = 666)\n", "f(x)=-2.832830 (action = 5994)\n", "f(x)=-1.052326 (action = 6025)\n", "f(x)=-1.031994 (action = 9027)\n", "f(x)=-2.497145 (action = 17981)\n", "\n", "\n", "Start the initial hyper parameter searching ...\n", "Done\n", "\n", "Start the hyper parameter learning ...\n", "0 -th epoch marginal likelihood 4.75041850833928\n", "50 -th epoch marginal likelihood -7.331885661121305\n", "100 -th epoch marginal likelihood -14.525898301300089\n", "150 -th epoch marginal likelihood -18.446026654138315\n", "200 -th epoch marginal likelihood -20.559141776660404\n", "250 -th epoch marginal likelihood -22.16012328113436\n", "300 -th epoch marginal likelihood -22.88424156118188\n", "350 -th epoch marginal likelihood -23.621114124835373\n", "400 -th epoch marginal likelihood -24.182813186455007\n", "450 -th epoch marginal likelihood -24.677331910443826\n", "500 -th epoch marginal likelihood -24.966768250598804\n", "Done\n", "\n", "0009-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.959137 (best action = 7437) \n", "list of simulation results\n", "f(x)=-1.046694 (action = 15946)\n", "f(x)=-1.140930 (action = 15476)\n", "f(x)=-1.096760 (action = 4225)\n", "f(x)=-1.006026 (action = 14968)\n", "f(x)=-1.037013 (action = 865)\n", "f(x)=-1.311643 (action = 25)\n", "f(x)=-1.087215 (action = 15830)\n", "f(x)=-1.227756 (action = 22)\n", "f(x)=-1.468925 (action = 15012)\n", "f(x)=-1.062520 (action = 15518)\n", "\n", "\n", "0010-th multiple probe search (EI) \n", "\n", "current best f(x) = -0.959137 (best action = 7437) \n", "list of simulation results\n", "f(x)=-0.996178 (action = 1332)\n", "f(x)=-0.992863 (action = 2589)\n", "f(x)=-1.074451 (action = 2379)\n", "f(x)=-1.013737 (action = 628)\n", "f(x)=-0.992342 (action = 2977)\n", "f(x)=-1.129678 (action = 2062)\n", "f(x)=-1.017355 (action = 1280)\n", "f(x)=-1.023616 (action = 11106)\n", "f(x)=-1.022245 (action = 1095)\n", "f(x)=-0.969706 (action = 4366)\n", "\n", "\n" ] } ], "source": [ "# If you want to perform the initial random search before starting the Bayesian optimization,\n", "# the random sampling is performed by\n", "\n", "res = policy.random_search(\n", " max_num_probes=2, num_search_each_probe=10, simulator=simulator()\n", ")\n", "# Input:\n", "# max_num_probes: number of random search\n", "# num_search_each_probe: number of probes\n", "# simulator: simulator which is defined above\n", "# output: physbo.search.discreate.results.history (class)\n", "\n", "\n", "# multiple probe Bayesian search\n", "# The single query version of COMBO is performed by\n", "res = policy.bayes_search(\n", " max_num_probes=8,\n", " num_search_each_probe=10,\n", " simulator=simulator(),\n", " score=\"EI\",\n", " interval=2,\n", " num_rand_basis=0,\n", ")\n", "\n", "# Input\n", "# max_num_probes: number of searching by Bayesian optimization\n", "# num_search_each_probe: number of probes\n", "# simulator: the class of simulator which is defined above\n", "# score: the type of aquision funciton. TS, EI and PI are available\n", "# interval: the timing for learning the hyper parameter.\n", "# In this case, the hyper parameter is learned at each 20 steps\n", "# If you set the negative value to interval, the hyper parameter learning is not performed\n", "# If you set zero to interval, the hyper parameter learning is performed only at the first step\n", "# num_rand_basis: the number of basis function. If you choose 0, ordinary Gaussian process runs" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T07:17:35.500962Z", "start_time": "2020-12-04T07:17:35.485574Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(x)=\n", "[-1.07060214 -1.00905602 -1.1958441 -0.98005416 -0.99281999 -1.14667577\n", " -1.00625502 -0.99986158 -1.05544467 -1.10097025 -1.20866565 -1.06940391\n", " -1.03164227 -1.01670171 -1.17256876 -1.08221892 -1.02527215 -1.03176082\n", " -0.98497219 -1.1077304 -1.08031283 -0.96123802 -1.08748007 -1.06988832\n", " -1.01522317 -1.04468073 -1.04504063 -1.01930058 -1.01869507 -1.01162164\n", " -1.02510471 -0.97718778 -0.97176629 -0.98458443 -1.0177939 -1.14570866\n", " -1.28362776 -1.01511657 -0.9781168 -0.96369703 -0.96468774 -0.97829476\n", " -0.98097921 -0.9744533 -1.04621299 -0.99167549 -1.05196509 -0.96376526\n", " -0.96952617 -1.04916921 -0.96599372 -0.97478363 -1.0130527 -1.05603682\n", " -0.99770846 -0.97555914 -0.99828355 -0.99681497 -0.96852892 -1.12481102\n", " -0.97487289 -0.99222727 -0.98350446 -1.01301176 -0.9595784 -1.03037382\n", " -0.9852775 -2.49732771 -1.07149506 -0.95913748 -0.97795732 -0.97233883\n", " -1.07941309 -0.97864414 -0.9659761 -0.99977033 -2.83283038 -1.05232556\n", " -1.03199421 -2.4971452 -1.04669399 -1.14092964 -1.09676036 -1.00602618\n", " -1.03701259 -1.31164288 -1.08721541 -1.22775613 -1.46892451 -1.06252009\n", " -0.99617818 -0.99286292 -1.074451 -1.01373668 -0.99234241 -1.12967845\n", " -1.01735526 -1.02361565 -1.02224543 -0.96970555]\n", "current best at each probe\n", "[-0.98005416 -0.98005416 -0.96123802 -0.96123802 -0.96123802 -0.96123802\n", " -0.95913748 -0.95913748 -0.95913748 -0.95913748]\n", "current best action at each probe=\n", "[4547. 4547. 7714. 7714. 7714. 7714. 7437. 7437. 7437. 7437.]\n", "history of chosen actions=\n", "[15673 9559 16927 4547 2553 13144 10827 1995 10763 16450 13085 15133\n", " 1706 2464 17812 16533 1336 10076 8876 15577 8128 7714 11460 6744\n", " 5474 4575 1216 11987 8534 8978 8117 10857 7381 7177 5713 5986\n", " 12002 7782 8525 7086 8897 6421 4735 11931 3503 5957 7096 8583\n", " 3477 8960 4032 6141 36 2965 3033 5291 1829 8990 7918 15021\n", " 4662 8362 11248 0 6640 6548 10656 17945 11 7437 10952 4070\n", " 17968 11524 7658 666 5994 6025 9027 17981 15946 15476 4225 14968\n", " 865 25 15830 22 15012 15518 1332 2589 2379 628 2977 2062\n", " 1280 11106 1095 4366]\n" ] } ], "source": [ "# The result of searching is summarized in the class physbo.search.discrete.results.history()\n", "# res.fx: observed negative energy at each step\n", "# res.chosen_actions: history of choosed actions\n", "# fbest, best_action= res.export_sequence_best_fx(): current best fx and current best action\n", "# that has been observed until each probe\n", "# res.num_runs: number of probes\n", "# res.total_num_search: total number of actions\n", "print(\"f(x)=\")\n", "print(res.fx[0 : res.total_num_search])\n", "\n", "best_fx, best_action = res.export_sequence_best_fx()\n", "\n", "print(\"current best at each probe\")\n", "print(best_fx)\n", "print(\"current best action at each probe=\")\n", "print(best_action)\n", "print(\"history of chosen actions=\")\n", "print(res.chosen_actions[0 : res.total_num_search])" ] }, { "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 }