-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
hyperparam.go
105 lines (90 loc) · 2.67 KB
/
hyperparam.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package optimizer
import (
"fmt"
"github.com/c-bata/goptuna"
jsonpatch "github.com/evanphx/json-patch/v5"
)
type paramDomain interface {
buildPatch(trail *goptuna.Trial) (jsonpatch.Patch, error)
}
type paramDomainBase struct {
label string
path string
}
type intRangeDomain struct {
paramDomainBase
min int
max int
}
func (d *intRangeDomain) buildPatch(trial *goptuna.Trial) (jsonpatch.Patch, error) {
val, err := trial.SuggestInt(d.label, d.min, d.max)
if err != nil {
return nil, err
}
jsonOp := []byte(reformatJson(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": %v }]`, d.path, val)))
return jsonpatch.DecodePatch(jsonOp)
}
type intStepRangeDomain struct {
paramDomainBase
min int
max int
step int
}
func (d *intStepRangeDomain) buildPatch(trial *goptuna.Trial) (jsonpatch.Patch, error) {
val, err := trial.SuggestStepInt(d.label, d.min, d.max, d.step)
if err != nil {
return nil, err
}
jsonOp := []byte(reformatJson(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": %v }]`, d.path, val)))
return jsonpatch.DecodePatch(jsonOp)
}
type floatRangeDomain struct {
paramDomainBase
min float64
max float64
}
func (d *floatRangeDomain) buildPatch(trial *goptuna.Trial) (jsonpatch.Patch, error) {
val, err := trial.SuggestFloat(d.label, d.min, d.max)
if err != nil {
return nil, err
}
jsonOp := []byte(reformatJson(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": %v }]`, d.path, val)))
return jsonpatch.DecodePatch(jsonOp)
}
type floatDiscreteRangeDomain struct {
paramDomainBase
min float64
max float64
step float64
}
func (d *floatDiscreteRangeDomain) buildPatch(trial *goptuna.Trial) (jsonpatch.Patch, error) {
val, err := trial.SuggestDiscreteFloat(d.label, d.min, d.max, d.step)
if err != nil {
return nil, err
}
jsonOp := []byte(reformatJson(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": %v }]`, d.path, val)))
return jsonpatch.DecodePatch(jsonOp)
}
type stringDomain struct {
paramDomainBase
options []string
}
func (d *stringDomain) buildPatch(trial *goptuna.Trial) (jsonpatch.Patch, error) {
val, err := trial.SuggestCategorical(d.label, d.options)
if err != nil {
return nil, err
}
jsonOp := []byte(reformatJson(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": "%v" }]`, d.path, val)))
return jsonpatch.DecodePatch(jsonOp)
}
type boolDomain struct {
paramDomainBase
}
func (d *boolDomain) buildPatch(trial *goptuna.Trial) (jsonpatch.Patch, error) {
valStr, err := trial.SuggestCategorical(d.label, []string{"false", "true"})
if err != nil {
return nil, err
}
jsonOp := []byte(reformatJson(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": %s }]`, d.path, valStr)))
return jsonpatch.DecodePatch(jsonOp)
}