/
ZooTrigger.scala
165 lines (152 loc) · 4.6 KB
/
ZooTrigger.scala
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
/*
* Copyright 2016 The BigDL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intel.analytics.bigdl.dllib.optim
import com.intel.analytics.bigdl.dllib.utils._
/**
* A trigger specifies a timespot or several timespots during training,
* and a corresponding action will be taken when the timespot(s)
* is reached.
*/
trait ZooTrigger extends Trigger {
protected var zooState: Table = T()
/**
* We also hold some training metrics to control trigger.
* @param zooState zoo state table
*/
private[bigdl] def setZooState(zooState: Table): Unit = {
this.zooState = zooState
}
}
/**
* A trigger that triggers an action when each epoch finishs.
* Could be used as trigger in setValidation and setCheckpoint
* in Optimizer, and also in TrainSummary.setSummaryTrigger.
*/
case class EveryEpoch() extends ZooTrigger{
private var lastEpoch = -1
override def apply(state: Table): Boolean = {
if (lastEpoch == -1) {
lastEpoch = state[Int]("epoch")
false
} else {
if (state[Int]("epoch") <= lastEpoch) {
false
} else {
if (zooState.contains("numSlice") && zooState.contains("currentSlice")) {
if (zooState[Int]("currentSlice") % zooState[Int]("numSlice") == 0) {
lastEpoch = state[Int]("epoch")
true
} else {
false
}
} else {
lastEpoch = state[Int]("epoch")
true
}
}
}
}
}
/**
* A trigger that triggers an action every "n" iterations.
* Could be used as trigger in setValidation and setCheckpoint
* in Optimizer, and also in TrainSummary.setSummaryTrigger.
*
* @param interval - trigger interval "n"
*/
case class SeveralIteration(interval: Int) extends ZooTrigger{
override def apply(state: Table): Boolean = {
val curIteration = state[Int]("neval") - 1
curIteration != 0 && curIteration % interval == 0
}
}
/**
* A trigger that triggers an action when training reaches
* the number of epochs specified by "max".
* Usually used in Optimizer.setEndWhen.
*
* @param max the epoch when the action takes place
*/
case class MaxEpoch(max: Int) extends ZooTrigger{
override def apply(state: Table): Boolean = {
state[Int]("epoch") > max
}
}
/**
* A trigger that triggers an action when training reaches
* the number of iterations specified by "max".
* Usually used in Optimizer.setEndWhen.
*
* @param max the iteration when the action takes place
*
*/
case class MaxIteration(max: Int) extends ZooTrigger {
override def apply(state: Table): Boolean = {
state[Int]("neval") > max
}
}
/**
* A trigger that triggers an action when validation score larger than "max" score
* @param max max score
*/
case class MaxScore(max: Float) extends ZooTrigger {
override def apply(state: Table): Boolean = {
state[Float]("score") > max
}
}
/**
* A trigger that triggers an action when training loss less than "min" loss
* @param min min loss
*/
case class MinLoss(min: Float) extends ZooTrigger {
override def apply(state: Table): Boolean = {
state[Float]("Loss") < min
}
}
/**
* A trigger contains other triggers and triggers when all of them trigger (logical AND)
* @param first first trigger
* @param others others triggers
*/
case class And(first : ZooTrigger, others : ZooTrigger*) extends ZooTrigger {
override def setZooState(zooState: Table): Unit = {
super.setZooState(zooState)
first.setZooState(zooState)
others.foreach{zt =>
zt.setZooState(zooState)
}
}
override def apply(state: Table): Boolean = {
first.apply(state) && others.forall(_.apply(state))
}
}
/**
* A trigger contains other triggers and triggers when any of them trigger (logical OR)
* @param first first trigger
* @param others others triggers
*/
case class Or(first : ZooTrigger, others : ZooTrigger*) extends ZooTrigger {
override def setZooState(zooState: Table): Unit = {
super.setZooState(zooState)
first.setZooState(zooState)
others.foreach{zt =>
zt.setZooState(zooState)
}
}
override def apply(state: Table): Boolean = {
first.apply(state) || others.exists(_.apply(state))
}
}