forked from huichen/mlf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lbfgs.go
283 lines (237 loc) · 7.68 KB
/
lbfgs.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
package optimizer
import (
"flag"
"github.com/huichen/mlf/data"
"github.com/huichen/mlf/util"
"log"
"math"
"runtime"
)
var (
lbfgs_history_size = flag.Int("lbfgs_history_size", 5, "L-BFGS中存储的历史步长")
lbfgs_threads = flag.Int("lbfgs_threads", 0, "使用多少个协程进行LBFGS收敛,值为0时使用所有CPU")
)
// limited-memory BFGS优化器
//
// l-bfgs的迭代算法见下面的论文
// Nocedal, J. (1980). "Updating Quasi-Newton Matrices with Limited Storage".
// Mathematics of Computation 35 (151): 773–782. doi:10.1090/S0025-5718-1980-0572855-7
//
// 这种方法最多保存最近m步的中间结果用以计算海森矩阵的近似值。
//
// 请用NewOptimizer函数建立新的优化器。
//
// 注意optimizer是
// 1. 协程不安全的,请为每个协程建一个optimizer;
// 2. 迭代不安全的,optimizer会记录每个迭代步骤的中间结果,如果需要重新开始新的优化,
// 请调用Clear函数。
type lbfgsOptimizer struct {
// 初始化参数
options OptimizerOptions
// 当前的步数,从0开始
// 如果需要重新优化,请调用Clear函数
k int
// 自变量
x []*util.Matrix
// 目标函数的偏导数向量
g []*util.Matrix
// s_k = x_(k+1) - x_k
s []*util.Matrix
// y_k = g_(k+1) - g_k
y []*util.Matrix
// ro_k = 1 / Y_k .* s_k
ro *util.Vector
// 特征向量维度
labels int
// 临时变量
q, z *util.Matrix
alpha, beta *util.Vector
}
// 开辟新的lbfgsOptimizer指针
func NewLbfgsOptimizer(options OptimizerOptions) Optimizer {
opt := new(lbfgsOptimizer)
opt.options = options
opt.k = 0
return opt
}
// 初始化优化结构体
// 为结构体中的向量分配新的内存,向量的长度可能发生变化。
func (opt *lbfgsOptimizer) initStruct(labels, features int, isSparse bool) {
opt.labels = labels
opt.x = make([]*util.Matrix, *lbfgs_history_size)
opt.g = make([]*util.Matrix, *lbfgs_history_size)
opt.s = make([]*util.Matrix, *lbfgs_history_size)
opt.y = make([]*util.Matrix, *lbfgs_history_size)
opt.ro = util.NewVector(*lbfgs_history_size)
opt.alpha = util.NewVector(*lbfgs_history_size)
opt.beta = util.NewVector(*lbfgs_history_size)
if !isSparse {
opt.q = util.NewMatrix(labels, features)
opt.z = util.NewMatrix(labels, features)
for i := 0; i < *lbfgs_history_size; i++ {
opt.x[i] = util.NewMatrix(labels, features)
opt.g[i] = util.NewMatrix(labels, features)
opt.s[i] = util.NewMatrix(labels, features)
opt.y[i] = util.NewMatrix(labels, features)
}
} else {
opt.q = util.NewSparseMatrix(labels)
opt.z = util.NewSparseMatrix(labels)
for i := 0; i < *lbfgs_history_size; i++ {
opt.x[i] = util.NewSparseMatrix(labels)
opt.g[i] = util.NewSparseMatrix(labels)
opt.s[i] = util.NewSparseMatrix(labels)
opt.y[i] = util.NewSparseMatrix(labels)
}
}
}
// 清除结构体中保存的数据,以便重复使用结构体
func (opt *lbfgsOptimizer) Clear() {
opt.k = 0
}
// 输入x_k和g_k,返回x需要更新的增量 d_k = - H_k * g_k
func (opt *lbfgsOptimizer) GetDeltaX(x, g *util.Matrix) *util.Matrix {
if x.NumLabels() != g.NumLabels() {
log.Fatal("x和g的维度不一致")
}
// 第一次调用时开辟内存
if opt.k == 0 {
if x.IsSparse() {
opt.initStruct(x.NumLabels(), 0, x.IsSparse())
} else {
opt.initStruct(x.NumLabels(), x.NumValues(), x.IsSparse())
}
}
currIndex := util.Mod(opt.k, *lbfgs_history_size)
// 更新x_k
opt.x[currIndex].DeepCopy(x)
// 更新g_k
opt.g[currIndex].DeepCopy(g)
// 当为第0步时,使用简单的gradient descent
if opt.k == 0 {
opt.k++
return g.Opposite()
}
prevIndex := util.Mod(opt.k-1, *lbfgs_history_size)
// 更新s_(k-1)
opt.s[prevIndex].WeightedSum(opt.x[currIndex], opt.x[prevIndex], 1, -1)
// 更新y_(k-1)
opt.y[prevIndex].WeightedSum(opt.g[currIndex], opt.g[prevIndex], 1, -1)
// 更新ro_(k-1)
opt.ro.Set(prevIndex, 1.0/util.MatrixDotProduct(opt.y[prevIndex], opt.s[prevIndex]))
// 计算两个循环的下限
lowerBound := opt.k - *lbfgs_history_size
if lowerBound < 0 {
lowerBound = 0
}
// 第一个循环
opt.q.DeepCopy(g)
for i := opt.k - 1; i >= lowerBound; i-- {
currIndex := util.Mod(i, *lbfgs_history_size)
opt.alpha.Set(currIndex,
opt.ro.Get(currIndex)*util.MatrixDotProduct(opt.s[currIndex], opt.q))
opt.q.Increment(opt.y[currIndex], -opt.alpha.Get(currIndex))
}
// 第二个循环
opt.z.DeepCopy(opt.q)
for i := lowerBound; i <= opt.k-1; i++ {
currIndex := util.Mod(i, *lbfgs_history_size)
opt.beta.Set(currIndex,
opt.ro.Get(currIndex)*util.MatrixDotProduct(opt.y[currIndex], opt.z))
opt.z.Increment(opt.s[currIndex],
opt.alpha.Get(currIndex)-opt.beta.Get(currIndex))
}
// 更新k
opt.k++
return opt.z.Opposite()
}
func (opt *lbfgsOptimizer) OptimizeWeights(
weights *util.Matrix, derivative_func ComputeInstanceDerivativeFunc, set data.Dataset) {
// 学习率计算器
learningRate := NewLearningRate(opt.options)
// 偏导数向量
derivative := weights.Populate()
// 优化循环
step := 0
convergingSteps := 0
oldWeights := weights.Populate()
weightsDelta := weights.Populate()
// 为各个工作协程开辟临时资源
numLbfgsThreads := *lbfgs_threads
if numLbfgsThreads == 0 {
numLbfgsThreads = runtime.NumCPU()
}
workerSet := make([]data.Dataset, numLbfgsThreads)
workerDerivative := make([]*util.Matrix, numLbfgsThreads)
workerInstanceDerivative := make([]*util.Matrix, numLbfgsThreads)
for iWorker := 0; iWorker < numLbfgsThreads; iWorker++ {
workerBuckets := []data.SkipBucket{
{true, iWorker},
{false, 1},
{true, numLbfgsThreads - 1 - iWorker},
}
workerSet[iWorker] = data.NewSkipDataset(set, workerBuckets)
workerDerivative[iWorker] = weights.Populate()
workerInstanceDerivative[iWorker] = weights.Populate()
}
log.Print("开始L-BFGS优化")
for {
if opt.options.MaxIterations > 0 && step >= opt.options.MaxIterations {
break
}
step++
// 开始工作协程
workerChannel := make(chan int, numLbfgsThreads)
for iWorker := 0; iWorker < numLbfgsThreads; iWorker++ {
go func(iw int) {
workerDerivative[iw].Clear()
iterator := workerSet[iw].CreateIterator()
iterator.Start()
for !iterator.End() {
instance := iterator.GetInstance()
derivative_func(
weights, instance, workerInstanceDerivative[iw])
// log.Print(workerInstanceDerivative[iw].GetValues(0))
workerDerivative[iw].Increment(
workerInstanceDerivative[iw], float64(1)/float64(set.NumInstances()))
iterator.Next()
}
workerChannel <- iw
}(iWorker)
}
derivative.Clear()
// 等待工作协程结束
for iWorker := 0; iWorker < numLbfgsThreads; iWorker++ {
<-workerChannel
}
for iWorker := 0; iWorker < numLbfgsThreads; iWorker++ {
derivative.Increment(workerDerivative[iWorker], 1)
}
// 添加正则化项
derivative.Increment(ComputeRegularization(weights, opt.options), 1.0/float64(set.NumInstances()))
// 计算特征权重的增量
delta := opt.GetDeltaX(weights, derivative)
// 根据学习率更新权重
learning_rate := learningRate.ComputeLearningRate(delta)
weights.Increment(delta, learning_rate)
weightsDelta.WeightedSum(weights, oldWeights, 1, -1)
oldWeights.DeepCopy(weights)
weightsNorm := weights.Norm()
weightsDeltaNorm := weightsDelta.Norm()
log.Printf("#%d |w|=%1.3g |dw|/|w|=%1.3g lr=%1.3g", step, weightsNorm, weightsDeltaNorm/weightsNorm, learning_rate)
// 判断是否溢出
if math.IsNaN(weightsNorm) {
log.Fatal("优化失败:不收敛")
}
// 判断是否收敛
if weightsDeltaNorm/weightsNorm < opt.options.ConvergingDeltaWeight {
convergingSteps++
if convergingSteps > opt.options.ConvergingSteps {
log.Printf("收敛")
break
}
} else {
convergingSteps = 0
}
}
}