diff --git a/examples/notebooks/Hyperparameter_Tuning.ipynb b/examples/notebooks/Hyperparameter_Tuning.ipynb index 44bd089bf..660d42b4d 100644 --- a/examples/notebooks/Hyperparameter_Tuning.ipynb +++ b/examples/notebooks/Hyperparameter_Tuning.ipynb @@ -1,32 +1,64 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Hyperparameter tuning of the server-side optimizer with Optuna\n", + "\n", + "This notebook shows specifically how to tune the *learning rate* of *FedAdam* using the Optuna package. Tuning of other hyperparameter and/or other server-side optimizers can be done analogously. The notebook *Aggregators.ipynb* shows how to use different aggregators with the FEDn Python API.\n", + "\n", + "For a complete list of implemented interfaces, please refer to the [FEDn APIs](https://fedn.readthedocs.io/en/latest/fedn.network.api.html#module-fedn.network.api.client). \n", + "\n", + "For implementation details related to how aggregators are implemented, we recommend to read [FEDn Framework Extensions](https://www.scaleoutsystems.com/post/fedn-framework-extensions).\n", + "\n", + "Before starting this tutorial, make sure you have a project running in FEDn Studio and have created the compute package and the initial model. If you're not sure how to do this, please follow the instructions in sections 1, 2, and 3 of the [quickstart guide](https://fedn.readthedocs.io/en/latest/quickstart.html). " + ] + }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "from fedn import APIClient\n", "import time\n", "import json\n", - "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this example, we assume the project is hosted on the public FEDn Studio. You can find the CONTROLLER_HOST address in the project dashboard.\n", + "\n", + "**Note:** If you're using a local sandbox, the CONTROLLER_HOST will be \"localhost,\" and the CONTROLLER_PORT will be 8092.\n", + "\n", + "Next, you'll need to generate an access token. To do this, go to the project page in FEDn Studio, click on \"Settings,\" then \"Generate token.\" Copy the access token from the Studio and paste it into the notebook. In case you need further details, have a look at the [FEDn ClientAPIs](https://fedn.readthedocs.io/en/latest/apiclient.html#)." + ] + }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ - "CONTROLLER_HOST = 'fedn.scaleoutsystems.com/' \n", + "CONTROLLER_HOST = 'fedn.scaleoutsystems.com/' # TODO byt ut till lokal\n", "ACCESS_TOKEN = ''\n", "client = APIClient(CONTROLLER_HOST,token=ACCESS_TOKEN, secure=True,verify=True)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Initialize FEDn with the compute package and seed model. Note that these files needs to be created separately. If you're not sure how to do this, please follow the instructions only in section 3 of the [quickstart guide](https://fedn.readthedocs.io/en/latest/quickstart.html#create-the-compute-package-and-seed-model)." + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ @@ -43,16 +75,20 @@ "\n", "Optuna expects an objective function - the function that evaluates a certain set of hyperparameter values. In this example, we will use the test accuracy as a validation score and we want to maximize it.\n", "\n", - "For each set of hyperparameter values, each 'trial', we will start a new session using the FEDn Python API. In each session/trial, we will select the model with the highest test accuracy and use that in the Optuna objective function to evaluate the trial." + "For each set of hyperparameter values, each `trial`, we will start a new session using the FEDn Python API. In each session/trial, we will select the model with the highest test accuracy and use that in the Optuna objective function to evaluate the trial.\n", + "\n", + "The `objective()` function gives us some flexibility in how we choose to evaluate the choice of hyperparameters in each trial/session. Below are two examples on how to calculate the attained validation accuracy in a session.\n", + "\n", + "* ``" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ - "# Helper function to get the model with the highest test accuracy within a session\n", + "# Helper function to get the highest test accuracy within a session\n", "def get_highest_test_accuracy_in_session(client, n_rounds):\n", " best_accuracy = 0\n", " validations_in_session = client.get_validations()['result'][:n_rounds]\n", @@ -61,12 +97,53 @@ " if val_accuracy > best_accuracy:\n", " best_accuracy = val_accuracy\n", "\n", - " return best_accuracy" + " return best_accuracy\n", + "\n", + "# Helper function to get the average test accuracy over the last 10 rounds in a session\n", + "def get_test_accuracy_in_session_smooth(client, n_rounds):\n", + " \n", + " n_rounds_to_avg = 5\n", + " if n_rounds_to_avg > n_rounds:\n", + " n_rounds_to_avg = n_rounds\n", + " \n", + " # New\n", + " models = client.get_model_trail()[-n_rounds_to_avg:] # model with index -1 lacks validations -> seed model??\n", + " # print(f'models: {len(models)}\\n {models}')\n", + " model_test_acc = []\n", + "\n", + " # Loop over the last 'n_rounds_to_avg' rounds\n", + " for model_index, model in enumerate(models):\n", + " \n", + " model_id = model[\"model\"]\n", + " validations = client.get_validations(model_id=model_id)\n", + " # print(f'Validation nr. {model_index}: {validations}')\n", + " a = []\n", + "\n", + " # Loop over all contributing clients\n", + " for validation in validations['result']: \n", + " metrics = json.loads(validation['data'])\n", + " a.append(metrics['test_accuracy'])\n", + " \n", + " model_test_acc.append(a)\n", + " print(f'Model id: {model_id}, Validations: {validations}')\n", + "\n", + " mean_val_accuracies = [np.mean(x) for x in model_test_acc]\n", + " print(f'Mean accuracy: {mean_val_accuracies}')\n", + "\n", + " # Old\n", + " # validations_to_avg = client.get_validations()['result'][:n_rounds_to_avg]\n", + " # val_accuracies = [json.loads(validation['data'])['test_accuracy'] for validation in validations_to_avg]\n", + "\n", + " mean_val_accuracy = np.mean(mean_val_accuracies)\n", + " print(f'Validation accuracy scores:\\n{mean_val_accuracies}')\n", + " print(f'Average validation accuracy: {mean_val_accuracy}')\n", + "\n", + " return mean_val_accuracy" ] }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ @@ -75,24 +152,20 @@ "# Objective function which will be sent to Optuna to evaluate the selection of hyperparameter values\n", "def objective(trial):\n", " # Number of rounds per session\n", - " n_rounds = 1\n", + " n_rounds = 5\n", "\n", " # Suggest hyperparameter priors\n", - " learning_rate = trial.suggest_float(\"learning_rate\", 1e-4, 1e-1, log=True)\n", + " learning_rate = trial.suggest_float(\"learning_rate\", 1e-3, 1e-1, log=True)\n", "\n", " # Set session configurations (from seed model)\n", " session_config = {\n", " \"helper\": \"numpyhelper\",\n", - " # \"id\": session_id,\n", " \"aggregator\": \"fedopt\",\n", " \"aggregator_kwargs\": {\n", " \"serveropt\": \"adam\",\n", - " \"learning_rate\": learning_rate,\n", - " \"beta1\": 0.9,\n", - " \"beta2\": 0.99,\n", - " \"tau\": 1e-4\n", + " \"learning_rate\": learning_rate\n", " },\n", - " \"model_id\": seed_model['model'], # Restart from seed model in each new session\n", + " \"model_id\": seed_model['model'],\n", " \"rounds\": n_rounds\n", " }\n", "\n", @@ -100,12 +173,12 @@ " result_fedadam = client.start_session(**session_config)\n", " session_id = result_fedadam['config']['session_id']\n", " \n", - " # Wait while the current session is active\n", - " while client.get_session_status(session_id) != 'Finished':\n", - " time.sleep(1)\n", + " # Wait for the session to finish\n", + " while not client.session_is_finished(session_id):\n", + " time.sleep(2)\n", " \n", - " # Return highest 'test' (validation) accuracy\n", - " return get_highest_test_accuracy_in_session(client=client, n_rounds=n_rounds)" + " # Return validation accuracy for session\n", + " return get_test_accuracy_in_session_smooth(client=client, n_rounds=n_rounds)" ] }, { @@ -121,124 +194,61 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "[I 2024-09-06 11:22:59,025] A new study created in memory with name: no-name-b1a1a339-93d1-4420-9006-00a43d546dd9\n" + "[I 2024-09-09 11:40:23,930] A new study created in memory with name: no-name-9f40df3c-4c9f-4283-a7df-2608de273a5f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model id: f8df5900-233b-4ee8-aa27-cbb197f517b4, Validations: {'count': 2, 'result': [{'correlation_id': '765ae8e1-bcfc-41b9-90e5-c47d122139ee', 'data': '{\"training_loss\": 2.9918696880340576, \"training_accuracy\": 0.2761666774749756, \"test_loss\": 3.145538568496704, \"test_accuracy\": 0.25600001215934753}', 'id': '66dec299819bae1528aeeefd', 'meta': '', 'model_id': 'f8df5900-233b-4ee8-aa27-cbb197f517b4', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client735', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:40:41.057123Z'}, {'correlation_id': '4bbb7175-52d1-4ad5-82cc-b97f8b728d19', 'data': '{\"training_loss\": 3.0555734634399414, \"training_accuracy\": 0.26350000500679016, \"test_loss\": 3.146380662918091, \"test_accuracy\": 0.25200000405311584}', 'id': '66dec299819bae1528aeeefb', 'meta': '', 'model_id': 'f8df5900-233b-4ee8-aa27-cbb197f517b4', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client268', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:40:41.056228Z'}]}\n", + "Model id: ac4fb06e-0186-4220-b0ea-dda55bcba92a, Validations: {'count': 2, 'result': [{'correlation_id': 'ddcf1b24-8b55-4cf5-9133-bc35637c2bf3', 'data': '{\"training_loss\": 5.030279636383057, \"training_accuracy\": 0.2666666805744171, \"test_loss\": 4.994809150695801, \"test_accuracy\": 0.27399998903274536}', 'id': '66dec2a7819bae1528aeef0c', 'meta': '', 'model_id': 'ac4fb06e-0186-4220-b0ea-dda55bcba92a', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client735', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:40:55.140524Z'}, {'correlation_id': 'c22bb9c4-4557-4b6e-8aac-28e907393ea4', 'data': '{\"training_loss\": 4.781228065490723, \"training_accuracy\": 0.2773333191871643, \"test_loss\": 4.559061527252197, \"test_accuracy\": 0.3059999942779541}', 'id': '66dec2a7819bae1528aeef09', 'meta': '', 'model_id': 'ac4fb06e-0186-4220-b0ea-dda55bcba92a', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client268', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:40:55.025714Z'}]}\n", + "Model id: 65343657-8bd2-4e48-9bfc-9dc1c90f766b, Validations: {'count': 2, 'result': [{'correlation_id': '9414bb80-d5c1-4c1b-a67f-c3500aa02961', 'data': '{\"training_loss\": 2.4126389026641846, \"training_accuracy\": 0.4465000033378601, \"test_loss\": 2.2521755695343018, \"test_accuracy\": 0.44699999690055847}', 'id': '66dec2b3819bae1528aeef19', 'meta': '', 'model_id': '65343657-8bd2-4e48-9bfc-9dc1c90f766b', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client268', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:41:07.439818Z'}, {'correlation_id': '1c6fa889-309e-4bc9-ae20-7f45bc7342fc', 'data': '{\"training_loss\": 2.369710922241211, \"training_accuracy\": 0.4494999945163727, \"test_loss\": 2.3147921562194824, \"test_accuracy\": 0.4399999976158142}', 'id': '66dec2b3819bae1528aeef17', 'meta': '', 'model_id': '65343657-8bd2-4e48-9bfc-9dc1c90f766b', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client735', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:41:07.436095Z'}]}\n", + "Model id: 56c93771-f8e7-4987-9f1f-fc5d050a415e, Validations: {'count': 2, 'result': [{'correlation_id': '7aded382-761f-4b55-8984-c24863d09d20', 'data': '{\"training_loss\": 0.8602045774459839, \"training_accuracy\": 0.7173333168029785, \"test_loss\": 0.9392972588539124, \"test_accuracy\": 0.6869999766349792}', 'id': '66dec2bf819bae1528aeef27', 'meta': '', 'model_id': '56c93771-f8e7-4987-9f1f-fc5d050a415e', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client268', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:41:19.240899Z'}, {'correlation_id': '2c35452a-393b-40c9-8f6f-e800419e16d2', 'data': '{\"training_loss\": 0.8731573224067688, \"training_accuracy\": 0.7064999938011169, \"test_loss\": 1.0654996633529663, \"test_accuracy\": 0.6869999766349792}', 'id': '66dec2bf819bae1528aeef25', 'meta': '', 'model_id': '56c93771-f8e7-4987-9f1f-fc5d050a415e', 'receiver': {'clientId': '', 'name': 'hyperparametertuning-hrt-fedn', 'role': 'COMBINER'}, 'sender': {'clientId': '', 'name': 'client735', 'role': 'WORKER'}, 'session_id': 'cc9be434-4dda-4dc5-b40e-762ff38cefac', 'timestamp': '2024-09-09T09:41:19.225037Z'}]}\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "/var/folders/p9/37n8_h0j3w136cfjm88xkpcr0000gn/T/ipykernel_13870/468742983.py:8: FutureWarning:\n", - "\n", - "suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.\n", - "\n", - "[I 2024-09-06 11:23:10,169] Trial 0 finished with value: 0.18700000643730164 and parameters: {'learning_rate': 0.01060256463073004}. Best is trial 0 with value: 0.18700000643730164.\n", - "[I 2024-09-06 11:23:23,317] Trial 1 finished with value: 0.4269999861717224 and parameters: {'learning_rate': 0.06363319752806389}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:23:34,356] Trial 2 finished with value: 0.2070000022649765 and parameters: {'learning_rate': 0.044054634905094}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:23:44,244] Trial 3 finished with value: 0.3019999861717224 and parameters: {'learning_rate': 0.005457455596524018}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:23:55,219] Trial 4 finished with value: 0.23999999463558197 and parameters: {'learning_rate': 0.0006534358443990889}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:24:07,568] Trial 5 finished with value: 0.1420000046491623 and parameters: {'learning_rate': 0.005409564385563341}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:24:18,643] Trial 6 finished with value: 0.2409999966621399 and parameters: {'learning_rate': 0.000296117238409861}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:24:30,917] Trial 7 finished with value: 0.13600000739097595 and parameters: {'learning_rate': 0.0001548659658268065}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:24:41,906] Trial 8 finished with value: 0.13199999928474426 and parameters: {'learning_rate': 0.0059773222630647126}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:24:53,059] Trial 9 finished with value: 0.2669999897480011 and parameters: {'learning_rate': 0.009610312272318417}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:25:05,070] Trial 10 finished with value: 0.39899998903274536 and parameters: {'learning_rate': 0.07936629680356555}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:25:18,276] Trial 11 finished with value: 0.2280000001192093 and parameters: {'learning_rate': 0.0999017085272383}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:25:32,440] Trial 12 finished with value: 0.09700000286102295 and parameters: {'learning_rate': 0.03030693483792421}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:25:43,418] Trial 13 finished with value: 0.3709999918937683 and parameters: {'learning_rate': 0.09839071999176227}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:25:54,343] Trial 14 finished with value: 0.11599999666213989 and parameters: {'learning_rate': 0.0018120798841072265}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:26:05,276] Trial 15 finished with value: 0.164000004529953 and parameters: {'learning_rate': 0.02630581835365611}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:26:16,256] Trial 16 finished with value: 0.3959999978542328 and parameters: {'learning_rate': 0.0015862265658428628}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:26:28,358] Trial 17 finished with value: 0.16599999368190765 and parameters: {'learning_rate': 0.018963170798261392}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:26:38,257] Trial 18 finished with value: 0.4099999964237213 and parameters: {'learning_rate': 0.05591498203997656}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:26:49,235] Trial 19 finished with value: 0.23499999940395355 and parameters: {'learning_rate': 0.0507252826898167}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:26:59,122] Trial 20 finished with value: 0.27799999713897705 and parameters: {'learning_rate': 0.015598714196031616}. Best is trial 1 with value: 0.4269999861717224.\n", - "[I 2024-09-06 11:27:11,184] Trial 21 finished with value: 0.453000009059906 and parameters: {'learning_rate': 0.04981982237560933}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:27:22,170] Trial 22 finished with value: 0.2160000056028366 and parameters: {'learning_rate': 0.04472041885602553}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:27:33,156] Trial 23 finished with value: 0.27900001406669617 and parameters: {'learning_rate': 0.04569663540155219}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:27:44,127] Trial 24 finished with value: 0.30799999833106995 and parameters: {'learning_rate': 0.02409255925048435}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:27:57,214] Trial 25 finished with value: 0.3959999978542328 and parameters: {'learning_rate': 0.012153379377141215}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:28:08,184] Trial 26 finished with value: 0.4350000023841858 and parameters: {'learning_rate': 0.06204357797505839}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:28:19,284] Trial 27 finished with value: 0.26899999380111694 and parameters: {'learning_rate': 0.0318047942959313}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:28:31,285] Trial 28 finished with value: 0.36500000953674316 and parameters: {'learning_rate': 0.0026889985534921607}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:28:43,293] Trial 29 finished with value: 0.17599999904632568 and parameters: {'learning_rate': 0.008406962615909752}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:28:54,275] Trial 30 finished with value: 0.36399999260902405 and parameters: {'learning_rate': 0.07570103253376405}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:29:05,232] Trial 31 finished with value: 0.1469999998807907 and parameters: {'learning_rate': 0.059086197752773524}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:29:17,298] Trial 32 finished with value: 0.20600000023841858 and parameters: {'learning_rate': 0.03714901133834086}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:29:29,314] Trial 33 finished with value: 0.3330000042915344 and parameters: {'learning_rate': 0.0646915046763561}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:29:41,414] Trial 34 finished with value: 0.23499999940395355 and parameters: {'learning_rate': 0.018225411751837776}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:29:52,437] Trial 35 finished with value: 0.4059999883174896 and parameters: {'learning_rate': 0.060873750215417036}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:30:03,385] Trial 36 finished with value: 0.23199999332427979 and parameters: {'learning_rate': 0.0005937941753721039}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:30:15,629] Trial 37 finished with value: 0.14399999380111694 and parameters: {'learning_rate': 0.03794230073789979}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:30:27,769] Trial 38 finished with value: 0.3569999933242798 and parameters: {'learning_rate': 0.021776847720301837}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:30:39,810] Trial 39 finished with value: 0.4009999930858612 and parameters: {'learning_rate': 0.012825763536089739}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:30:51,808] Trial 40 finished with value: 0.4180000126361847 and parameters: {'learning_rate': 0.006945279111941595}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:31:01,716] Trial 41 finished with value: 0.3070000112056732 and parameters: {'learning_rate': 0.003924318415667345}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:31:12,678] Trial 42 finished with value: 0.19499999284744263 and parameters: {'learning_rate': 0.0009962905683287265}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:31:25,797] Trial 43 finished with value: 0.14900000393390656 and parameters: {'learning_rate': 0.007485623216043211}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:31:37,890] Trial 44 finished with value: 0.34299999475479126 and parameters: {'learning_rate': 0.08121672594329421}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:31:47,800] Trial 45 finished with value: 0.18799999356269836 and parameters: {'learning_rate': 0.03218747043584}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:31:58,778] Trial 46 finished with value: 0.3499999940395355 and parameters: {'learning_rate': 0.0043209290517303035}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:32:08,667] Trial 47 finished with value: 0.2070000022649765 and parameters: {'learning_rate': 0.09007673305976491}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:32:19,734] Trial 48 finished with value: 0.15299999713897705 and parameters: {'learning_rate': 0.00011242006456928975}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:32:32,488] Trial 49 finished with value: 0.13199999928474426 and parameters: {'learning_rate': 0.052926437331853586}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:32:43,824] Trial 50 finished with value: 0.19099999964237213 and parameters: {'learning_rate': 0.015080515584708165}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:32:54,136] Trial 51 finished with value: 0.41499999165534973 and parameters: {'learning_rate': 0.06571961786071892}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:33:05,493] Trial 52 finished with value: 0.22300000488758087 and parameters: {'learning_rate': 0.06853902191868609}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:33:18,187] Trial 53 finished with value: 0.17900000512599945 and parameters: {'learning_rate': 0.043188154256233874}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:33:30,701] Trial 54 finished with value: 0.2759999930858612 and parameters: {'learning_rate': 0.03137506646247659}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:33:41,351] Trial 55 finished with value: 0.36899998784065247 and parameters: {'learning_rate': 0.09815877477219788}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:33:54,493] Trial 56 finished with value: 0.14499999582767487 and parameters: {'learning_rate': 0.024965815660978004}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:34:04,881] Trial 57 finished with value: 0.335999995470047 and parameters: {'learning_rate': 0.0002369246671375477}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:34:17,437] Trial 58 finished with value: 0.13300000131130219 and parameters: {'learning_rate': 0.05402719364497381}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:34:28,108] Trial 59 finished with value: 0.30399999022483826 and parameters: {'learning_rate': 0.04024469007474665}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:34:40,344] Trial 60 finished with value: 0.3540000021457672 and parameters: {'learning_rate': 0.0021520115377091278}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:34:51,334] Trial 61 finished with value: 0.17000000178813934 and parameters: {'learning_rate': 0.06777601481845777}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:35:02,328] Trial 62 finished with value: 0.2549999952316284 and parameters: {'learning_rate': 0.054552697374482345}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:35:14,370] Trial 63 finished with value: 0.24799999594688416 and parameters: {'learning_rate': 0.07824127973056091}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:35:25,393] Trial 64 finished with value: 0.15299999713897705 and parameters: {'learning_rate': 0.062190956331727144}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:35:36,519] Trial 65 finished with value: 0.21699999272823334 and parameters: {'learning_rate': 0.04574533489252647}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:35:46,388] Trial 66 finished with value: 0.31700000166893005 and parameters: {'learning_rate': 0.026700039728034024}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:35:56,731] Trial 67 finished with value: 0.35499998927116394 and parameters: {'learning_rate': 0.035572253575668344}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:36:07,788] Trial 68 finished with value: 0.38100001215934753 and parameters: {'learning_rate': 0.020947893979189017}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:36:18,778] Trial 69 finished with value: 0.39399999380111694 and parameters: {'learning_rate': 0.09807853814362605}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:36:30,423] Trial 70 finished with value: 0.13099999725818634 and parameters: {'learning_rate': 0.074008662788353}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:36:43,473] Trial 71 finished with value: 0.17800000309944153 and parameters: {'learning_rate': 0.006494329870318023}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:36:54,514] Trial 72 finished with value: 0.27900001406669617 and parameters: {'learning_rate': 0.013324381014246824}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:37:05,715] Trial 73 finished with value: 0.4050000011920929 and parameters: {'learning_rate': 0.047348523959182987}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:37:20,038] Trial 74 finished with value: 0.28600001335144043 and parameters: {'learning_rate': 0.05213136025170205}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:37:33,497] Trial 75 finished with value: 0.22100000083446503 and parameters: {'learning_rate': 0.0468237789021135}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:37:46,689] Trial 76 finished with value: 0.23499999940395355 and parameters: {'learning_rate': 0.009671712975184507}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:37:58,076] Trial 77 finished with value: 0.4050000011920929 and parameters: {'learning_rate': 0.028449834843744366}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:38:10,357] Trial 78 finished with value: 0.35499998927116394 and parameters: {'learning_rate': 0.03736743378716483}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:38:21,594] Trial 79 finished with value: 0.33799999952316284 and parameters: {'learning_rate': 0.0011102163493235233}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:38:33,990] Trial 80 finished with value: 0.15299999713897705 and parameters: {'learning_rate': 0.059419725114923834}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:38:44,958] Trial 81 finished with value: 0.20100000500679016 and parameters: {'learning_rate': 0.029590575438061956}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:38:57,152] Trial 82 finished with value: 0.35499998927116394 and parameters: {'learning_rate': 0.017734648001224127}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:39:07,228] Trial 83 finished with value: 0.40700000524520874 and parameters: {'learning_rate': 0.08418584424126585}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:39:19,411] Trial 84 finished with value: 0.14499999582767487 and parameters: {'learning_rate': 0.08334759428158998}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:39:31,771] Trial 85 finished with value: 0.10499999672174454 and parameters: {'learning_rate': 0.06386707358622472}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:39:43,951] Trial 86 finished with value: 0.16899999976158142 and parameters: {'learning_rate': 0.08176816323358746}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:39:58,405] Trial 87 finished with value: 0.1550000011920929 and parameters: {'learning_rate': 0.04691662345629954}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:40:09,377] Trial 88 finished with value: 0.3019999861717224 and parameters: {'learning_rate': 0.0005092474657505243}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:40:21,584] Trial 89 finished with value: 0.14100000262260437 and parameters: {'learning_rate': 0.06835465614566856}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:40:31,586] Trial 90 finished with value: 0.1940000057220459 and parameters: {'learning_rate': 0.040926964321475}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:40:43,624] Trial 91 finished with value: 0.3370000123977661 and parameters: {'learning_rate': 0.05663192019646022}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:40:55,919] Trial 92 finished with value: 0.22499999403953552 and parameters: {'learning_rate': 0.03407804204205137}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:41:08,004] Trial 93 finished with value: 0.3490000069141388 and parameters: {'learning_rate': 0.083789776074211}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:41:19,210] Trial 94 finished with value: 0.13199999928474426 and parameters: {'learning_rate': 0.02789909609279049}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:41:31,487] Trial 95 finished with value: 0.3779999911785126 and parameters: {'learning_rate': 0.04909602447498623}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:41:43,551] Trial 96 finished with value: 0.3160000145435333 and parameters: {'learning_rate': 0.07135979861985685}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:41:55,018] Trial 97 finished with value: 0.13699999451637268 and parameters: {'learning_rate': 0.02340684816934831}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:42:07,090] Trial 98 finished with value: 0.38999998569488525 and parameters: {'learning_rate': 0.04183807409164346}. Best is trial 21 with value: 0.453000009059906.\n", - "[I 2024-09-06 11:42:18,542] Trial 99 finished with value: 0.335999995470047 and parameters: {'learning_rate': 0.09709298119891982}. Best is trial 21 with value: 0.453000009059906.\n" + "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3464: RuntimeWarning: Mean of empty slice.\n", + " return _methods._mean(a, axis=axis, dtype=dtype,\n", + "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/_methods.py:192: RuntimeWarning: invalid value encountered in scalar divide\n", + " ret = ret.dtype.type(ret / rcount)\n", + "[W 2024-09-09 11:41:29,099] Trial 0 failed with parameters: {'learning_rate': 0.09377062950652325} because of the following error: The value nan is not acceptable.\n", + "[W 2024-09-09 11:41:29,100] Trial 0 failed with value nan.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model id: e4a747bb-f5a2-4845-898d-ca6e9d005bb0, Validations: {'count': 0, 'result': []}\n", + "Mean accuracy: [0.2540000081062317, 0.28999999165534973, 0.44349999725818634, 0.6869999766349792, nan]\n", + "Validation accuracy scores:\n", + "[0.2540000081062317, 0.28999999165534973, 0.44349999725818634, 0.6869999766349792, nan]\n", + "Average validation accuracy: nan\n" + ] + }, + { + "ename": "ValueError", + "evalue": "No trials are completed yet.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[80], line 6\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# Optimize hyperparameters\u001b[39;00m\n\u001b[1;32m 5\u001b[0m study\u001b[38;5;241m.\u001b[39moptimize(objective, n_trials\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBest hyperparameters:\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43mstudy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbest_params\u001b[49m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBest value:\u001b[39m\u001b[38;5;124m\"\u001b[39m, study\u001b[38;5;241m.\u001b[39mbest_value)\n", + "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/optuna/study/study.py:119\u001b[0m, in \u001b[0;36mStudy.best_params\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[38;5;129m@property\u001b[39m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mbest_params\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, Any]:\n\u001b[1;32m 109\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Return parameters of the best trial in the study.\u001b[39;00m\n\u001b[1;32m 110\u001b[0m \n\u001b[1;32m 111\u001b[0m \u001b[38;5;124;03m .. note::\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 116\u001b[0m \n\u001b[1;32m 117\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 119\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbest_trial\u001b[49m\u001b[38;5;241m.\u001b[39mparams\n", + "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/optuna/study/study.py:162\u001b[0m, in \u001b[0;36mStudy.best_trial\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_is_multi_objective():\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[1;32m 158\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA single best trial cannot be retrieved from a multi-objective study. Consider \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124musing Study.best_trials to retrieve a list containing the best trials.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 160\u001b[0m )\n\u001b[0;32m--> 162\u001b[0m best_trial \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_storage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_best_trial\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_study_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 164\u001b[0m \u001b[38;5;66;03m# If the trial with the best value is infeasible, select the best trial from all feasible\u001b[39;00m\n\u001b[1;32m 165\u001b[0m \u001b[38;5;66;03m# trials. Note that the behavior is undefined when constrained optimization without the\u001b[39;00m\n\u001b[1;32m 166\u001b[0m \u001b[38;5;66;03m# violation value in the best-valued trial.\u001b[39;00m\n\u001b[1;32m 167\u001b[0m constraints \u001b[38;5;241m=\u001b[39m best_trial\u001b[38;5;241m.\u001b[39msystem_attrs\u001b[38;5;241m.\u001b[39mget(_CONSTRAINTS_KEY)\n", + "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/optuna/storages/_in_memory.py:232\u001b[0m, in \u001b[0;36mInMemoryStorage.get_best_trial\u001b[0;34m(self, study_id)\u001b[0m\n\u001b[1;32m 229\u001b[0m best_trial_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_studies[study_id]\u001b[38;5;241m.\u001b[39mbest_trial_id\n\u001b[1;32m 231\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m best_trial_id \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 232\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;124mNo trials are completed yet.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 233\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_studies[study_id]\u001b[38;5;241m.\u001b[39mdirections) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 234\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[1;32m 235\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBest trial can be obtained only for single-objective optimization.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 236\u001b[0m )\n", + "\u001b[0;31mValueError\u001b[0m: No trials are completed yet." ] } ], @@ -247,7 +257,7 @@ "study = optuna.create_study(direction=\"maximize\")\n", "\n", "# Optimize hyperparameters\n", - "study.optimize(objective, n_trials=100) #19m19s to run 100 trials (session) with one round each\n", + "study.optimize(objective, n_trials=1)\n", "print(\"Best hyperparameters:\", study.best_params)\n", "print(\"Best value:\", study.best_value)" ] @@ -268,9 +278,7 @@ "source": [ "import optuna.visualization as vis\n", "\n", - "# vis.plot_optimization_history(study)\n", - "# vis.plot_param_importances(study)\n", - "\n" + "vis.plot_slice(study)" ] }, { @@ -1612,7 +1620,7 @@ } ], "source": [ - "vis.plot_slice(study)" + "vis.plot_optimization_history(study)" ] } ],