/
train.go
186 lines (166 loc) · 4.62 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
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
package model
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"sync"
"time"
"github.com/lwch/gotorch/optimizer"
"github.com/lwch/gotorch/tensor"
"github.com/lwch/runtime"
"github.com/lwch/tnn/example/couplet/logic/feature"
"github.com/lwch/tnn/example/couplet/logic/sample"
"github.com/olekukonko/tablewriter"
)
// Train 训练模型
func (m *Model) Train(sampleDir, modelDir string) {
// go func() { // for pprof
// http.ListenAndServe(":8888", nil)
// }()
m.modelDir = modelDir
runtime.Assert(os.MkdirAll(modelDir, 0755))
// 加载样本
m.vocabs, m.vocabsIdx = feature.LoadVocab(filepath.Join(sampleDir, "vocabs"))
trainX := feature.LoadData(filepath.Join(sampleDir, "in.txt"), m.vocabsIdx, -1)
trainY := feature.LoadData(filepath.Join(sampleDir, "out.txt"), m.vocabsIdx, -1)
for i := 0; i < len(trainX); i++ {
m.samples = append(m.samples, sample.New(trainX[i], trainY[i]))
}
// 加载embedding
if _, err := os.Stat(filepath.Join(modelDir, "embedding")); os.IsNotExist(err) {
m.buildEmbedding(filepath.Join(modelDir, "embedding"))
}
if _, err := os.Stat(filepath.Join(modelDir, "couplet.model")); !os.IsNotExist(err) {
m.Load(m.modelDir)
} else {
m.copyVocabs(filepath.Join(sampleDir, "vocabs"))
m.embedding = m.loadEmbedding(filepath.Join(modelDir, "embedding"))
m.build()
}
m.total = len(m.samples)
m.optimizer = optimizer.NewAdam(optimizer.WithAdamLr(lr))
// optimizer := optimizer.NewSGD(lr, 0)
go m.showProgress()
begin := time.Now()
for i := 0; i < epoch; i++ {
m.epoch = i + 1
loss := m.trainEpoch()
// m.optimizer.Step(m.params())
m.save()
fmt.Printf("train %d, cost=%s, loss=%f\n",
i+1, time.Since(begin).String(),
loss)
if i == 0 {
m.showModelInfo()
}
}
m.save()
}
func (m *Model) trainWorker(samples []*sample.Sample) float64 {
x := make([]float32, 0, len(samples)*paddingSize*embeddingDim)
y := make([]int64, 0, len(samples)*paddingSize)
padding := make([]int, 0, len(samples))
for _, s := range samples {
xTrain, yTrain, p := s.Embedding(paddingSize, m.embedding)
x = append(x, xTrain...)
y = append(y, yTrain...)
padding = append(padding, p)
}
xIn := tensor.FromFloat32(storage, x, int64(len(samples)), paddingSize, embeddingDim)
yOut := tensor.FromInt64(storage, y, int64(len(samples)), paddingSize)
pred := m.forward(xIn, padding, true)
pred = pred.Permute(0, 2, 1)
loss := lossFunc(pred, yOut)
loss.Backward()
m.current.Add(uint64(len(samples)))
return loss.Value()
}
func (m *Model) trainBatch(b []batch) float64 {
var wg sync.WaitGroup
wg.Add(len(b))
var sum float64
for i := 0; i < len(b); i++ {
go func(samples []*sample.Sample) {
defer wg.Done()
sum += m.trainWorker(samples)
}(b[i].data)
}
wg.Wait()
storage.GC()
m.optimizer.Step(m.params())
return sum / float64(len(b))
}
// trainEpoch 运行一个批次
func (m *Model) trainEpoch() float64 {
m.status = statusTrain
m.current.Store(0)
// 生成索引序列
idx := make([]int, len(m.samples))
for i := 0; i < len(idx); i++ {
idx[i] = i
}
rand.Shuffle(len(idx), func(i, j int) {
idx[i], idx[j] = idx[j], idx[i]
})
workerCount := 2 // 可能由于超线程技术,此处2个并发速度最快
var batches []batch
for i := 0; i < len(m.samples); i += batchSize {
var b batch
for j := 0; j < batchSize; j++ {
if i+j >= len(m.samples) {
break
}
b.append(m.samples[idx[i+j]])
}
batches = append(batches, b)
}
var trainBatch []batch
var sum float64
var size float64
for _, b := range batches {
trainBatch = append(trainBatch, b)
if len(trainBatch) >= workerCount {
sum += m.trainBatch(trainBatch)
trainBatch = trainBatch[:0]
size++
}
}
if len(trainBatch) > 0 {
sum += m.trainBatch(trainBatch)
size++
}
return sum / size
}
func (m *Model) showModelInfo() {
table := tablewriter.NewWriter(os.Stdout)
defer table.Render()
table.SetHeader([]string{"name", "count"})
var total int64
for _, attn := range m.attn {
cnt := paramSize(attn.attn.Params())
total += cnt
table.Append([]string{attn.attn.Name(), fmt.Sprintf("%d", cnt)})
cnt = paramSize(attn.dense.Params())
total += cnt
table.Append([]string{attn.dense.Name(), fmt.Sprintf("%d", cnt)})
cnt = paramSize(attn.output.Params())
total += cnt
table.Append([]string{attn.output.Name(), fmt.Sprintf("%d", cnt)})
}
cnt := paramSize(m.output.Params())
total += cnt
table.Append([]string{m.output.Name(), fmt.Sprintf("%d", cnt)})
table.Append([]string{"total", fmt.Sprintf("%d", total)})
}
func paramSize(params map[string]*tensor.Tensor) int64 {
var ret int64
for _, p := range params {
size := int64(1)
for _, s := range p.Shapes() {
size *= s
}
ret += size
}
return ret
}