-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.go
124 lines (105 loc) · 2.82 KB
/
train.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
package vae
import (
"go-ml.dev/pkg/base/fu"
"go-ml.dev/pkg/base/model"
"go-ml.dev/pkg/base/tables"
"go-ml.dev/pkg/nn"
"go-ml.dev/pkg/nn/mx"
"go-ml.dev/pkg/zorros"
"reflect"
)
func train(e Model, dataset model.Dataset, w model.Workout) (report *model.Report, err error) {
t, err := dataset.Source.Lazy().First(1).Collect()
if err != nil {
return
}
features := t.OnlyNames(dataset.Features...)
Test := fu.Fnzs(dataset.Test, model.TestCol)
if fu.IndexOf(Test, t.Names()) < 0 {
err = zorros.Errorf("dataset does not have column `%v`", Test)
return
}
if e.Width <= 0 {
m, er := t.Matrix(features, 1)
if er != nil {
return nil, zorros.Wrapf(er, "failed to get features matrix: %s", er.Error())
}
e.Width = m.Width
}
if e.Hidden <= 0 {
e.Hidden = e.Width / 2
}
if e.Optimizer == nil {
e.Optimizer = nn.Adam{Lr: .001}
}
if e.BatchSize <= 0 {
e.BatchSize = DefaultBatchSize
}
if e.Beta < 1 {
e.Beta = 1
}
network := nn.New(
e.Context.Upgrade(),
&nn.Lambda{e.autoencoder},
mx.Dim(e.Width),
nn.LossFunc(e.loss),
e.BatchSize,
e.Seed)
//network.PrintSummary(true)
memorize := e.modelmap(network, features)
train := dataset.Source.Lazy().IfNotFlag(dataset.Test).Batch(e.BatchSize).Parallel()
full := dataset.Source.Lazy().Batch(e.BatchSize).Parallel()
out := make([]float32, network.Graph.Output.Dim().Total())
loss := make([]float32, network.Graph.Loss.Dim().Total())
for done := false; w != nil && !done; w = w.Next() {
opt := e.Optimizer.Init(w.Iteration())
network.Params["_sampling"].Ones()
if err = train.Drain(func(value reflect.Value) error {
if value.Kind() == reflect.Bool {
return nil
}
t := value.Interface().(*tables.Table)
m, err := t.Matrix(features, e.BatchSize)
if err != nil {
return err
}
network.Train(m.Features, nil, opt)
return nil
}); err != nil {
return
}
network.Params["_sampling"].Zeros()
trainmu := w.TrainMetrics()
testmu := w.TestMetrics()
if err = full.Drain(func(value reflect.Value) error {
if value.Kind() == reflect.Bool {
return nil
}
t := value.Interface().(*tables.Table)
m, err := t.Matrix(features, e.BatchSize)
if err != nil {
return err
}
network.Forward(m.Features, out)
network.Loss.CopyValuesTo(loss)
lc := m.AsColumn()
rt := tables.MatrixColumn(out, e.BatchSize)
for i, c := range t.Col(Test).ExtractAs(fu.Bool, true).([]bool) {
if c {
testmu.Update(rt.Value(i), lc.Value(i), float64(loss[i]))
} else {
trainmu.Update(rt.Value(i), lc.Value(i), float64(loss[i]))
}
}
return nil
}); err != nil {
return
}
lr0, _ := trainmu.Complete()
lr1, d := testmu.Complete()
if report, done, err = w.Complete(memorize, lr0, lr1, d); err != nil {
return nil, zorros.Wrapf(err, "tailed to complete model: %s", err.Error())
}
}
return
}