-
Notifications
You must be signed in to change notification settings - Fork 0
/
l2tune
executable file
·220 lines (200 loc) · 5.22 KB
/
l2tune
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env coffee
fs = require 'fs'
coeffs_to_weights = require('./coeffs_to_weights.coffee')
match = require './match.coffee'
{ N_PHASES } = require './pattern'
MATCH_MIN = 3
defaults = require('./learn.coffee').defaults
opt = require 'yargs'
.options
k:
desc: 'Number of cross validation splits'
default: defaults.k
m:
alias: 'min'
desc: 'Minimum L2 to search'
default: 0.01
M:
alias: 'max'
desc: 'Maximum L2 to search'
default: 2
p:
alias: 'precision'
desc: 'Maximum step (ratio) to search'
default: 1.1
l:
alias: 'logistic'
desc: 'Logistic regression'
type: 'boolean'
default: false
b:
alias: 'book'
desc: 'Database file'
type: 'string'
default: defaults.book
requiresArg: true
e:
alias: 'epochs'
desc: 'Number of epochs'
type: 'number'
default: defaults.epochs
requiresArg: true
batch_size:
desc: 'Number of samples per batch (default auto)'
type: 'number'
default: defaults.batch_size
requiresArg: true
o:
alias: 'outfile'
desc: 'Output L2 param file (default l2.json or l2_logistic.json)'
type: 'string'
default: null
requiresArg: true
match:
desc: 'Use match to fine tune'
type: 'boolean'
default: false
match_range:
desc: 'Switch CV to match in this range'
type: 'number'
default: 1.4
requiresArg: true
s:
alias: 'search'
desc: 'Number of searches used in match'
type: 'number'
default: 300000
requiresArg: true
w:
alias: 'wld'
desc: 'WLD search depth used in match'
type: 'number'
default: 24
requiresArg: true
f:
alias: 'full'
desc: 'Full search depth used in match'
type: 'number'
default: 22
requiresArg: true
weights:
desc: 'Output weights.json file (only when --match)'
type: 'string'
default: 'weights.json'
requiresArg: true
h:
alias: 'help'
.version false
.strict()
.argv
opt.verbose = false
{ load_samples, cross_validation } = require('./learn.coffee')(opt)
{ learn } = require('./learn.coffee')({opt..., verbose: false})
try_cv = (l2, min) ->
process.stdout.write "L2=#{l2}: "
loss = cross_validation({opt..., l2})
process.stdout.write "loss=#{loss}"
if loss < min
process.stdout.write " *\n"
else
process.stdout.write '\n'
loss
tune_cv = (phase, precision) ->
console.log "Phase #{phase}"
load_samples phase
l2 = (opt.min * opt.max) ** .5
step = (opt.max / opt.min) ** .25
min_loss = try_cv(l2, Infinity)
best = l2
loop
l2 = best * step
loss = try_cv(l2, min_loss)
if loss < min_loss
min_loss = loss
best = l2
else
l2 = best / step
loss = try_cv(l2, min_loss)
if loss < min_loss
min_loss = loss
best = l2
break if step <= precision
step **= .5
best
l2_array =
for phase in [0...N_PHASES]
precision =
if opt.match and phase >= MATCH_MIN
opt.match_range ** .5
else
opt.precision
tune_cv(phase, precision)
weights_array = []
prepare_weights = ->
for phase in [0...N_PHASES]
continue if weights_array[phase]?
console.log "Learning phase #{phase} L2=#{l2_array[phase]}"
load_samples phase
weights_array[phase] = learn phase, l2: l2_array[phase]
try_match = (phase, l2, max, skip_learn=false) ->
process.stdout.write "L2=#{l2}: "
weights_array[phase] = learn phase, l2: l2 unless skip_learn
weights = weights_array[phase]
coeffs_to_weights outfile: opt.weights, array: weights_array, verbose: false
{ winrate, avg } = match
weights: opt.weights
search: opt.search
wld: opt.wld
full: opt.full
quiet: true
openings: match.defaults.openings
min: max
score = winrate + avg * 0.0001
process.stdout.write "score=#{Math.round(score*1000000)/1000000}"
console.log if score > max then ' *' else ''
{ score, weights }
tune_match = (phase, score) ->
load_samples phase
best = l2_array[phase]
if score
console.log "L2=#{best}: score=#{Math.round(score*1000000)/1000000} *"
weights = weights_array[phase]
else
{ score, weights } = try_match(phase, best, -Infinity, true)
best_weights = weights
max = score
best_weights = weights
step = opt.match_range
while step > opt.precision
step **= .5
l2 = best * step
{ score, weights } = try_match(phase, l2, max)
if score > max
max = score
best_weights = weights
best = l2
else
l2 = best / step
{ score, weights } = try_match(phase, l2, max)
if score > max
max = score
best_weights = weights
best = l2
l2_array[phase] = best
weights_array[phase] = best_weights
max
if opt.match
prepare_weights()
score = null
for phase in [MATCH_MIN...N_PHASES]
console.log "Phase #{phase}"
score = tune_match phase, score
coeffs_to_weights outfile: opt.weights, array: weights_array
outfile = opt.outfile or
if opt.logistic
'l2_logistic.json'
else
'l2.json'
process.stdout.write "Writing L2 parameter to '#{outfile}': "
fs.writeFileSync outfile, JSON.stringify(l2_array)
process.stdout.write "done\n"