-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
179 lines (164 loc) · 3.87 KB
/
options.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
package boo
import (
"fmt"
"github.com/rmera/boo/utils"
)
// Contain options to create a multi-class classification ensamble.
type Options struct {
XGB bool
Rounds int
MaxDepth int
EarlyStop int //roundw without increased fit before we stop trying.
LearningRate float64
Lambda float64
MinChildWeight float64
Gamma float64
SubSample float64
ColSubSample float64
BaseScore float64
MinSample int //the minimum samples in each tree
TreeMethod string
// EarlyStopRounds int //stop after n consecutive rounds of no improvement. Not implemented yet.
Verbose bool
Loss utils.LossFunc
}
// Returns a pointer to an Options structure with the default values
// for an XGBoost multi-class classification ensamble.
func DefaultXOptions() *Options {
O := new(Options)
O.XGB = true
O.Rounds = 20
O.SubSample = 0.8
O.ColSubSample = 0.8
O.Lambda = 1.5
O.MinChildWeight = 3
O.Gamma = 0.2
O.MaxDepth = 5
O.LearningRate = 0.3
O.BaseScore = 0.5
O.TreeMethod = "exact"
O.EarlyStop = 10
O.Loss = &utils.SQErrLoss{}
O.Verbose = false //just for clarity
O.MinSample = 5
return O
}
func (o *Options) Equal(O *Options) bool {
if O.XGB != o.XGB {
return false
}
if O.Rounds != o.Rounds {
return false
}
if O.SubSample != o.SubSample {
return false
}
if O.ColSubSample != o.ColSubSample {
return false
}
if O.Lambda != o.Lambda {
return false
}
if O.MinChildWeight != o.MinChildWeight {
return false
}
if O.Gamma != o.Gamma {
return false
}
if O.MaxDepth != o.MaxDepth {
return false
}
if O.LearningRate != o.LearningRate {
return false
}
if O.BaseScore != o.BaseScore {
return false
}
if O.TreeMethod != "exact" {
return false
}
if O.Loss != o.Loss {
return false
}
if O.Verbose != o.Verbose {
return false
}
if O.MinSample != o.MinSample {
return false
}
return true
}
func (o *Options) Clone() *Options {
O := new(Options)
O.XGB = o.XGB
O.Rounds = o.Rounds
O.SubSample = o.SubSample
O.ColSubSample = o.ColSubSample
O.Lambda = o.Lambda
O.MinChildWeight = o.MinChildWeight
O.Gamma = o.Gamma
O.MaxDepth = o.MaxDepth
O.LearningRate = o.LearningRate
O.BaseScore = o.BaseScore
O.TreeMethod = "exact"
O.Loss = o.Loss
O.Verbose = o.Verbose
O.MinSample = o.MinSample
return O
}
// Returns a pointer to an Options structure with the default
// options a for regular gradient boosting multi-class classification
// ensamble.
func DefaultGOptions() *Options {
O := new(Options)
O.XGB = false
O.Rounds = 10
O.MaxDepth = 4
O.LearningRate = 0.1
O.MinChildWeight = 3
O.Loss = &utils.MSELoss{}
return O
}
func DefaultOptions() *Options {
return DefaultXOptions()
}
// Returns a string representation of the options
func (O *Options) String() string {
if O.XGB {
return fmt.Sprintf("xgboost %d r/%d md/%.3f lr/%.3f ss/%.3f bs/%.3f gam/%.3f lam/%.3f mcw/%.3f css", O.Rounds, O.MaxDepth, O.LearningRate, O.SubSample, O.BaseScore, O.Gamma, O.Lambda, O.MinChildWeight, O.ColSubSample)
} else {
return fmt.Sprintf("gboost %d r/%d md/%.3f lr/%.3f mcw", O.Rounds, O.MaxDepth, O.LearningRate, O.MinChildWeight)
}
}
func (o *Options) Check() error {
n := fmt.Errorf
if o.Rounds <= 0 {
return n("Round! %v", o.Rounds)
}
if o.LearningRate <= 0 {
return n("LearningRate:", o.LearningRate)
}
if o.SubSample <= 0 || o.ColSubSample <= 0 {
return n("SubsSample or ColSubSample %v %v", o.SubSample, o.ColSubSample)
}
ssmax := 1.0
if o.SubSample > ssmax || o.ColSubSample > ssmax {
return n("SubSample or ColSubSample %v %v", o.SubSample, o.ColSubSample)
}
if o.Lambda < 0 {
return n("Lambda %v", o.Lambda)
}
if o.MinChildWeight < 1 {
return n("MinChildWeight %d", o.MinChildWeight)
}
if o.Gamma < 0 {
return n("Gamma %v", o.Gamma)
}
if o.MaxDepth < 2 {
return n("MaxDepth %v", o.MaxDepth)
}
if o.MinSample < 1 {
return n("MinSample %v", o.MinSample)
}
return nil
}