forked from taskgraph/taskgraph
/
stop_criteria.go
65 lines (52 loc) · 1.63 KB
/
stop_criteria.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
package op
import (
"time"
)
// This stop criteria stop after some fix iterations.
type fixCountStopCriteria struct {
maxIter, curIter int
}
func (f *fixCountStopCriteria) Done(param Parameter, value float32, gradient Parameter) bool {
f.curIter += 1
return f.curIter >= f.maxIter
}
func MakeFixCountStopCriteria(iter int) StopCriteria {
return &fixCountStopCriteria{maxIter: iter, curIter: 0}
}
// This criteria stop the iteration when the norm of gradient below some predefined threshold
type gradNormStopCriteria struct {
grad_norm_thres float32
}
func (g *gradNormStopCriteria) Done(param Parameter, value float32, gradient Parameter) bool {
norm := Sum(gradient, func(x float32) float32 { return x * x })
return norm < g.grad_norm_thres
}
func MakeGradientNormStopCriteria(thres float32) StopCriteria {
return &gradNormStopCriteria{grad_norm_thres: thres}
}
// Criterion as checking timeout
type timeoutCriterion struct {
start time.Time
limit time.Duration
}
func (c *timeoutCriterion) Done(param Parameter, value float32, gradient Parameter) bool {
return time.Now().Sub(c.start) > c.limit
}
func MakeTimeoutCriterion(limit time.Duration) StopCriteria {
return &timeoutCriterion{start: time.Now(), limit: limit}
}
// Criterion as a combination of criteria
type composedCriterion struct {
criterion []StopCriteria
}
func (c *composedCriterion) Done(param Parameter, value float32, gradient Parameter) bool {
for _, ic := range c.criterion {
if ic.Done(param, value, gradient) {
return true
}
}
return false
}
func MakeComposedCriterion(criteria ...StopCriteria) StopCriteria {
return &composedCriterion{criterion: criteria}
}