red-optuna 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +7 -0
- data/README.md +58 -0
- data/Rakefile +21 -0
- data/example/rumale-simple.rb +33 -0
- data/lib/optuna.rb +30 -0
- data/lib/optuna/version.rb +3 -0
- data/red-optuna.gemspec +40 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5a84b67ae1cb553ca5e99114729b3ba6128a467ead4e1dbc3a23305647eb8c50
|
4
|
+
data.tar.gz: ae4ed367ce6762ed6b59000f9b6b3b9f475583cbd956ee0af5bc237ff9af77bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19d5ddba5c778708215e7a905f86c50e4c3fbee3c1c5518c1352a7471c50655398554ea0e6b0faadf2b7fe998cfe3835d176ce43b078bb6adbb3d29d70c3d097
|
7
|
+
data.tar.gz: 8ca53b6f37b3f7aeaa081b1306da55e0c10fe8dbf0d3f17d030c9dce0834c41f89afc4bc02f0b4f31910757333d6f17d78df257dfc9fee0b9d64efbedf78d911
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2019 Kouhei Sutou <kou@clear-code.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Red Optuna
|
2
|
+
|
3
|
+
Red Optuna is Ruby bindings for [Optuna](https://optuna.org/), a
|
4
|
+
hyperparameter optimization framework.
|
5
|
+
|
6
|
+
## Description
|
7
|
+
|
8
|
+
Red Optuna is a hyperparameter optimization framework. You can
|
9
|
+
optimize hyperparameter automatically.
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
```console
|
14
|
+
% gem install red-optuna
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Here is an example to optimize hyperparameter for Iris dataset
|
20
|
+
classifier by Rumale.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require "datasets-numo-narray"
|
24
|
+
require "optuna"
|
25
|
+
require "rumale"
|
26
|
+
|
27
|
+
iris = Datasets::Iris.new.to_table
|
28
|
+
x = iris.to_narray(:sepal_length,
|
29
|
+
:sepal_width,
|
30
|
+
:petal_length,
|
31
|
+
:petal_width)
|
32
|
+
y = Numo::NArray[*iris.label_encode(:label)]
|
33
|
+
|
34
|
+
study = Optuna::Study.new
|
35
|
+
study.optimize(n_trials: 100) do |trial|
|
36
|
+
classifier_name = trial.suggest_categorical("classifier",
|
37
|
+
["SVC", "RandomForest"])
|
38
|
+
if classifier_name == "SVC"
|
39
|
+
svc_regulation = trial.suggest_uniform("svc_regulation", 0.0, 1.0)
|
40
|
+
classifier = Rumale::LinearModel::SVC.new(reg_param: svc_regulation.to_f)
|
41
|
+
else
|
42
|
+
rf_max_depth = trial.suggest_loguniform("rf_max_depth", 2, 32).to_i
|
43
|
+
classifier = Rumale::Ensemble::RandomForestClassifier.new(max_depth: rf_max_depth)
|
44
|
+
end
|
45
|
+
|
46
|
+
splitter = Rumale::ModelSelection::StratifiedKFold.new
|
47
|
+
cv = Rumale::ModelSelection::CrossValidation.new(estimator: classifier,
|
48
|
+
splitter: splitter)
|
49
|
+
report = cv.perform(x, y)
|
50
|
+
accuracy = report[:test_score].sum / splitter.n_splits
|
51
|
+
1.0 - accuracy
|
52
|
+
end
|
53
|
+
p study.best_trial
|
54
|
+
```
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The MIT license. See `LICENSE.txt` for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "bundler/gem_helper"
|
5
|
+
|
6
|
+
base_dir = File.join(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
helper = Bundler::GemHelper.new(base_dir)
|
9
|
+
def helper.version_tag
|
10
|
+
version
|
11
|
+
end
|
12
|
+
|
13
|
+
helper.install
|
14
|
+
spec = helper.gemspec
|
15
|
+
|
16
|
+
desc "Run tests"
|
17
|
+
task :test do
|
18
|
+
ruby("test/run-test.rb")
|
19
|
+
end
|
20
|
+
|
21
|
+
task default: :test
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "datasets-numo-narray"
|
4
|
+
require "optuna"
|
5
|
+
require "rumale"
|
6
|
+
|
7
|
+
iris = Datasets::Iris.new.to_table
|
8
|
+
x = iris.to_narray(:sepal_length,
|
9
|
+
:sepal_width,
|
10
|
+
:petal_length,
|
11
|
+
:petal_width)
|
12
|
+
y = Numo::NArray[*iris.label_encode(:label)]
|
13
|
+
|
14
|
+
study = Optuna::Study.new
|
15
|
+
study.optimize(n_trials: 100) do |trial|
|
16
|
+
classifier_name = trial.suggest_categorical("classifier",
|
17
|
+
["SVC", "RandomForest"])
|
18
|
+
if classifier_name == "SVC"
|
19
|
+
svc_regulation = trial.suggest_uniform("svc_regulation", 0.0, 1.0)
|
20
|
+
classifier = Rumale::LinearModel::SVC.new(reg_param: svc_regulation.to_f)
|
21
|
+
else
|
22
|
+
rf_max_depth = trial.suggest_loguniform("rf_max_depth", 2, 32).to_i
|
23
|
+
classifier = Rumale::Ensemble::RandomForestClassifier.new(max_depth: rf_max_depth)
|
24
|
+
end
|
25
|
+
|
26
|
+
splitter = Rumale::ModelSelection::StratifiedKFold.new
|
27
|
+
cv = Rumale::ModelSelection::CrossValidation.new(estimator: classifier,
|
28
|
+
splitter: splitter)
|
29
|
+
report = cv.perform(x, y)
|
30
|
+
accuracy = report[:test_score].sum / splitter.n_splits
|
31
|
+
1.0 - accuracy
|
32
|
+
end
|
33
|
+
p study.best_trial
|
data/lib/optuna.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "pycall"
|
2
|
+
|
3
|
+
module Optuna
|
4
|
+
class << self
|
5
|
+
def __pyptr__
|
6
|
+
@optuna ||= PyCall.import_module("optuna")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Study = __pyptr__.Study
|
11
|
+
class Study
|
12
|
+
register_python_type_mapping
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def new(*args)
|
16
|
+
Optuna.__pyptr__.create_study(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def optimize(*args, &block)
|
21
|
+
args = [block, *args] if block_given?
|
22
|
+
super(*args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Trial = __pyptr__.Trial
|
27
|
+
class Trial
|
28
|
+
register_python_type_mapping
|
29
|
+
end
|
30
|
+
end
|
data/red-optuna.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
clean_white_space = lambda do |entry|
|
4
|
+
entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
|
5
|
+
end
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
|
8
|
+
require "optuna/version"
|
9
|
+
|
10
|
+
Gem::Specification.new do |spec|
|
11
|
+
spec.name = "red-optuna"
|
12
|
+
spec.version = Optuna::VERSION
|
13
|
+
spec.homepage = "https://github.com/red-data-tools/red-optuna"
|
14
|
+
spec.authors = ["Kouhei Sutou"]
|
15
|
+
spec.email = ["kou@clear-code.com"]
|
16
|
+
|
17
|
+
readme = File.read("README.md")
|
18
|
+
readme.force_encoding("UTF-8")
|
19
|
+
spec.summary = clean_white_space.call(readme.split(/^\#+\s(.*)$/)[2])
|
20
|
+
entries = readme.split(/^\#\#\s(.*)$/)
|
21
|
+
description = clean_white_space.call(entries[entries.index("Description") + 1])
|
22
|
+
spec.description, = description.split(/\n\n+/, 3)
|
23
|
+
spec.license = "MIT"
|
24
|
+
spec.files = [
|
25
|
+
"README.md",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"Rakefile",
|
28
|
+
"Gemfile",
|
29
|
+
"#{spec.name}.gemspec",
|
30
|
+
]
|
31
|
+
spec.files += Dir.glob("lib/**/*.rb")
|
32
|
+
spec.files += Dir.glob("example/**/*.rb")
|
33
|
+
|
34
|
+
spec.add_runtime_dependency("pycall")
|
35
|
+
|
36
|
+
spec.add_development_dependency("bundler")
|
37
|
+
spec.add_development_dependency("rake")
|
38
|
+
spec.add_development_dependency("red-datasets-numo-narray")
|
39
|
+
spec.add_development_dependency("rumale")
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: red-optuna
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kouhei Sutou
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pycall
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: red-datasets-numo-narray
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rumale
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |
|
84
|
+
Red Optuna is a hyperparameter optimization framework. You can
|
85
|
+
optimize hyperparameter automatically.
|
86
|
+
email:
|
87
|
+
- kou@clear-code.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- example/rumale-simple.rb
|
97
|
+
- lib/optuna.rb
|
98
|
+
- lib/optuna/version.rb
|
99
|
+
- red-optuna.gemspec
|
100
|
+
homepage: https://github.com/red-data-tools/red-optuna
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.7.6.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Red Optuna is Ruby bindings for [Optuna](https://optuna.org/), a hyperparameter
|
124
|
+
optimization framework.
|
125
|
+
test_files: []
|