-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.go
111 lines (94 loc) · 2.99 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
package nn
import (
"go4ml.xyz/base/fu"
"go4ml.xyz/base/model"
"go4ml.xyz/base/tables"
"go4ml.xyz/zorros"
"reflect"
)
type ModelMapFunction func(network *Network, features []string, predicts string) model.MemorizeMap
func DefaultModelMap(network *Network, features []string, predicts string) model.MemorizeMap {
return model.MemorizeMap{"model": mnemosyne{network, features, predicts}}
}
func Train(e Model, dataset model.Dataset, w model.Workout, mmf ModelMapFunction) (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
}
Label := fu.Fnzs(dataset.Label, model.LabelCol)
if fu.IndexOf(Label, t.Names()) < 0 {
err = zorros.Errorf("dataset does not have column `%v`", Label)
return
}
if e.BatchSize <= 0 {
e.BatchSize = DefaultBatchSize
}
predicts := fu.Fnzs(e.Predicted, model.PredictedCol)
network := New(e.Context.Upgrade(), e.Network, e.Input, e.Loss, e.BatchSize, e.Seed)
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())
network.SummaryOut(true, w.Verbose)
for done := false; w != nil && !done; w = w.Next() {
opt := e.Optimizer.Init(w.Iteration())
if err = train.Drain(func(value reflect.Value) error {
if value.Kind() == reflect.Bool {
return nil
}
t := value.Interface().(*tables.Table)
m, err := t.MatrixWithLabel(features, Label, e.BatchSize)
if err != nil {
return err
}
network.Train(m.Features, m.Labels, opt)
return nil
}); err != nil {
return
}
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.MatrixWithLabel(features, Label, e.BatchSize)
if err != nil {
return err
}
network.Label.SetValues(m.Labels)
network.Forward(m.Features, out)
resultCol := tables.MatrixColumn(out, e.BatchSize)
labelCol := t.Col(Label)
network.Loss.CopyValuesTo(loss)
l := loss[0]
for i, c := range t.Col(Test).ExtractAs(fu.Bool, true).([]bool) {
if len(loss) > 1 {
l = loss[i]
}
if c {
testmu.Update(resultCol.Value(i), labelCol.Value(i), float64(l))
} else {
trainmu.Update(resultCol.Value(i), labelCol.Value(i), float64(l))
}
}
return nil
}); err != nil {
return
}
lr0, _ := trainmu.Complete()
lr1, d := testmu.Complete()
memorize := mmf(network, features, predicts)
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
}