Code Monkey home page Code Monkey logo

optkeras's Introduction

OptKeras, a wrapper around Keras and Optuna

PyPI version Python Version License: MIT Open In Colab

A Python package designed to optimize hyperparameters of Keras Deep Learning models using Optuna. Supported features include pruning, logging, and saving models.

What is Keras?

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.

What is Optuna?

Optuna is an automatic hyperparameter optimization software framework, particularly designed for machine learning.

What are the advantages of OptKeras?

  • 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. 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.
  • Optuna manages logs in database using SQLAlchemy 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.
  • 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.
  • 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.
  • 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.
  • 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.

How to install OptKeras?

Option 1: install from the PyPI

	pip install optkeras

Option 2: install from the GitHub repository

	pip install git+https://github.com/Minyus/optkeras.git

Option 3: clone the GitHub repository, cd into the downloaded repository, and run:

	python setup.py install

How to use OptKeras?

Please see the OptKeras example available in Google Colab (free cloud GPU) environment.

To run the code, navigate to "Runtime" >> "Run all".

To download the notebook file, navigate to "File" >> "Download .ipynb".

Here are the basic steps to use.

""" Step 0. Import Keras, Optuna, and OptKeras """

from keras.models import Sequential
from keras.layers import Flatten, Dense, Conv2D
from keras.optimizers import Adam
import keras.backend as K

import optuna

from optkeras.optkeras import OptKeras


study_name = dataset_name + '_Simple'

""" Step 1. Instantiate OptKeras class
You can specify arguments for Optuna's create_study method and other arguments 
for OptKeras such as enable_pruning. 
"""

ok = OptKeras(study_name=study_name)


""" Step 2. Define objective function for Optuna """

def objective(trial):
    
    """ Step 2.1. Define parameters to try using methods of optuna.trial such as 
    suggest_categorical. In this simple demo, try 2*2*2*2 = 16 parameter sets: 
    2 values specified in list for each of 4 parameters 
    (filters, kernel_size, strides, and activation for convolution).
    """    
    model = Sequential()
    model.add(Conv2D(
        filters = trial.suggest_categorical('filters', [32, 64]), 
        kernel_size = trial.suggest_categorical('kernel_size', [3, 5]), 
        strides = trial.suggest_categorical('strides', [1, 2]), 
        activation = trial.suggest_categorical('activation', ['relu', 'linear']), 
        input_shape = input_shape ))
    model.add(Flatten())
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(optimizer = Adam(), 
                loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    
    """ Step 2.2. Specify callbacks(trial) and keras_verbose in fit 
    (or fit_generator) method of Keras model
    """
    model.fit(x_train, y_train, 
              validation_data = (x_test, y_test), shuffle = True,
              batch_size = 512, epochs = 2,
              callbacks = ok.callbacks(trial), 
              verbose = ok.keras_verbose )  
    
    """ Step 2.3. Return trial_best_value (or latest_value) """
    return ok.trial_best_value

""" Step 3. Run optimize. 
Set n_trials and/or timeout (in sec) for optimization by Optuna
"""
ok.optimize(objective, timeout = 60) # 1 minute for demo

Will OptKeras limit features of Keras or Optuna?

Not at all! You can access the full feaures of Keras and Optuna even if OptKeras is used.

What parameaters are available for OptKeras?

Which version of Python is supported?

Python 3.5 or later

What was the tested environment for OptKeras?

  • Keras 2.2.4
  • TensorFlow 1.14.0
  • Optuna 0.14.0
  • OptKeras 0.0.7

About author

Yusuke Minami

License

MIT License (see https://github.com/Minyus/optkeras/blob/master/LICENSE).

optkeras's People

Contributors

minyus avatar mludvik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

optkeras's Issues

colab notebook default example error

I run the link of your colab notebook as is and I receive the following AssertionError at every run

[W 2020-07-02 16:28:05,165] Setting status of trial#0 as TrialState.FAIL because of the following error: AssertionError('[OptKeras] Monitor variable needs to be in the logs dictionary. Use a callback.',)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/optuna/study.py", line 469, in _run_trial
    result = func(trial)
  File "/usr/local/lib/python3.6/dist-packages/optkeras/optkeras.py", line 130, in fun_tf
    return fun(trial)
  File "<ipython-input-6-acfc70c5c697>", line 41, in objective
    verbose=ok.keras_verbose )
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1239, in fit
    validation_freq=validation_freq)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py", line 216, in fit_loop
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "/usr/local/lib/python3.6/dist-packages/keras/callbacks/callbacks.py", line 152, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "/usr/local/lib/python3.6/dist-packages/optkeras/optkeras.py", line 300, in on_epoch_end
    assert self.monitor in logs, '[OptKeras] Monitor variable needs to be in the logs dictionary. Use a callback.'
AssertionError: [OptKeras] Monitor variable needs to be in the logs dictionary. Use a callback.

it simply doesn't optimize anything

"trial_best_logs" reset every epoch

resetting the trial best logs should occur "on_train_begin" instead of "on_epoch_begin". The result now in the case of "mode_max" =True, logs reset every epoch during a single trial

ModuleNotFoundError: No module named 'optuna.structs'

When I try to import Optkeras with from optkeras.optkeras import OptKeras

I get the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In [8], line 27
     25 import logging
     26 import optuna
---> 27 from optkeras.optkeras import OptKeras
     28 from sklearn.preprocessing import MinMaxScaler
     29 scaler = MinMaxScaler()

File ~/.conda/envs/deeplearning/lib/python3.10/site-packages/optkeras/optkeras.py:392
    390 from optuna.pruners import BasePruner
    391 from optuna.storages import BaseStorage  # NOQA
--> 392 from optuna.structs import TrialState
    395 class RepeatPruner(BasePruner):
    396     """ Prune if the same parameter set was found in Optuna database
    397         Coded based on source code of MedianPruner class at
    398         https://github.com/pfnet/optuna/blob/master/optuna/pruners/median.py
    399     """

ModuleNotFoundError: No module named 'optuna.structs'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.