forked from xlvector/hector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue: implement OWLQN and LBFGS and pass the unittests
issue: rename lbfgs_minimizer_test.go to minimizer_test.go
- Loading branch information
Showing
6 changed files
with
260 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package hector | ||
|
||
// Description: function for minimizer such as LBFGS and OWLQN | ||
type DiffFunction interface { | ||
Value(pos *Vector) float64 | ||
Gradient(pos *Vector) *Vector | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package hector | ||
|
||
import ("fmt" | ||
"math") | ||
|
||
/** | ||
* It's based the paper "Scalable Training of L1-Regularized Log-Linear Models" | ||
* by Galen Andrew and Jianfeng Gao | ||
* user: weixuan | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
type OWLQNMinimizer struct { | ||
l1reg float64 | ||
costFun DiffFunction | ||
numHist int | ||
maxIteration int | ||
tolerance float64 | ||
} | ||
|
||
func NewOWLQNMinimizer(l1reg float64) *OWLQNMinimizer { | ||
m := new(OWLQNMinimizer) | ||
m.l1reg = l1reg | ||
m.numHist = 10 | ||
m.maxIteration = 20 | ||
m.tolerance = 1e-4 | ||
return m | ||
} | ||
|
||
func (m *OWLQNMinimizer) Minimize(costfun DiffFunction, init *Vector) *Vector { | ||
m.costFun = costfun; | ||
var cost float64 = m.Evaluate(init) | ||
var grad *Vector = costfun.Gradient(init).Copy() | ||
var pos *Vector = init.Copy() | ||
var terminalCriterion *relativeMeanImprCriterion = NewRelativeMeanImprCriterion(m.tolerance) | ||
terminalCriterion.addCost(cost) | ||
|
||
var helper *QuasiNewtonHelper = NewQuasiNewtonHelper(m.numHist, m, pos, grad) | ||
fmt.Println("Iter\tcost\timprovement") | ||
fmt.Printf("%d\t%e\tUndefined\n", 0, cost) | ||
for iter:=1; iter <= m.maxIteration; iter++ { | ||
// customed steepest descending dir | ||
steepestDescDir := grad.Copy() | ||
m.updateGrad(pos, steepestDescDir) | ||
steepestDescDir.ApplyScale(-1.0) | ||
dir := steepestDescDir.Copy() | ||
// quasi-newton dir | ||
helper.ApplyQuasiInverseHession(dir) | ||
m.fixDirSign(dir, steepestDescDir) | ||
// customed grad for the new position | ||
potentialGrad := grad.Copy() | ||
m.updateGradForNewPos(pos, potentialGrad, dir) | ||
newCost, newPos := helper.BackTrackingLineSearch(cost, pos, potentialGrad, dir, iter==1) | ||
if cost == newCost { | ||
break | ||
} | ||
cost = newCost | ||
pos = newPos | ||
terminalCriterion.addCost(cost) | ||
fmt.Printf("%d\t%e\t%e\n", iter, newCost, terminalCriterion.improvement) | ||
if terminalCriterion.isTerminable() { | ||
break | ||
} | ||
grad = costfun.Gradient(pos).Copy() | ||
if helper.UpdateState(pos, grad) { | ||
break | ||
} | ||
} | ||
return pos | ||
} | ||
|
||
func (m *OWLQNMinimizer) updateGradForNewPos(x *Vector, grad *Vector, dir *Vector) { | ||
if m.l1reg == 0 { | ||
return | ||
} | ||
for key, val := range grad.data { | ||
xval := x.GetValue(key) | ||
if xval < 0 { | ||
grad.SetValue(key, val - m.l1reg) | ||
} else if xval > 0 { | ||
grad.SetValue(key, val + m.l1reg) | ||
} else { | ||
dirval := dir.GetValue(key) | ||
if dirval < 0 { | ||
grad.SetValue(key, val - m.l1reg) | ||
} else if dirval > 0 { | ||
grad.SetValue(key, val + m.l1reg) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (m *OWLQNMinimizer) updateGrad(x *Vector, grad *Vector) { | ||
if m.l1reg == 0 { | ||
return | ||
} | ||
for key, val := range grad.data { | ||
xval := x.GetValue(key) | ||
if xval < 0 { | ||
grad.SetValue(key, val - m.l1reg) | ||
} else if xval > 0 { | ||
grad.SetValue(key, val + m.l1reg) | ||
} else { | ||
if val < -m.l1reg { | ||
grad.SetValue(key, val + m.l1reg) | ||
} else if val > m.l1reg { | ||
grad.SetValue(key, val - m.l1reg) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (m *OWLQNMinimizer) fixDirSign(dir *Vector, steepestDescDir *Vector) { | ||
if m.l1reg == 0 { | ||
return | ||
} | ||
for key, val := range dir.data { | ||
if val * steepestDescDir.GetValue(key) <= 0 { | ||
dir.SetValue(key, 0) | ||
} | ||
} | ||
} | ||
|
||
func (m *OWLQNMinimizer) Evaluate(pos *Vector) float64 { | ||
cost := m.costFun.Value(pos) | ||
for _, val := range pos.data { | ||
cost += math.Abs(val) * m.l1reg | ||
} | ||
return cost | ||
} | ||
|
||
func (m *OWLQNMinimizer) NextPoint(curPos *Vector, dir *Vector, alpha float64) *Vector { | ||
newPos := curPos.ElemWiseMultiplyAdd(dir, alpha) | ||
if m.l1reg > 0 { | ||
for key, val := range curPos.data { | ||
if val * newPos.GetValue(key) < 0 { | ||
newPos.SetValue(key, 0) | ||
} | ||
} | ||
} | ||
return newPos | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package hector | ||
|
||
import ("math") | ||
|
||
/** | ||
* It's based the paper "Scalable Training of L1-Regularized Log-Linear Models" | ||
* by Galen Andrew and Jianfeng Gao | ||
* user: weixuan | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
type relativeMeanImprCriterion struct { | ||
minHist int | ||
maxHist int | ||
tolerance float64 | ||
improvement float64 | ||
costList []float64 | ||
} | ||
|
||
func NewRelativeMeanImprCriterion(tolerance float64) *relativeMeanImprCriterion { | ||
tc := new(relativeMeanImprCriterion) | ||
tc.minHist = 5 | ||
tc.maxHist = 10 | ||
tc.costList = make([]float64, 0, tc.maxHist) | ||
tc.tolerance = tolerance | ||
return tc | ||
} | ||
|
||
func (tc *relativeMeanImprCriterion) calImprovement() float64{ | ||
sz := len(tc.costList) | ||
if sz <= tc.minHist { | ||
return math.MaxFloat32 | ||
} | ||
first := tc.costList[0] | ||
last := tc.costList[sz-1] | ||
impr := (first - last) /float64(sz-1) | ||
if last != 0 { | ||
impr = math.Abs(impr / last) | ||
} else if first != 0 { | ||
impr = math.Abs(impr / first) | ||
} else { | ||
impr = 0 | ||
} | ||
if sz > tc.maxHist { | ||
tc.costList = tc.costList[1:] | ||
} | ||
return impr | ||
} | ||
|
||
func (tc *relativeMeanImprCriterion) addCost(latestCost float64) { | ||
tc.costList = append(tc.costList, latestCost) | ||
tc.improvement = tc.calImprovement() | ||
} | ||
|
||
func (tc *relativeMeanImprCriterion) isTerminable() bool { | ||
return tc.improvement <= tc.tolerance | ||
} |