/
scale.go
482 lines (424 loc) · 11.3 KB
/
scale.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package scale
import (
"encoding/gob"
"errors"
"fmt"
"math"
"sync"
"github.com/reggo/common"
"github.com/gonum/matrix/mat64"
)
func init() {
gob.Register(None{})
gob.Register(Linear{})
gob.Register(Normal{})
//gob.Register(&Probability{})
common.Register(&None{})
common.Register(&Linear{})
common.Register(&Normal{})
//common.Register(&Probability{})
}
// TODO: Check that scalers don't set IsScaled if there is an error, and add
// comment about the behavior
// TODO: Add comment that it is assumed if data can be scaled, it can also be unscaled without error
// IdenticalDimensions is an error type expressing that
// a dimension all had equal values. Dims is a list of unequal dimensions
type UniformDimension struct {
Dims []int
}
func (i *UniformDimension) Error() string {
return "Some dimensions had all values with the same entry"
}
type UnequalLength struct{}
func (u UnequalLength) Error() string {
return "Data length mismatch"
}
// Scalar is an interface for transforming data so it is appropriately scaled
// for the machine learning algorithm. The data are a slice of data points.
// All of the data points must have equal lengths. An error is returned if
// some of the data have unequal lengths or if less than two data points are
// entered
type Scaler interface {
Scale(point []float64) error // Scales (in place) the data point
Unscale(point []float64) error // Unscales (in place) the data point
IsScaled() bool // Returns true if the scale for this type has already been set
Dimensions() int //Number of dimensions for wihich the data was scaled
SetScale(data *mat64.Dense) error // Uses the input data to set the scale
}
type SliceError struct {
Header string
Idx int
Err error
}
func (s *SliceError) Error() string {
return fmt.Sprintf("%v: element %v, error %v", s.Header, s.Idx, s.Err)
}
type ErrorList []*SliceError
func (e ErrorList) Error() string {
return fmt.Sprintf("%v errors found", len(e))
}
// ScaleData is a wrapper for scaling data in parallel.
// TODO: Make this work better so that if there is an error somewhere data isn't changed
func ScaleData(scaler Scaler, data *mat64.Dense) error {
m := &sync.Mutex{}
var e ErrorList
f := func(start, end int) {
for r := start; r < end; r++ {
errTmp := scaler.Scale(data.RowView(r))
if errTmp != nil {
m.Lock()
e = append(e, &SliceError{Header: "scale", Idx: r, Err: errTmp})
m.Unlock()
}
}
}
nSamples, _ := data.Dims()
grain := common.GetGrainSize(nSamples, 1, 500)
common.ParallelFor(nSamples, grain, f)
if len(e) != 0 {
return e
}
return nil
}
// UnscaleData is a wrapper for unscaling data in parallel.
// TODO: Make this work better so that if there is an error somewhere data isn't changed
func UnscaleData(scaler Scaler, data *mat64.Dense) error {
m := &sync.Mutex{}
var e ErrorList
f := func(start, end int) {
for r := start; r < end; r++ {
errTmp := scaler.Unscale(data.RowView(r))
if errTmp != nil {
m.Lock()
e = append(e, &SliceError{Header: "scale", Idx: r, Err: errTmp})
m.Unlock()
}
}
}
nSamples, _ := data.Dims()
grain := common.GetGrainSize(nSamples, 1, 500)
common.ParallelFor(nSamples, grain, f)
if len(e) != 0 {
return e
}
return nil
}
// ScaleTrainingData sets the scale of the scalers if they are not already set
// and then scales the data in inputs and outputs
// TODO: Change so that if any error occurs, scaling will be undone
// TODO: Make run concurrently
func ScaleTrainingData(inputs, outputs *mat64.Dense, inputScaler, outputScaler Scaler) error {
var err error
if !inputScaler.IsScaled() {
err = inputScaler.SetScale(inputs)
if err != nil {
return err
}
}
if !outputScaler.IsScaled() {
err = outputScaler.SetScale(outputs)
if err != nil {
return err
}
}
err = ScaleData(inputScaler, inputs)
if err != nil {
return err
}
err = ScaleData(outputScaler, outputs)
if err != nil {
UnscaleData(inputScaler, inputs)
return err
}
return nil
}
func UnscaleTrainingData(inputs, outputs *mat64.Dense, inputScaler, outputScaler Scaler) error {
UnscaleData(inputScaler, inputs)
UnscaleData(outputScaler, outputs)
return nil
}
// None is a type specifying no transformation of the input should be done
type None struct {
Dim int // Dimensions
Scaled bool
}
func (n None) IsScaled() bool {
return n.Scaled
}
func (n None) Scale(x []float64) error {
return nil
}
func (n None) Unscale(x []float64) error {
return nil
}
func (n None) Dimensions() int {
return n.Dim
}
func (n *None) SetScale(data *mat64.Dense) error {
rows, cols := data.Dims()
if rows < 2 {
return errors.New("scale: less than two inputs")
}
n.Dim = cols
n.Scaled = true
return nil
}
// Linear is a type for scaling the data to be between 0 and 1
type Linear struct {
Min []float64 // Maximum value of the data
Max []float64 // Minimum value of the data
Scaled bool // Flag if the scale has been set
Dim int // Number of dimensions of the data
}
// IsScaled returns true if the scale has been set
func (l *Linear) IsScaled() bool {
return l.Scaled
}
// Dimensions returns the length of the data point
func (l *Linear) Dimensions() int {
return l.Dim
}
// SetScale sets a linear scale between 0 and 1. If no data
// points. If the minimum and maximum value are identical in
// a dimension, the minimum and maximum values will be set to
// that value +/- 0.5 and a
func (l *Linear) SetScale(data *mat64.Dense) error {
rows, dim := data.Dims()
if rows < 2 {
return errors.New("scale: less than two inputs")
}
// Generate data for min and max if they don't already exist
if len(l.Min) < dim {
l.Min = make([]float64, dim)
} else {
l.Min = l.Min[0:dim]
}
if len(l.Max) < dim {
l.Max = make([]float64, dim)
} else {
l.Max = l.Max[0:dim]
}
for i := range l.Min {
l.Min[i] = math.Inf(1)
}
for i := range l.Max {
l.Max[i] = math.Inf(-1)
}
// Find the minimum and maximum in each dimension
for i := 0; i < rows; i++ {
for j := 0; j < dim; j++ {
val := data.At(i, j)
if val < l.Min[j] {
l.Min[j] = val
}
if val > l.Max[j] {
l.Max[j] = val
}
}
}
l.Scaled = true
l.Dim = dim
var unifError *UniformDimension
// Check that the maximum and minimum values are not identical
for i := range l.Min {
if l.Min[i] == l.Max[i] {
if unifError == nil {
unifError = &UniformDimension{}
}
unifError.Dims = append(unifError.Dims, i)
l.Min[i] -= 0.5
l.Max[i] += 0.5
}
}
if unifError != nil {
return unifError
}
return nil
}
// Scales the point returning an error if the length doesn't match
func (l *Linear) Scale(point []float64) error {
if len(point) != l.Dim {
return UnequalLength{}
}
for i, val := range point {
point[i] = (val - l.Min[i]) / (l.Max[i] - l.Min[i])
}
return nil
}
func (l *Linear) Unscale(point []float64) error {
if len(point) != l.Dim {
return UnequalLength{}
}
for i, val := range point {
point[i] = val*(l.Max[i]-l.Min[i]) + l.Min[i]
}
return nil
}
// Normal scales the data to have a mean of 0 and a variance of 1
// in each dimension
type Normal struct {
Mu []float64
Sigma []float64
Dim int
Scaled bool
}
// IsScaled returns true if the scale has been set
func (n *Normal) IsScaled() bool {
return n.Scaled
}
// Dimensions returns the length of the data point
func (n *Normal) Dimensions() int {
return n.Dim
}
// SetScale Finds the appropriate scaling of the data such that the dataset has
// a mean of 0 and a variance of 1. If the standard deviation of any of
// the data is zero (all of the entries have the same value),
// the standard deviation is set to 1.0 and a UniformDimension error is
// returned
func (n *Normal) SetScale(data *mat64.Dense) error {
rows, dim := data.Dims()
if rows < 2 {
return errors.New("scale: less than two inputs")
}
// Need to find the mean input and the std of the input
mean := make([]float64, dim)
for i := 0; i < rows; i++ {
for j := 0; j < dim; j++ {
mean[j] += data.At(i, j)
}
}
for i := range mean {
mean[i] /= float64(rows)
}
// TODO: Replace this with something that has better numerical properties
std := make([]float64, dim)
for i := 0; i < rows; i++ {
for j := 0; j < dim; j++ {
diff := data.At(i, j) - mean[j]
std[j] += diff * diff
}
}
for i := range std {
std[i] /= float64(rows)
std[i] = math.Sqrt(std[i])
}
n.Scaled = true
n.Dim = dim
var unifError *UniformDimension
for i := range std {
if std[i] == 0 {
if unifError == nil {
unifError = &UniformDimension{}
}
unifError.Dims = append(unifError.Dims, i)
std[i] = 1.0
}
}
n.Mu = mean
n.Sigma = std
if unifError != nil {
return unifError
}
return nil
}
// Scale scales the data point
func (n *Normal) Scale(point []float64) error {
if len(point) != n.Dim {
return UnequalLength{}
}
for i := range point {
point[i] = (point[i] - n.Mu[i]) / n.Sigma[i]
}
return nil
}
// Unscale unscales the data point
func (n *Normal) Unscale(point []float64) error {
if len(point) != n.Dim {
return UnequalLength{}
}
for i := range point {
point[i] = point[i]*n.Sigma[i] + n.Mu[i]
}
return nil
}
/*
type ProbabilityDistribution interface {
Fit([]float64) error
CumProb(float64) float64
Quantile(float64) float64
Prob(float64) float64
}
// Probability scales the inputs based on the supplied
// probability distributions
type Probability struct {
UnscaledDistribution []ProbabilityDistribution // Probabilitiy distribution from which the data come
ScaledDistribution []ProbabilityDistribution // Probability distribution to which the data should be scaled
Dim int
Scaled bool
}
// IsScaled returns true if the scale has been set
func (p *Probability) IsScaled() bool {
return p.Scaled
}
// Dimensions returns the length of the data point
func (p *Probability) Dimensions() int {
return p.Dim
}
func (p *Probability) SetScale(data *mat64.Dense) error {
err := checkInputs(data)
if err != nil {
return err
}
p.Dim = len(data[0])
if len(p.UnscaledDistribution) != p.Dim {
return errors.New("Number of unscaled probability distributions must equal dimension")
}
if len(p.ScaledDistribution) != p.Dim {
return errors.New("Unscaled distribution not set")
}
tmp := make([]float64, len(data))
for i := 0; i < p.Dim; i++ {
// Collect all the data into tmp
for j, point := range data {
tmp[j] = point[i]
}
// Fit the probability distribution using the samples
p.UnscaledDistribution[i].Fit(tmp)
}
return nil
}
func (p *Probability) Scale(point []float64) error {
if len(point) != p.Dim {
return UnequalLength{}
}
for i := range point {
// Check that the point doesn't have zero probability
if p.UnscaledDistribution[i].Prob(point[i]) == 0 {
return errors.New("Zero probability point")
}
prob := p.UnscaledDistribution[i].CumProb(point[i])
point[i] = p.ScaledDistribution[i].Quantile(prob)
if math.IsInf(point[i], 0) {
panic("inf point")
}
if math.IsNaN(point[i]) {
panic("NaN point")
}
}
return nil
}
func (p *Probability) Unscale(point []float64) error {
if len(point) != p.Dim {
return UnequalLength{}
}
for i := range point {
// Check that the point doesn't have zero probability
if p.UnscaledDistribution[i].Prob(point[i]) == 0 {
return errors.New("Zero probability point")
}
prob := p.ScaledDistribution[i].CumProb(point[i])
point[i] = p.UnscaledDistribution[i].Quantile(prob)
}
return nil
}
*/