Skip to content

Commit

Permalink
initial commit of BayBE quickstart tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhmanning committed Mar 27, 2024
1 parent 4c704e9 commit b3419c1
Showing 1 changed file with 204 additions and 0 deletions.
204 changes: 204 additions & 0 deletions src/baybe_playground.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1-dimensional continuous data BO test using BayBE"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from baybe.targets import NumericalTarget\n",
"from baybe.objective import Objective\n",
"\n",
"target = NumericalTarget(\n",
" name=\"Yield\",\n",
" mode=\"MAX\",\n",
")\n",
"objective = Objective(mode=\"SINGLE\", targets=[target])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from baybe.parameters import NumericalContinuousParameter\n",
"\n",
"parameters = [\n",
" NumericalContinuousParameter('schwefel1', bounds=(-500,500)),\n",
" NumericalContinuousParameter('schwefel2', bounds=(-500,500))\n",
"\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from baybe.recommenders import SequentialGreedyRecommender\n",
"\n",
"recommender = SequentialGreedyRecommender()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"from baybe.searchspace import SearchSpace\n",
"\n",
"searchspace = SearchSpace.from_product(parameters)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"from baybe import Campaign\n",
"\n",
"campaign = Campaign(searchspace, objective, recommender)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__annotations__',\n",
" '__attrs_attrs__',\n",
" '__attrs_own_setattr__',\n",
" '__attrs_types_resolved__',\n",
" '__class__',\n",
" '__delattr__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getstate__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__setstate__',\n",
" '__sizeof__',\n",
" '__slots__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" '_cached_recommendation',\n",
" '_measurements_exp',\n",
" '_measurements_parameters_comp',\n",
" '_measurements_targets_comp',\n",
" '_validate_strategy',\n",
" '_validate_tolerance_flag',\n",
" 'add_measurements',\n",
" 'from_config',\n",
" 'from_dict',\n",
" 'from_json',\n",
" 'measurements',\n",
" 'n_batches_done',\n",
" 'n_fits_done',\n",
" 'numerical_measurements_must_be_within_tolerance',\n",
" 'objective',\n",
" 'parameters',\n",
" 'recommend',\n",
" 'recommender',\n",
" 'searchspace',\n",
" 'strategy',\n",
" 'targets',\n",
" 'to_dict',\n",
" 'to_json',\n",
" 'validate_config']"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(campaign)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "can only convert an array of size 1 to a Python scalar",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[16], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df \u001b[38;5;241m=\u001b[39m \u001b[43mcampaign\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecommend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2\u001b[0m display(df)\n",
"File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\baybe\\campaign.py:298\u001b[0m, in \u001b[0;36mCampaign.recommend\u001b[1;34m(self, batch_size, batch_quantity)\u001b[0m\n\u001b[0;32m 295\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_measurements_exp\u001b[38;5;241m.\u001b[39mfillna({\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFitNr\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mn_fits_done}, inplace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 297\u001b[0m \u001b[38;5;66;03m# Get the recommended search space entries\u001b[39;00m\n\u001b[1;32m--> 298\u001b[0m rec \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecommender\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecommend\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 299\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msearchspace\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 300\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 301\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_measurements_parameters_comp\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 302\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_measurements_targets_comp\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 303\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 305\u001b[0m \u001b[38;5;66;03m# Cache the recommendations\u001b[39;00m\n\u001b[0;32m 306\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cached_recommendation \u001b[38;5;241m=\u001b[39m rec\u001b[38;5;241m.\u001b[39mcopy()\n",
"File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\baybe\\recommenders\\pure\\bayesian\\base.py:139\u001b[0m, in \u001b[0;36mBayesianRecommender.recommend\u001b[1;34m(self, searchspace, batch_size, train_x, train_y)\u001b[0m\n\u001b[0;32m 136\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _ONNX_INSTALLED \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msurrogate_model, CustomONNXSurrogate):\n\u001b[0;32m 137\u001b[0m CustomONNXSurrogate\u001b[38;5;241m.\u001b[39mvalidate_compatibility(searchspace)\n\u001b[1;32m--> 139\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msetup_acquisition_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43msearchspace\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain_x\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain_y\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 141\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39mrecommend(searchspace, batch_size, train_x, train_y)\n",
"File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\baybe\\recommenders\\pure\\bayesian\\base.py:94\u001b[0m, in \u001b[0;36mBayesianRecommender.setup_acquisition_function\u001b[1;34m(self, searchspace, train_x, train_y)\u001b[0m\n\u001b[0;32m 89\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m train_x \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m train_y \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 90\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[0;32m 91\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBayesian recommenders do not support empty training data yet.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 92\u001b[0m )\n\u001b[1;32m---> 94\u001b[0m best_f \u001b[38;5;241m=\u001b[39m \u001b[43mtrain_y\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitem\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 95\u001b[0m surrogate_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_fit(searchspace, train_x, train_y)\n\u001b[0;32m 96\u001b[0m acquisition_function_cls \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_acquisition_function_cls()\n",
"File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\pandas\\core\\base.py:418\u001b[0m, in \u001b[0;36mIndexOpsMixin.item\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m 417\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(\u001b[38;5;28mself\u001b[39m))\n\u001b[1;32m--> 418\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcan only convert an array of size 1 to a Python scalar\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mValueError\u001b[0m: can only convert an array of size 1 to a Python scalar"
]
}
],
"source": [
"df = campaign.recommend(batch_size=3)\n",
"display(df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "BO",
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit b3419c1

Please sign in to comment.