{"id":15176681,"url":"https://github.com/minyus/optkeras","last_synced_at":"2025-10-01T16:30:38.108Z","repository":{"id":57449414,"uuid":"170270539","full_name":"Minyus/optkeras","owner":"Minyus","description":"OptKeras: wrapper around Keras and Optuna for hyperparameter optimization","archived":true,"fork":false,"pushed_at":"2020-04-01T23:36:39.000Z","size":39010,"stargazers_count":30,"open_issues_count":3,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-17T12:46:46.439Z","etag":null,"topics":["deep","deep-learning","hyperparameter-optimization","hyperparameters","keras","neural-network","optuna"],"latest_commit_sha":null,"homepage":"https://medium.com/@Minyus86/optkeras-112bcc34ec73","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Minyus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-12T07:13:00.000Z","updated_at":"2024-11-07T05:37:32.000Z","dependencies_parsed_at":"2022-09-26T17:31:17.022Z","dependency_job_id":null,"html_url":"https://github.com/Minyus/optkeras","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Minyus%2Foptkeras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Minyus%2Foptkeras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Minyus%2Foptkeras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Minyus%2Foptkeras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Minyus","download_url":"https://codeload.github.com/Minyus/optkeras/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234882550,"owners_count":18901290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep","deep-learning","hyperparameter-optimization","hyperparameters","keras","neural-network","optuna"],"created_at":"2024-09-27T13:40:19.744Z","updated_at":"2025-10-01T16:30:32.829Z","avatar_url":"https://github.com/Minyus.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OptKeras, a wrapper around Keras and Optuna\n\n[![PyPI version](https://badge.fury.io/py/optkeras.svg)](https://badge.fury.io/py/optkeras)\n![Python Version](https://img.shields.io/badge/python-3.5%20%7C%203.6%20%7C%203.7-blue.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Minyus/optkeras/blob/master/examples/OptKeras_Example.ipynb)\n\nA Python package designed to optimize hyperparameters of Keras Deep Learning models using Optuna. Supported features include pruning, logging, and saving models.\n\n\n### What is Keras?\n\n[Keras](https://keras.io/) is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.\n\n\n### What is Optuna?\n\n[Optuna](https://optuna.org/) is an automatic hyperparameter optimization software framework, particularly designed for machine learning. \n\n\n### What are the advantages of OptKeras?\n\n- Optuna supports pruning option which can terminate the trial (training) early based on the interim objective values (loss, accuracy, etc.). Please see [Optuna's key features](https://optuna.org/#key_features). OptKeras can leverage Optuna's pruning option. If enable_pruning is set to True and the performance in early epochs is not good, OptKeras can terminate training (after the first epoch at the earliest) and try another parameter set.\n- Optuna manages logs in database using [SQLAlchemy](https://www.sqlalchemy.org/) and can resume trials after interruption, even after the machine is rebooted (after 90 minutes of inactivity or 12 hours of runtime of Google Colab) if the database is saved as a storage file. OptKeras can leverage this feature.\n- More epochs do not necessarily improve the performance of Deep Neural Network. OptKeras keeps the best value though epochs so it can be used as the final value.\n- OptKeras can log metrics (loss, accuracy, etc. for train and test datasets) with trial id and timestamp (begin and end) for each epoch to a CSV file.\n- OptKeras can save the best Keras models (only the best Keras model overall or all of the best models for each parameter set) with trial id in its file name so you can link to the log.\n- OptKeras supports randomized grid search (randomized search by sampling parameter sets without replacement; grid search in a randomized order) useful if your primary purpose is benchmarking/comparison rather than optimization. \n\n\n### How to install OptKeras?\n\nOption 1: install from the PyPI\n\n```bash\n\tpip install optkeras\n```\n\nOption 2: install from the GitHub repository\n\n```bash\n\tpip install git+https://github.com/Minyus/optkeras.git\n```\n\nOption 3: clone the [GitHub repository](https://github.com/Minyus/optkeras.git), cd into the downloaded repository, and run:\n\n```bash\n\tpython setup.py install\n```\n\n### How to use OptKeras?\n\nPlease see the [OptKeras example]( \nhttps://colab.research.google.com/github/Minyus/optkeras/blob/master/examples/OptKeras_Example.ipynb\n) available in Google Colab (free cloud GPU) environment.\n\nTo run the code, navigate to \"Runtime\" \u003e\u003e \"Run all\".\n\nTo download the notebook file, navigate to \"File\" \u003e\u003e \"Download .ipynb\".\n\nHere are the basic steps to use.\n\n```python\n\"\"\" Step 0. Import Keras, Optuna, and OptKeras \"\"\"\n\nfrom keras.models import Sequential\nfrom keras.layers import Flatten, Dense, Conv2D\nfrom keras.optimizers import Adam\nimport keras.backend as K\n\nimport optuna\n\nfrom optkeras.optkeras import OptKeras\n\n\nstudy_name = dataset_name + '_Simple'\n\n\"\"\" Step 1. Instantiate OptKeras class\nYou can specify arguments for Optuna's create_study method and other arguments \nfor OptKeras such as enable_pruning. \n\"\"\"\n\nok = OptKeras(study_name=study_name)\n\n\n\"\"\" Step 2. Define objective function for Optuna \"\"\"\n\ndef objective(trial):\n    \n    \"\"\" Step 2.1. Define parameters to try using methods of optuna.trial such as \n    suggest_categorical. In this simple demo, try 2*2*2*2 = 16 parameter sets: \n    2 values specified in list for each of 4 parameters \n    (filters, kernel_size, strides, and activation for convolution).\n    \"\"\"    \n    model = Sequential()\n    model.add(Conv2D(\n        filters = trial.suggest_categorical('filters', [32, 64]), \n        kernel_size = trial.suggest_categorical('kernel_size', [3, 5]), \n        strides = trial.suggest_categorical('strides', [1, 2]), \n        activation = trial.suggest_categorical('activation', ['relu', 'linear']), \n        input_shape = input_shape ))\n    model.add(Flatten())\n    model.add(Dense(num_classes, activation='softmax'))\n    model.compile(optimizer = Adam(), \n                loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n    \n    \"\"\" Step 2.2. Specify callbacks(trial) and keras_verbose in fit \n    (or fit_generator) method of Keras model\n    \"\"\"\n    model.fit(x_train, y_train, \n              validation_data = (x_test, y_test), shuffle = True,\n              batch_size = 512, epochs = 2,\n              callbacks = ok.callbacks(trial), \n              verbose = ok.keras_verbose )  \n    \n    \"\"\" Step 2.3. Return trial_best_value (or latest_value) \"\"\"\n    return ok.trial_best_value\n\n\"\"\" Step 3. Run optimize. \nSet n_trials and/or timeout (in sec) for optimization by Optuna\n\"\"\"\nok.optimize(objective, timeout = 60) # 1 minute for demo\n```\n\n\n### Will OptKeras limit features of Keras or Optuna?\n\nNot at all! You can access the full feaures of Keras and Optuna even if OptKeras is used. \n\n\n### What parameaters are available for OptKeras?\n\n- monitor: The metric to optimize by Optuna.\n'val_loss' in default.\n- enable_pruning: Enable pruning by Optuna.\nFalse in default.\nReference: https://optuna.readthedocs.io/en/latest/tutorial/pruning.html\n- enable_keras_log: Enable logging by Keras CSVLogger callback.\nrue in default.\nReference: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/CSVLogger\n- keras_log_file_suffix: Suffix of the file if enable_keras_log is True.\n'_Keras.csv' in default.\n- enable_optuna_log: Enable generating a log file by Optuna study.trials_dataframe().\nTrue in default.\nReference: https://optuna.readthedocs.io/en/latest/reference/study.html#optuna.study.Study.trials_dataframe\n- optuna_log_file_suffix: Suffix of the file if enable_optuna_log is True.\n'Optuna.csv' in default.\n- models_to_keep: The number of models to keep. Either 1 , 0, or -1 (save all models).\n1 in default.\n- ckpt_period: Period to save model check points.\n1 in default.\nReference: https://keras.io/callbacks/#modelcheckpoint\n- save_weights_only: if True, then only the model's weights will be saved (model.save_weights(filepath)), else the full model is saved (model.save(filepath)).\nFalse in default.\nReference: https://keras.io/callbacks/#modelcheckpoint\n- save_best_only: if save_best_only=True, the latest best model according to the quantity monitored will not be overwritten.\nTrue in default.\nReference: https://keras.io/callbacks/#modelcheckpoint\n- model_file_prefix: Prefix of the model file path if models_to_keep is not 0.\n'model_' in default.\n- model_file_suffix: Suffix of the model file path if models_to_keep is not 0.\n'.h5' in default.\n- directory_path: The path of the directory for the files.\nCurrent working directory in default.\n- verbose: How much info to print onto the screen.\nEither 0 (no messages), 1 , or 2 (troubleshooting)\n1 in default.\n- random_grid_search_mode: Run randomized grid search instead of optimization.\nFalse in default.\n- **kwargs: parameters for optuna.study.create_study():\nstudy_name, storage, sampler=None, pruner=None, direction='minimize'\nReference: https://optuna.readthedocs.io/en/latest/reference/study.html#optuna.study.create_study\n\n\n### Which version of Python is supported?\nPython 3.5 or later\n\n### What was the tested environment for OptKeras?\n\n- Keras 2.2.4\n- TensorFlow 1.14.0\n- Optuna 0.14.0\n- OptKeras 0.0.7\n\n### About author \n\nYusuke Minami\n\n- https://github.com/Minyus\n- https://www.linkedin.com/in/yusukeminami/\n- https://twitter.com/Minyus86\n\n\n### License\n\nMIT License (see https://github.com/Minyus/optkeras/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminyus%2Foptkeras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fminyus%2Foptkeras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminyus%2Foptkeras/lists"}