From f6f7088b5ab28042edc6d2969fc8e30e59306f18 Mon Sep 17 00:00:00 2001 From: c-bata Date: Tue, 31 Mar 2020 16:36:46 +0900 Subject: [PATCH 1/3] Add SuggestFloat and SuggestLogFloat API --- trial.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/trial.go b/trial.go index 12633f4d..4d3e944f 100644 --- a/trial.go +++ b/trial.go @@ -167,7 +167,19 @@ func (t *Trial) Number() (int, error) { } // SuggestUniform suggests a value from a uniform distribution. +// Deprecated: Please use SuggestFloat method. func (t *Trial) SuggestUniform(name string, low, high float64) (float64, error) { + return t.SuggestFloat(name, low, high) +} + +// SuggestLogUniform suggests a value from a uniform distribution in the log domain. +// Deprecated: Please use SuggestLogFloat method. +func (t *Trial) SuggestLogUniform(name string, low, high float64) (float64, error) { + return t.SuggestLogFloat(name, low, high) +} + +// SuggestFloat suggests a value for the floating point parameter. +func (t *Trial) SuggestFloat(name string, low, high float64) (float64, error) { if low > high { return 0, errors.New("'low' must be smaller than or equal to the 'high'") } @@ -176,15 +188,14 @@ func (t *Trial) SuggestUniform(name string, low, high float64) (float64, error) }) } -// SuggestLogUniform suggests a value from a uniform distribution in the log domain. -func (t *Trial) SuggestLogUniform(name string, low, high float64) (float64, error) { +// SuggestLogFloat suggests a value for the log-scale floating point parameter. +func (t *Trial) SuggestLogFloat(name string, low, high float64) (float64, error) { if low > high { return 0, errors.New("'low' must be smaller than or equal to the 'high'") } - v, err := t.suggest(name, LogUniformDistribution{ + return t.suggest(name, UniformDistribution{ High: high, Low: low, }) - return v, err } // SuggestInt suggests an integer parameter. From 760d486633b2c896e8528e4f2de4030c2347ab79 Mon Sep 17 00:00:00 2001 From: c-bata Date: Tue, 31 Mar 2020 16:48:46 +0900 Subject: [PATCH 2/3] Use new suggest API --- README.md | 4 +-- _examples/cmaes/main.go | 4 +-- _examples/concurrency/main.go | 4 +-- _examples/enqueue_trial/main.go | 4 +-- _examples/signalhandling/main.go | 4 +-- _examples/simple_rdb/main.go | 4 +-- _examples/simple_tpe/main.go | 4 +-- _examples/trialnotify/main.go | 4 +-- sampler_test.go | 34 ++++++++++++------------ study_test.go | 12 ++++----- trial.go | 45 ++++++++++++++++++-------------- trial_test.go | 12 ++++----- 12 files changed, 70 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index d75b2183..d2ab1274 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,8 @@ import ( // Define an objective function we want to minimize. func objective(trial goptuna.Trial) (float64, error) { // Define a search space of the input values. - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) // Here is a two-dimensional quadratic function. // F(x1, x2) = (x1 - 2)^2 + (x2 + 5)^2 diff --git a/_examples/cmaes/main.go b/_examples/cmaes/main.go index 0976cd86..1d38da66 100644 --- a/_examples/cmaes/main.go +++ b/_examples/cmaes/main.go @@ -9,8 +9,8 @@ import ( ) func objective(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/_examples/concurrency/main.go b/_examples/concurrency/main.go index b45746fd..84f6ff0b 100644 --- a/_examples/concurrency/main.go +++ b/_examples/concurrency/main.go @@ -11,8 +11,8 @@ import ( ) func objective(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/_examples/enqueue_trial/main.go b/_examples/enqueue_trial/main.go index 06407664..7b811167 100644 --- a/_examples/enqueue_trial/main.go +++ b/_examples/enqueue_trial/main.go @@ -13,8 +13,8 @@ import ( ) func objective(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/_examples/signalhandling/main.go b/_examples/signalhandling/main.go index f0556e0d..8ce9580e 100644 --- a/_examples/signalhandling/main.go +++ b/_examples/signalhandling/main.go @@ -20,8 +20,8 @@ import ( func objective(trial goptuna.Trial) (float64, error) { ctx := trial.GetContext() - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) cmd := exec.CommandContext(ctx, "sleep", "1") err := cmd.Run() diff --git a/_examples/simple_rdb/main.go b/_examples/simple_rdb/main.go index 4a124daf..4053def2 100644 --- a/_examples/simple_rdb/main.go +++ b/_examples/simple_rdb/main.go @@ -14,8 +14,8 @@ import ( ) func objective(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/_examples/simple_tpe/main.go b/_examples/simple_tpe/main.go index 07b2fc36..93a6e513 100644 --- a/_examples/simple_tpe/main.go +++ b/_examples/simple_tpe/main.go @@ -9,8 +9,8 @@ import ( ) func objective(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/_examples/trialnotify/main.go b/_examples/trialnotify/main.go index 3ba8e3f2..3b118a80 100644 --- a/_examples/trialnotify/main.go +++ b/_examples/trialnotify/main.go @@ -11,8 +11,8 @@ import ( ) func objective(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/sampler_test.go b/sampler_test.go index bf651224..610af174 100644 --- a/sampler_test.go +++ b/sampler_test.go @@ -208,22 +208,22 @@ func TestRelativeSampler(t *testing.T) { // First trial cannot trigger relative sampler. err = study.Optimize(func(trial goptuna.Trial) (f float64, e error) { - _, _ = trial.SuggestUniform("uniform", -10, 10) - _, _ = trial.SuggestLogUniform("log_uniform", 1e-10, 1e10) + _, _ = trial.SuggestFloat("uniform", -10, 10) + _, _ = trial.SuggestLogFloat("log_uniform", 1e-10, 1e10) _, _ = trial.SuggestInt("int", -10, 10) - _, _ = trial.SuggestDiscreteUniform("discrete", -10, 10, 0.5) + _, _ = trial.SuggestDiscreteFloat("discrete", -10, 10, 0.5) _, _ = trial.SuggestCategorical("categorical", []string{"choice1", "choice2", "choice3"}) return 0.0, nil }, 1) // Second trial call relative sampler. err = study.Optimize(func(trial goptuna.Trial) (f float64, e error) { - uniformParam, _ := trial.SuggestUniform("uniform", -10, 10) + uniformParam, _ := trial.SuggestFloat("uniform", -10, 10) if uniformParam != 3 { t.Errorf("should be 3, but got %f", uniformParam) } - logUniformParam, _ := trial.SuggestLogUniform("log_uniform", 1e-10, 1e10) + logUniformParam, _ := trial.SuggestLogFloat("log_uniform", 1e-10, 1e10) if logUniformParam != 100 { t.Errorf("should be 100, but got %f", logUniformParam) } @@ -233,7 +233,7 @@ func TestRelativeSampler(t *testing.T) { t.Errorf("should be 7, but got %d", intParam) } - discreteParam, _ := trial.SuggestDiscreteUniform("discrete", -10, 10, 0.5) + discreteParam, _ := trial.SuggestDiscreteFloat("discrete", -10, 10, 0.5) if discreteParam != 5.5 { t.Errorf("should be 5.5, but got %f", discreteParam) } @@ -274,8 +274,8 @@ func TestRelativeSampler_UnsupportedSearchSpace(t *testing.T) { // First trial cannot trigger relative sampler. err = study.Optimize(func(trial goptuna.Trial) (f float64, e error) { - _, _ = trial.SuggestUniform("x1", -10, 10) - _, _ = trial.SuggestLogUniform("x2", 1e-10, 1e10) + _, _ = trial.SuggestFloat("x1", -10, 10) + _, _ = trial.SuggestLogFloat("x2", 1e-10, 1e10) return 0.0, nil }, 1) if err != nil { @@ -285,11 +285,11 @@ func TestRelativeSampler_UnsupportedSearchSpace(t *testing.T) { // Second trial. RelativeSampler return ErrUnsupportedSearchSpace. err = study.Optimize(func(trial goptuna.Trial) (f float64, e error) { - _, e = trial.SuggestUniform("x1", -10, 10) + _, e = trial.SuggestFloat("x1", -10, 10) if e != nil { t.Errorf("err should be nil, but got %s", e) } - _, e = trial.SuggestLogUniform("x2", 1e-10, 1e10) + _, e = trial.SuggestLogFloat("x2", 1e-10, 1e10) if e != nil { t.Errorf("err should be nil, but got %s", e) } @@ -331,7 +331,7 @@ func TestIntersectionSearchSpace(t *testing.T) { if err = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { x, _ := trial.SuggestInt("x", 0, 10) - y, _ := trial.SuggestUniform("y", -3, 3) + y, _ := trial.SuggestFloat("y", -3, 3) return float64(x) + y, nil }, 1); err != nil { panic(err) @@ -361,7 +361,7 @@ func TestIntersectionSearchSpace(t *testing.T) { // First Trial if err = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { x, _ := trial.SuggestInt("x", 0, 10) - y, _ := trial.SuggestUniform("y", -3, 3) + y, _ := trial.SuggestFloat("y", -3, 3) return float64(x) + y, nil }, 1); err != nil { panic(err) @@ -369,7 +369,7 @@ func TestIntersectionSearchSpace(t *testing.T) { // Second Trial if err = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { - y, _ := trial.SuggestUniform("y", -3, 3) + y, _ := trial.SuggestFloat("y", -3, 3) return y, nil }, 1); err != nil { panic(err) @@ -395,7 +395,7 @@ func TestIntersectionSearchSpace(t *testing.T) { // First Trial if err = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { x, _ := trial.SuggestInt("x", 0, 10) - y, _ := trial.SuggestUniform("y", -3, 3) + y, _ := trial.SuggestFloat("y", -3, 3) return float64(x) + y, nil }, 1); err != nil { panic(err) @@ -403,7 +403,7 @@ func TestIntersectionSearchSpace(t *testing.T) { // Second Trial if err = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { - y, _ := trial.SuggestUniform("y", -3, 3) + y, _ := trial.SuggestFloat("y", -3, 3) return y, nil }, 1); err != nil { panic(err) @@ -411,12 +411,12 @@ func TestIntersectionSearchSpace(t *testing.T) { // Failed trial (ignore error) _ = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { - _, _ = trial.SuggestUniform("y", -3, 3) + _, _ = trial.SuggestFloat("y", -3, 3) return 0.0, errors.New("something error") }, 1) // Pruned trial if err = study.Optimize(func(trial goptuna.Trial) (v float64, e error) { - _, _ = trial.SuggestUniform("y", -3, 3) + _, _ = trial.SuggestFloat("y", -3, 3) return 0.0, goptuna.ErrTrialPruned }, 1); err != nil { panic(err) diff --git a/study_test.go b/study_test.go index 756fd673..42bdaee8 100644 --- a/study_test.go +++ b/study_test.go @@ -20,8 +20,8 @@ func ExampleStudy_Optimize() { ) objective := func(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } @@ -76,8 +76,8 @@ func ExampleStudy_EnqueueTrial() { ) objective := func(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } @@ -108,8 +108,8 @@ func TestStudy_EnqueueTrial_WithUnfixedParameter(t *testing.T) { ) objective := func(trial goptuna.Trial) (float64, error) { - x1, _ := trial.SuggestUniform("x1", -10, 10) - x2, _ := trial.SuggestUniform("x2", -10, 10) + x1, _ := trial.SuggestFloat("x1", -10, 10) + x2, _ := trial.SuggestFloat("x2", -10, 10) return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil } diff --git a/trial.go b/trial.go index 4d3e944f..697a9c33 100644 --- a/trial.go +++ b/trial.go @@ -178,6 +178,12 @@ func (t *Trial) SuggestLogUniform(name string, low, high float64) (float64, erro return t.SuggestLogFloat(name, low, high) } +// SuggestDiscreteUniform suggests a value from a discrete uniform distribution. +// Deprecated: Please use SuggestDiscreteFloat method. +func (t *Trial) SuggestDiscreteUniform(name string, low, high, q float64) (float64, error) { + return t.SuggestDiscreteFloat(name, low, high, q) +} + // SuggestFloat suggests a value for the floating point parameter. func (t *Trial) SuggestFloat(name string, low, high float64) (float64, error) { if low > high { @@ -193,21 +199,35 @@ func (t *Trial) SuggestLogFloat(name string, low, high float64) (float64, error) if low > high { return 0, errors.New("'low' must be smaller than or equal to the 'high'") } - return t.suggest(name, UniformDistribution{ + return t.suggest(name, LogUniformDistribution{ High: high, Low: low, }) } +// SuggestDiscreteFloat suggests a value for the discrete floating point parameter. +func (t *Trial) SuggestDiscreteFloat(name string, low, high, q float64) (float64, error) { + if low > high { + return 0, errors.New("'low' must be smaller than or equal to the 'high'") + } + d := DiscreteUniformDistribution{ + High: high, Low: low, Q: q, + } + ir, err := t.suggest(name, d) + if err != nil { + return 0, err + } + return d.ToExternalRepr(ir).(float64), err +} + // SuggestInt suggests an integer parameter. func (t *Trial) SuggestInt(name string, low, high int) (int, error) { if low > high { return 0, errors.New("'low' must be smaller than or equal to the 'high'") } - d := IntUniformDistribution{ + v, err := t.suggest(name, IntUniformDistribution{ High: high, Low: low, - } - v, err := t.suggest(name, d) - return d.ToExternalRepr(v).(int), err + }) + return int(v), err } // SuggestIntWithStep suggests an integer parameter. @@ -225,21 +245,6 @@ func (t *Trial) SuggestIntWithStep(name string, low, high, step int) (int, error return d.ToExternalRepr(v).(int), err } -// SuggestDiscreteUniform suggests a value from a discrete uniform distribution. -func (t *Trial) SuggestDiscreteUniform(name string, low, high, q float64) (float64, error) { - if low > high { - return 0, errors.New("'low' must be smaller than or equal to the 'high'") - } - d := DiscreteUniformDistribution{ - High: high, Low: low, Q: q, - } - ir, err := t.suggest(name, d) - if err != nil { - return 0, err - } - return d.ToExternalRepr(ir).(float64), err -} - // SuggestCategorical suggests an categorical parameter. func (t *Trial) SuggestCategorical(name string, choices []string) (string, error) { if len(choices) == 0 { diff --git a/trial_test.go b/trial_test.go index a526920c..5a1fe285 100644 --- a/trial_test.go +++ b/trial_test.go @@ -17,7 +17,7 @@ func TestTrial_Suggest(t *testing.T) { name: "SuggestUniform", objective: func(trial goptuna.Trial) (float64, error) { // low is larger than high - x1, err := trial.SuggestUniform("x", -10, 10) + x1, err := trial.SuggestFloat("x", -10, 10) if err != nil { return -1, err } @@ -29,7 +29,7 @@ func TestTrial_Suggest(t *testing.T) { name: "SuggestUniform: low is larger than high", objective: func(trial goptuna.Trial) (float64, error) { // low is larger than high - x1, err := trial.SuggestUniform("x", 10, -10) + x1, err := trial.SuggestFloat("x", 10, -10) if err != nil { return -1, err } @@ -40,7 +40,7 @@ func TestTrial_Suggest(t *testing.T) { { name: "SuggestLogUniform", objective: func(trial goptuna.Trial) (float64, error) { - x1, err := trial.SuggestLogUniform("x", 1e5, 1e10) + x1, err := trial.SuggestLogFloat("x", 1e5, 1e10) if err != nil { return -1, err } @@ -51,7 +51,7 @@ func TestTrial_Suggest(t *testing.T) { { name: "SuggestLogUniform: low is larger than high", objective: func(trial goptuna.Trial) (float64, error) { - x1, err := trial.SuggestLogUniform("x", 1e10, 1e5) + x1, err := trial.SuggestLogFloat("x", 1e10, 1e5) if err != nil { return -1, err } @@ -62,7 +62,7 @@ func TestTrial_Suggest(t *testing.T) { { name: "SuggestDiscreteUniform", objective: func(trial goptuna.Trial) (float64, error) { - x1, err := trial.SuggestDiscreteUniform("x", -10, 10, 0.5) + x1, err := trial.SuggestDiscreteFloat("x", -10, 10, 0.5) if err != nil { return -1, err } @@ -73,7 +73,7 @@ func TestTrial_Suggest(t *testing.T) { { name: "SuggestDiscreteUniform: low is larger than high", objective: func(trial goptuna.Trial) (float64, error) { - x1, err := trial.SuggestDiscreteUniform("x", 10, -10, 0.5) + x1, err := trial.SuggestDiscreteFloat("x", 10, -10, 0.5) if err != nil { return -1, err } From b59e4120efa2f24f03b1a19195c42373f887a6d6 Mon Sep 17 00:00:00 2001 From: c-bata Date: Fri, 10 Apr 2020 15:45:07 +0900 Subject: [PATCH 3/3] Add some refactor changes --- trial.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/trial.go b/trial.go index 697a9c33..9bab3253 100644 --- a/trial.go +++ b/trial.go @@ -167,19 +167,19 @@ func (t *Trial) Number() (int, error) { } // SuggestUniform suggests a value from a uniform distribution. -// Deprecated: Please use SuggestFloat method. +// Deprecated: This method will be removed at v1.0.0. Please use SuggestFloat method. func (t *Trial) SuggestUniform(name string, low, high float64) (float64, error) { return t.SuggestFloat(name, low, high) } // SuggestLogUniform suggests a value from a uniform distribution in the log domain. -// Deprecated: Please use SuggestLogFloat method. +// Deprecated: This method will be removed at v1.0.0. Please use SuggestLogFloat method. func (t *Trial) SuggestLogUniform(name string, low, high float64) (float64, error) { return t.SuggestLogFloat(name, low, high) } // SuggestDiscreteUniform suggests a value from a discrete uniform distribution. -// Deprecated: Please use SuggestDiscreteFloat method. +// Deprecated: This method will be removed at v1.0.0. Please use SuggestDiscreteFloat method. func (t *Trial) SuggestDiscreteUniform(name string, low, high, q float64) (float64, error) { return t.SuggestDiscreteFloat(name, low, high, q) } @@ -224,14 +224,15 @@ func (t *Trial) SuggestInt(name string, low, high int) (int, error) { if low > high { return 0, errors.New("'low' must be smaller than or equal to the 'high'") } - v, err := t.suggest(name, IntUniformDistribution{ + d := IntUniformDistribution{ High: high, Low: low, - }) - return int(v), err + } + v, err := t.suggest(name, d) + return d.ToExternalRepr(v).(int), err } -// SuggestIntWithStep suggests an integer parameter. -func (t *Trial) SuggestIntWithStep(name string, low, high, step int) (int, error) { +// SuggestStepInt suggests a step-interval integer parameter. +func (t *Trial) SuggestStepInt(name string, low, high, step int) (int, error) { if low > high { return 0, errors.New("'low' must be smaller than or equal to the 'high'") }