Skip to content

Commit

Permalink
Add Optuna jupyter example (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothiraldan authored Oct 7, 2024
1 parent 43a02e9 commit f448dfe
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- integrations/llm/comet-llm/notebooks/CometLLM_hello_world.ipynb
- integrations/model-evaluation/gradio/notebooks/Gradio_and_Comet.ipynb
- integrations/model-evaluation/gradio/notebooks/Logging_Model_Inferences_with_Comet_and_Gradio.ipynb
- integrations/model-optimization/optuna/notebooks/Comet_with_optuna.ipynb
- integrations/model-optimization/ray-tune/notebooks/Comet_and_Ray.ipynb
- integrations/model-training/composer/notebooks/comet_composer.ipynb
- integrations/model-training/fastai/notebooks/fastai_hello_world.ipynb
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a4f94350-269b-4e7e-a4ae-afb0804b201f",
"metadata": {},
"source": [
"# Comet with Optuna\n",
"\n",
"<img src=\"https://cdn.comet.ml/img/notebook_logo.png\">\n",
"<img src=\"https://optuna.org/assets/img/optuna-logo.png\">\n",
"\n",
"[Optuna](https://optuna.org/) is an open source hyperparameter optimization framework to automate hyperparameter search.\n",
"\n",
"Instrument Optuna with Comet to start managing experiments and track hyperparameters for faster and easier reproducibility and collaboration.\n",
"\n",
"Get a preview for what's to come. Check out a completed experiment created from this notebook [here](https://www.comet.com/examples/comet-example-optuna-notebook/)"
]
},
{
"cell_type": "markdown",
"id": "5989b8ec-d587-4658-8a70-e6dfa7391f64",
"metadata": {},
"source": [
"# Install dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bef2b18-d046-404f-a55c-eacdcf17506c",
"metadata": {},
"outputs": [],
"source": [
"%pip install \"comet_ml>=3.33.10\" \"optuna>=4.0.0\" \"optuna-integration>=4.0.0\""
]
},
{
"cell_type": "markdown",
"id": "2a4b2cf0-a0a7-499a-b09c-92a00e43d7d6",
"metadata": {},
"source": [
"# Login to Comet"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5f3da5e-b71a-4c44-a79e-5d1e0dec5837",
"metadata": {},
"outputs": [],
"source": [
"import comet_ml\n",
"\n",
"comet_ml.login()"
]
},
{
"cell_type": "markdown",
"id": "585d806c-8c8e-471a-bffb-5f5a6f2c15fd",
"metadata": {},
"source": [
"# Create the Study and Comet Callback"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28d1913f-5ee1-446d-a53b-74822a7ac041",
"metadata": {},
"outputs": [],
"source": [
"import optuna\n",
"from optuna_integration.comet import CometCallback\n",
"\n",
"study = optuna.create_study()\n",
"comet = CometCallback(\n",
" study,\n",
" project_name=\"comet-example-optuna-hello-world-notebook\",\n",
" metric_names=[\"score\"],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "dd19a3c4-19ff-4dbf-bda7-e84d1cb98ee7",
"metadata": {},
"source": [
"# Create the objective function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbaef3ce-3568-424b-80e8-ab2594ebc804",
"metadata": {},
"outputs": [],
"source": [
"@comet.track_in_comet()\n",
"def objective(trial):\n",
" x = trial.suggest_float(\"x\", -10, 10)\n",
" objective = (x - 2) ** 2\n",
"\n",
" return objective"
]
},
{
"cell_type": "markdown",
"id": "4c2dede2-58ce-4a2e-9097-75b7c6694e0a",
"metadata": {},
"source": [
"# Start the optimization"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7aa323f-8faf-41d6-b318-049add1f4422",
"metadata": {},
"outputs": [],
"source": [
"study.optimize(objective, n_trials=20, callbacks=[comet])\n",
"\n",
"best_params = study.best_params\n",
"found_x = best_params[\"x\"]\n",
"print(\"Found x: {}, (x - 2)^2: {}\".format(found_x, (found_x - 2) ** 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7baf1d2f-561f-4524-b9d3-c08b572eedba",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit f448dfe

Please sign in to comment.