-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsampler.go
190 lines (169 loc) · 5.38 KB
/
sampler.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package goptuna
import (
"errors"
"math"
"math/rand"
"sync"
)
var (
// ErrUnsupportedSearchSpace represents sampler does not support a given search space.
ErrUnsupportedSearchSpace = errors.New("unsupported search space")
)
// Sampler is the interface for sampling algorithms that do not use
// relationship between parameters such as random sampling and TPE.
//
// Note that if study object has RelativeSampler, this interface is used
// only for parameters that are not sampled by RelativeSampler.
type Sampler interface {
// Sample a single parameter for a given distribution.
Sample(*Study, FrozenTrial, string, interface{}) (float64, error)
}
// RelativeSampler is the interface for sampling algorithms that use
// relationship between parameters such as Gaussian Process and CMA-ES.
//
// This interface is called once at the beginning of each trial,
// i.e., right before the evaluation of the objective function.
type RelativeSampler interface {
// SampleRelative samples multiple dimensional parameters in a given search space.
SampleRelative(*Study, FrozenTrial, map[string]interface{}) (map[string]float64, error)
}
// IntersectionSearchSpace return return the intersection search space of the Study.
//
// Intersection search space contains the intersection of parameter distributions that have been
// suggested in the completed trials of the study so far.
// If there are multiple parameters that have the same name but different distributions,
// neither is included in the resulting search space
// (i.e., the parameters with dynamic value ranges are excluded).
func IntersectionSearchSpace(study *Study) (map[string]interface{}, error) {
var searchSpace map[string]interface{}
trials, err := study.GetTrials()
if err == ErrTrialsPartiallyDeleted {
study.logger.Warn("Some trials are not used to calculate intersection of search spaces." +
" Please use `goptuna.StudyOptionDefineSearchSpace` option.")
err = nil
} else if err != nil {
return nil, err
}
for i := range trials {
if trials[i].State != TrialStateComplete {
continue
}
if searchSpace == nil {
searchSpace = trials[i].Distributions
continue
}
exists := func(name string) bool {
for name2 := range trials[i].Distributions {
if name == name2 {
return true
}
}
return false
}
deleteParams := make([]string, 0, len(searchSpace))
for name := range searchSpace {
if !exists(name) {
deleteParams = append(deleteParams, name)
} else if trials[i].Distributions[name] != searchSpace[name] {
deleteParams = append(deleteParams, name)
}
}
for j := range deleteParams {
delete(searchSpace, deleteParams[j])
}
}
return searchSpace, nil
}
var _ Sampler = &RandomSampler{}
// RandomSampler for random search
type RandomSampler struct {
rng *rand.Rand
mu sync.Mutex
}
// RandomSamplerOption is a type of function to set options.
type RandomSamplerOption func(sampler *RandomSampler)
// RandomSamplerOptionSeed sets seed number.
func RandomSamplerOptionSeed(seed int64) RandomSamplerOption {
return func(sampler *RandomSampler) {
sampler.rng = rand.New(rand.NewSource(seed))
}
}
// NewRandomSampler implements random search algorithm.
func NewRandomSampler(opts ...RandomSamplerOption) *RandomSampler {
s := &RandomSampler{
rng: rand.New(rand.NewSource(0)),
}
for _, opt := range opts {
opt(s)
}
return s
}
// Sample a parameter for a given distribution.
func (s *RandomSampler) Sample(
study *Study,
trial FrozenTrial,
paramName string,
paramDistribution interface{},
) (float64, error) {
s.mu.Lock()
defer s.mu.Unlock()
switch d := paramDistribution.(type) {
case UniformDistribution:
if d.Single() {
return d.Low, nil
}
return s.rng.Float64()*(d.High-d.Low) + d.Low, nil
case LogUniformDistribution:
if d.Single() {
return d.Low, nil
}
logLow := math.Log(d.Low)
logHigh := math.Log(d.High)
return math.Exp(s.rng.Float64()*(logHigh-logLow) + logLow), nil
case IntUniformDistribution:
if d.Single() {
return float64(d.Low), nil
}
return float64(s.rng.Intn(d.High-d.Low) + d.Low), nil
case StepIntUniformDistribution:
if d.Single() {
return float64(d.Low), nil
}
r := (d.High - d.Low) / d.Step
v := (s.rng.Intn(r) * d.Step) + d.Low
return float64(v), nil
case DiscreteUniformDistribution:
if d.Single() {
return d.Low, nil
}
q := d.Q
r := d.High - d.Low
// [low, high] is shifted to [0, r] to align sampled values at regular intervals.
low := 0 - 0.5*q
high := r + 0.5*q
x := s.rng.Float64()*(high-low) + low
v := math.Round(x/q)*q + d.Low
return math.Min(math.Max(v, d.Low), d.High), nil
case CategoricalDistribution:
if d.Single() {
return float64(0), nil
}
return float64(rand.Intn(len(d.Choices))), nil
default:
return 0.0, errors.New("undefined distribution")
}
}
// RandomSearchSampler for random search
// Deprecated: this is renamed to RandomSampler.
type RandomSearchSampler RandomSampler
// RandomSearchSamplerOption is a type of function to set change the option.
// Deprecated: this is renamed to RandomSamplerOption.
type RandomSearchSamplerOption RandomSamplerOption
var (
// RandomSearchSamplerOptionSeed sets seed number.
// Deprecated: this is renamed to RandomSamplerOptionSeed.
RandomSearchSamplerOptionSeed = RandomSamplerOptionSeed
// NewRandomSearchSampler implements random search algorithm.
// Deprecated: this is renamed to NewRandomSampler.
NewRandomSearchSampler = NewRandomSampler
)