-
Notifications
You must be signed in to change notification settings - Fork 13
/
train.go
136 lines (128 loc) · 3.24 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
125
126
127
128
129
130
131
132
133
134
135
136
package lstm
import (
"context"
"errors"
"sync"
"github.com/owulveryck/lstm/datasetter"
G "gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
// the cost function
func (l *lstm) cost(dataSet datasetter.Trainer) (cost, perplexity, hidden, cell *G.Node, err error) {
hidden, cell, err = l.forwardStep(dataSet, l.prevHidden, l.prevCell, 0)
if err != nil {
return nil, nil, nil, nil, err
}
var loss, perp *G.Node
// Evaluate the cost
for i, computedVector := range dataSet.GetComputedVectors() {
expectedValue, err := dataSet.GetExpectedValue(i)
if err != nil {
return nil, nil, nil, nil, err
}
logprob := G.Must(G.Neg(G.Must(G.Log(computedVector))))
loss = G.Must(G.Slice(logprob, G.S(expectedValue)))
log2prob := G.Must(G.Neg(G.Must(G.Log2(computedVector))))
perp = G.Must(G.Slice(log2prob, G.S(expectedValue)))
if cost == nil {
cost = loss
} else {
cost = G.Must(G.Add(cost, loss))
}
G.WithName("Cost")(cost)
if perplexity == nil {
perplexity = perp
} else {
perplexity = G.Must(G.Add(perplexity, perp))
}
}
//l.prevHidden = hidden
//l.prevCell = cell
//g := l.g.SubgraphRoots(cost, perplexity)
//l.g = g
return
}
// TrainingInfos returns info about the current training process
type TrainingInfos struct {
Step int
Perplexity float32
Cost float32
}
// Train the model
func (m *Model) Train(ctx context.Context, dset datasetter.FullTrainer, solver G.Solver, pauseChan <-chan struct{}) (<-chan TrainingInfos, <-chan error) {
infoChan := make(chan TrainingInfos, 0)
step := 0
errc := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(1)
paused := false
go func() {
if len(pauseChan) != 0 {
errc <- errors.New("pauseChan must not be buffered")
wg.Done()
return
}
var hiddenT, cellT tensor.Tensor
for {
select {
case <-ctx.Done():
errc <- nil
wg.Done()
return
case <-pauseChan:
paused = true
default:
if paused {
<-pauseChan
paused = false
}
step++
if hiddenT == nil {
hiddenT = tensor.New(tensor.Of(tensor.Float32), tensor.WithShape(m.hiddenSize))
}
if cellT == nil {
cellT = tensor.New(tensor.Of(tensor.Float32), tensor.WithShape(m.hiddenSize))
}
lstm := m.newLSTM(hiddenT, cellT)
trainer, err := dset.GetTrainer()
if err != nil {
errc <- err
wg.Done()
return
}
cost, perplexity, hidden, cell, err := lstm.cost(trainer)
if err != nil {
errc <- err
wg.Done()
return
}
machine := G.NewLispMachine(lstm.g)
if err := machine.RunAll(); err != nil {
errc <- err
wg.Done()
return
}
// send infos about this execution step in a non blocking channel
select {
case infoChan <- TrainingInfos{
Perplexity: perplexity.Value().Data().(float32),
Cost: cost.Value().Data().(float32),
Step: step,
}:
default:
}
copy(hiddenT.Data().([]float32), hidden.Value().Data().([]float32))
copy(cellT.Data().([]float32), cell.Value().Data().([]float32))
solver.Step(G.Nodes{
lstm.biasC, lstm.biasF, lstm.biasI, lstm.biasO, lstm.biasY,
lstm.uc, lstm.uf, lstm.ui, lstm.uo,
lstm.wc, lstm.wf, lstm.wi, lstm.wo, lstm.wy})
}
}
}()
go func() {
wg.Wait()
close(infoChan)
}()
return infoChan, errc
}