-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.go
More file actions
272 lines (250 loc) · 6.46 KB
/
tutorial.go
File metadata and controls
272 lines (250 loc) · 6.46 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package tutorial
import (
"bitbucket.org/dtolpin/gogp/gp"
"bitbucket.org/dtolpin/infergo/ad"
"bitbucket.org/dtolpin/infergo/infer"
"bitbucket.org/dtolpin/infergo/model"
"encoding/csv"
"flag"
"fmt"
"gonum.org/v1/gonum/optimize"
"gonum.org/v1/gonum/stat"
"io"
"math"
"math/rand"
"os"
"strconv"
"time"
)
var (
OPTINP = false
MINOPT = 0
ALG = "lbfgs"
PARALLEL = false
ITERS = 1000 // major iterations
MINITERS = 10 // minimum iterations to accept in lbfgs
THRESHOLD = 1e-6 // gradient threshold
RATE = 0.01 // learning rate (for Adam)
NTASKS = 0
NONORMALIZE = false
OUTOFSAMPLE = false
)
func init() {
rand.Seed(time.Now().UnixNano())
flag.StringVar(&ALG, "a", ALG,
"optimization algorithm + adam or lbfgs)")
flag.BoolVar(&PARALLEL, "p", PARALLEL,
"compute covariance in parallel")
flag.BoolVar(&NONORMALIZE, "n", NONORMALIZE,
"normalize outputs")
flag.BoolVar(&OUTOFSAMPLE, "o", OUTOFSAMPLE,
"forecast out of sample")
}
// Evaluate evaluates Gaussian process on CSV data. One step
// out of sample forecast is recorded for each time point, along
// with the hyperparameters. This function is called by all
// case studies in the tutorial. For optimization, LBFGS from
// the gonum library (http://gonum.org) is used for faster
// execution. In general though, LBFGS is a bit of hit-or-miss,
// failing to optimize occasionally, so in real applications a
// different optimization/inference algorithm may be a better
// choice.
func Evaluate(
gp *gp.GP, // gaussian process
m model.Model, // optimization model
theta []float64, // initial values of hyperparameters
rdr io.Reader, // data
wtr io.Writer, // forecasts
) error {
if PARALLEL {
ad.MTSafeOn()
}
gp.Parallel = ad.IsMTSafe()
// Load the data
var err error
fmt.Fprint(os.Stderr, "loading...")
X, Y, err := load(rdr)
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, "done")
// Normalize Y
var meany, stdy float64
if NONORMALIZE {
meany, stdy = 0., 1.
} else {
meany, stdy = stat.MeanStdDev(Y, nil)
for i := range Y {
Y[i] = (Y[i] - meany) / stdy
}
}
// Forecast one step out of sample, iteratively.
// Output data augmented with predictions.
fmt.Fprintln(os.Stderr, "Forecasting...")
for end := 0; end != len(X); end++ {
Xi := X[:end]
Yi := Y[:end]
// Construct the initial point in the optimization space
var x []float64
if OPTINP {
// If the inputs are optimized as well as the
// hyperparameters, the inputs are appended to the
// parameter vector of Observe.
x = make([]float64, len(theta)+len(Xi)*(gp.NDim+1))
copy(x, theta)
k := len(theta)
for j := range Xi {
copy(x[k:], Xi[j])
k += gp.NDim
}
copy(x[k:], Yi)
} else {
// If only the hyperparameters are optimized, the
// inputs are stored in the fields of the GP.
x = make([]float64, len(theta))
copy(x, theta)
gp.X = Xi
gp.Y = Yi
}
// Randomize the initial values of hyperparameters
for i := range theta {
x[i] += 0.1 * rand.NormFloat64()
}
// Initial log likelihood
lml0 := m.Observe(x)
model.DropGradient(m)
if len(gp.X) > MINOPT {
switch ALG {
case "lbfgs":
// Optimize the parameters
Func, Grad := infer.FuncGrad(m)
p := optimize.Problem{Func: Func, Grad: Grad}
// For some kernels and data, the optimizing of
// hyperparameters does not make sense with too few
// points.
result, err := optimize.Minimize(
p, x, &optimize.Settings{
MajorIterations: ITERS,
GradientThreshold: THRESHOLD,
Concurrent: NTASKS,
}, nil)
// We do not need the optimizer to `officially'
// converge, a few iterations usually bring most
// of the improvement. However, in pathological
// cases even a few iterations do not succeed,
// and we want to report that.
if err != nil && result.Stats.MajorIterations <= MINITERS {
// There was a problem and the optimizer stopped
// too early.
fmt.Fprintf(os.Stderr,
"%d: stuck after %d iterations: %v\n",
end, result.Stats.MajorIterations, err)
}
x = result.X
case "adam":
opt := &infer.Adam{Rate: RATE}
epoch := 0
Epochs:
for ; epoch != ITERS; epoch++ {
_, grad := opt.Step(m, x)
for i := range grad {
if math.Abs(grad[i]) >= THRESHOLD {
continue Epochs
}
}
break Epochs
}
}
}
// Final log likelihood
lml := m.Observe(x)
model.DropGradient(m)
ad.DropAllTapes()
// Forecast
Z := X[end : end+1]
mu, sigma, err := gp.Produce(Z)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to forecast: %v\n", err)
}
// Output forecasts
z := Z[0]
for j := range z {
fmt.Fprintf(wtr, "%f,", z[j])
}
fmt.Fprintf(wtr, "%f,%f,%f,%f,%f",
Y[end]*stdy + meany,
mu[0]*stdy + meany,
sigma[0]*stdy,
lml0, lml)
for i := 0; i != len(theta); i++ {
fmt.Fprintf(wtr, ",%f", math.Exp(x[i]))
}
fmt.Fprintln(wtr)
}
if OUTOFSAMPLE {
Z := make([][]float64, len(X))
for i := range Z {
Z[i] = make([]float64, len(X[0]))
copy(Z[i], X[i])
for j := range Z[i] {
Z[i][j] += X[len(X)-1][j]
}
}
Z = Z[1:]
mu, sigma, err := gp.Produce(Z)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to forecast: %v\n", err)
}
// Output forecasts
for i := range Z {
z := Z[i]
for j := range z {
fmt.Fprintf(wtr, "%f,", z[j])
}
fmt.Fprintf(wtr, "nan,%f,%f", mu[i]*stdy + meany, sigma[i]*stdy)
fmt.Fprintln(wtr)
}
}
fmt.Fprintln(os.Stderr, "done")
return nil
}
// load parses the data from csv and returns inputs and outputs,
// suitable for feeding to the GP.
func load(rdr io.Reader) (
x [][]float64,
y []float64,
err error,
) {
csv := csv.NewReader(rdr)
RECORDS:
for {
record, err := csv.Read()
switch err {
case nil:
// record contains the data
xi := make([]float64, len(record)-1)
i := 0
for ; i != len(record)-1; i++ {
xi[i], err = strconv.ParseFloat(record[i], 64)
if err != nil {
// data error
return x, y, err
}
}
yi, err := strconv.ParseFloat(record[i], 64)
if err != nil {
// data error
return x, y, err
}
x = append(x, xi)
y = append(y, yi)
case io.EOF:
// end of file
break RECORDS
default:
// i/o error
return x, y, err
}
}
return x, y, err
}