Project

red-optuna

0.0
No commit activity in last 3 years
No release in over 3 years
Red Optuna is a hyperparameter optimization framework. You can optimize hyperparameter automatically.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0.2.0
 Project Readme

Red Optuna

Red Optuna is Ruby bindings for Optuna, a hyperparameter optimization framework.

Description

Red Optuna is a hyperparameter optimization framework. You can optimize hyperparameter automatically.

Install

% gem install red-optuna

Usage

Here is an example to optimize hyperparameter for Iris dataset classifier by Rumale.

require "datasets-numo-narray"
require "optuna"
require "rumale"

iris = Datasets::Iris.new.to_table
x = iris.to_narray(:sepal_length,
                   :sepal_width,
                   :petal_length,
                   :petal_width)
y = Numo::NArray[*iris.label_encode(:label)]

study = Optuna::Study.new
study.optimize(n_trials: 100) do |trial|
  classifier_name = trial.suggest_categorical("classifier",
                                              ["SVC", "RandomForest"])
  if classifier_name == "SVC"
    svc_regulation = trial.suggest_uniform("svc_regulation", 0.0, 1.0)
    classifier = Rumale::LinearModel::SVC.new(reg_param: svc_regulation.to_f)
  else
    rf_max_depth = trial.suggest_loguniform("rf_max_depth", 2, 32).to_i
    classifier = Rumale::Ensemble::RandomForestClassifier.new(max_depth: rf_max_depth)
  end

  splitter = Rumale::ModelSelection::StratifiedKFold.new
  cv = Rumale::ModelSelection::CrossValidation.new(estimator: classifier,
                                                   splitter: splitter)
  report = cv.perform(x, y)
  accuracy = report[:test_score].sum / splitter.n_splits
  1.0 - accuracy
end
p study.best_trial

License

The MIT license. See LICENSE.txt for details.