Belle II Software development
DefaultModel Class Reference
Inheritance diagram for DefaultModel:

Public Member Functions

def __init__ (self, mlp, mom_init=.9, mom_max=.99, mom_epochs=200, lr_init=.05, lr_min=1e-6, lr_dec_rate=.976, stop_epochs=10, min_epochs=200, max_epochs=1000, wd_coeffs=None, change_optimizer=None, staircase=True, smooth_cross_entropy=False)
 
def initialize (self, data_set)
 
def __call__ (self, x)
 
def get_optimizer (self, epoch=0)
 
def loss (self, predict_y, true_y)
 

Public Attributes

 mlp
 mlp net
 
 wd_coeffs
 weight decay coefficients
 
 global_step
 global step
 
 c_mom_init
 initial momentum
 
 c_mom_max
 maximum momentum
 
 c_mom_epochs
 momentum epochs
 
 c_mom_dec_rate
 momentum decay rate
 
 c_lr_init
 initial learning rate
 
 c_lr_min
 minimum learning rate
 
 c_lr_dec_rate
 learning rate decay rate
 
 c_stop_epochs
 number of epochs without improvement for early termination
 
 c_staircase
 use staircase
 
 batches_per_epoch
 batches per epoch unknown.
 
 optimizers
 define multiple optimizers
 
 optimizer_change_epochs
 used optimizers
 
 min_epochs
 min epochs
 
 max_epochs
 max epochs
 
 termination_criterion
 termination criterion
 
 recent_params
 recent params
 
 best_value
 the best value will be set a default start value, then updated with the termination criterion
 
 step_countdown
 step countdown
 
 smooth_cross_entropy
 True for a small epsilon addition, false for a clipped network output.
 
 is_initialized
 check if initialized
 

Protected Member Functions

def _default_termination_criterion (self, monitoring_param, epoch, prop_dec=1e-5)
 
def _get_learning_rate (self)
 
def _get_momentum (self)
 
def _set_optimizer (self)
 

Detailed Description

define the default model

Definition at line 198 of file tensorflow_dnn_model.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  mlp,
  mom_init = .9,
  mom_max = .99,
  mom_epochs = 200,
  lr_init = .05,
  lr_min = 1e-6,
  lr_dec_rate = .976,
  stop_epochs = 10,
  min_epochs = 200,
  max_epochs = 1000,
  wd_coeffs = None,
  change_optimizer = None,
  staircase = True,
  smooth_cross_entropy = False 
)
initialization function
:param mlp: network model.
:param mom_init: initial momentum
:param mom_max: maximum momentum
:param mom_epochs: momentum epochs
:param lr_init: initial learning rate
:param lr_min: minimum learning rate
:param lr_dec_rate: learning rate decay factor
:param stop_epochs: number of epochs without improvement required for early termination
:param min_epochs: minimum number of epochs for training
:param max_epochs: maximum number of epochs for training
:param wd_coeffs: weight decay coefficients. If not None must have one per mlp layer.
:param change_optimizer:
:param staircaise:
"param smooth_cross_entropy:

Definition at line 203 of file tensorflow_dnn_model.py.

216 smooth_cross_entropy=False):
217 """
218 initialization function
219 :param mlp: network model.
220 :param mom_init: initial momentum
221 :param mom_max: maximum momentum
222 :param mom_epochs: momentum epochs
223 :param lr_init: initial learning rate
224 :param lr_min: minimum learning rate
225 :param lr_dec_rate: learning rate decay factor
226 :param stop_epochs: number of epochs without improvement required for early termination
227 :param min_epochs: minimum number of epochs for training
228 :param max_epochs: maximum number of epochs for training
229 :param wd_coeffs: weight decay coefficients. If not None must have one per mlp layer.
230 :param change_optimizer:
231 :param staircaise:
232 "param smooth_cross_entropy:
233 """
234
235
236 self.mlp = mlp
237
238 if wd_coeffs is not None:
239 assert len(wd_coeffs) == len(mlp.layers)
240
241
242 self.wd_coeffs = wd_coeffs
243
244
245 self.global_step = tf.Variable(0, trainable=False, name='global_step', dtype=tf.int64)
246
247 # --optimizer params--
248
249 self.c_mom_init = tf.constant(mom_init, dtype=tf.float32)
250
251
252 self.c_mom_max = tf.constant(mom_max, dtype=tf.float32)
253
254
255 self.c_mom_epochs = tf.constant(mom_epochs, dtype=tf.float32)
256
257
258 self.c_mom_dec_rate = (self.c_mom_max - self.c_mom_init) / tf.cast(self.c_mom_epochs, tf.float32)
259
260
261 self.c_lr_init = tf.constant(lr_init, dtype=tf.float32)
262
263
264 self.c_lr_min = tf.constant(lr_min, dtype=tf.float32)
265
266
267 self.c_lr_dec_rate = tf.constant(lr_dec_rate, dtype=tf.float32)
268
269
270 self.c_stop_epochs = stop_epochs
271
272
273 self.c_staircase = staircase
274
275
276 self.batches_per_epoch = None
277
278
279 self.optimizers = []
280 # list with epochs in which optimizers will be changed, if None is given, only the default optimizer will be
281
282 self.optimizer_change_epochs = change_optimizer
283 if change_optimizer is not None:
284 self.optimizer_change_epochs.insert(0, 0)
285 self.optimizer_change_epochs.append(sys.maxsize)
286
287 # termination criterion
288
289 self.min_epochs = min_epochs
290
291
292 self.max_epochs = max_epochs
293
294
295 self.termination_criterion = None
296
297
298 self.recent_params = []
299
300
301 self.best_value = np.inf
302
303
304 self.step_countdown = self.c_stop_epochs
305
306
307 self.smooth_cross_entropy = smooth_cross_entropy
308
309
310 self.is_initialized = False
311
312 return
313

Member Function Documentation

◆ __call__()

def __call__ (   self,
  x 
)
Call the mlp

Definition at line 336 of file tensorflow_dnn_model.py.

336 def __call__(self, x):
337 """
338 Call the mlp
339 """
340 return self.mlp(x)
341

◆ _default_termination_criterion()

def _default_termination_criterion (   self,
  monitoring_param,
  epoch,
  prop_dec = 1e-5 
)
protected
early stopping criterion

:param monitoring_param: the parameter to monitor for early termination
:param epoch: the current epoch
:param prop_dec:
:return:

Definition at line 342 of file tensorflow_dnn_model.py.

345 prop_dec=1e-5):
346 """
347 early stopping criterion
348
349 :param monitoring_param: the parameter to monitor for early termination
350 :param epoch: the current epoch
351 :param prop_dec:
352 :return:
353 """
354 if epoch < self.min_epochs:
355 return False
356
357 if monitoring_param < self.best_value * (1. - prop_dec):
358 self.step_countdown = self.c_stop_epochs
359 self.best_value = monitoring_param
360 else:
361 self.step_countdown -= 1
362
363 if self.step_countdown > 0:
364 return False
365 return True
366

◆ _get_learning_rate()

def _get_learning_rate (   self)
protected
Returns the learning rate at the current global step.

Definition at line 367 of file tensorflow_dnn_model.py.

367 def _get_learning_rate(self):
368 """
369 Returns the learning rate at the current global step.
370 """
371 p = tf.cast(self.global_step, tf.float32) / tf.cast(self.batches_per_epoch, tf.float32)
372 if self.c_staircase:
373 p = tf.floor(p)
374 return tf.maximum(tf.multiply(self.c_lr_init, tf.pow(self.c_lr_dec_rate, p)), self.c_lr_min)
375

◆ _get_momentum()

def _get_momentum (   self)
protected
returns the momentum at the current global step.

Definition at line 376 of file tensorflow_dnn_model.py.

376 def _get_momentum(self):
377 """
378 returns the momentum at the current global step.
379 """
380 t_batches_per_epoch = tf.constant(self.batches_per_epoch, dtype=tf.float32)
381 global_step = tf.cast(self.global_step, tf.float32)
382
383 if self.c_staircase:
384 t_limited_mom = tf.minimum(self.c_mom_init + self.c_mom_dec_rate *
385 tf.floor(global_step / t_batches_per_epoch), self.c_mom_max)
386 else:
387 t_limited_mom = tf.minimum(self.c_mom_init + self.c_mom_dec_rate *
388 (global_step / t_batches_per_epoch), self.c_mom_max)
389 return t_limited_mom
390

◆ _set_optimizer()

def _set_optimizer (   self)
protected
set optimizers

Definition at line 391 of file tensorflow_dnn_model.py.

391 def _set_optimizer(self):
392 """
393 set optimizers
394 """
395 self.optimizers.append(tf.optimizers.SGD(learning_rate=self._get_learning_rate,
396 momentum=self._get_momentum))
397 self.optimizers.append(tf.optimizers.SGD(learning_rate=self._get_learning_rate))
398 return
399

◆ get_optimizer()

def get_optimizer (   self,
  epoch = 0 
)
get the optimizer. If multiple optimizers are booked gets the one appropriate for the epoch.

:param epoch: current epoch.

Definition at line 400 of file tensorflow_dnn_model.py.

400 def get_optimizer(self, epoch=0):
401 """
402 get the optimizer. If multiple optimizers are booked gets the one appropriate for the epoch.
403
404 :param epoch: current epoch.
405 """
406
407 if self.optimizer_change_epochs is None:
408 return self.optimizers[0]
409
410 if len(self.optimizer_change_epochs) > len(self.optimizers) + 1:
411 raise RuntimeError
412
413 # switch optimizer for given epoch
414 for i in range(1, len(self.optimizer_change_epochs)):
415 if self.optimizer_change_epochs[i - 1] <= epoch < self.optimizer_change_epochs[i]:
416 return self.optimizers[i - 1]
417

◆ initialize()

def initialize (   self,
  data_set 
)
Finalises initialization based of data_set specific information (number of batches per epoch)

Definition at line 316 of file tensorflow_dnn_model.py.

316 def initialize(self, data_set):
317 """
318 Finalises initialization based of data_set specific information (number of batches per epoch)
319 """
320 if self.is_initialized:
321 raise RuntimeError
322
323 self.batches_per_epoch = data_set.batches
324
325 # check layer dimensions align properly
326 if not self.mlp.is_initialized:
327 self.mlp.initialize()
328
329 self._set_optimizer()
330 self.termination_criterion = self._default_termination_criterion
331
332 self.is_initialized = True
333 return
334

◆ loss()

def loss (   self,
  predict_y,
  true_y 
)
calculate the loss

:param predict_y: predicted labels
:param true_y: true labels

Definition at line 418 of file tensorflow_dnn_model.py.

418 def loss(self, predict_y, true_y):
419 """
420 calculate the loss
421
422 :param predict_y: predicted labels
423 :param true_y: true labels
424 """
425
426 epsilon = 1e-10
427 t_epsilon = tf.constant(epsilon)
428
429 # sum over batch
430 with tf.name_scope('cross_entropy'):
431 if self.smooth_cross_entropy:
432 cross_entropy = -tf.reduce_mean(tf.reduce_sum((true_y * tf.math.log(predict_y + t_epsilon) + (1 - true_y) *
433 tf.math.log(1 - predict_y + t_epsilon)), 1))
434 else:
435 cross_entropy = -tf.reduce_mean(tf.reduce_sum((true_y * tf.math.log(tf.clip_by_value(predict_y, epsilon, 1))) +
436 ((1 - true_y) * tf.math.log(tf.clip_by_value((1 - predict_y),
437 epsilon, 1))), 1))
438 loss = cross_entropy
439
440 if self.wd_coeffs:
441 wd_coeffs = self.wd_coeffs
442 weights = self.mlp.w
443
444 wd = [tf.constant(coeff) * tf.nn.l2_loss(w) for coeff, w in zip(wd_coeffs, weights)]
445 loss += sum(wd)
446
447 return loss, cross_entropy
448
449

Member Data Documentation

◆ batches_per_epoch

batches_per_epoch

batches per epoch unknown.

needs to be set with initialize

Definition at line 276 of file tensorflow_dnn_model.py.

◆ best_value

best_value

the best value will be set a default start value, then updated with the termination criterion

Definition at line 301 of file tensorflow_dnn_model.py.

◆ c_lr_dec_rate

c_lr_dec_rate

learning rate decay rate

Definition at line 267 of file tensorflow_dnn_model.py.

◆ c_lr_init

c_lr_init

initial learning rate

Definition at line 261 of file tensorflow_dnn_model.py.

◆ c_lr_min

c_lr_min

minimum learning rate

Definition at line 264 of file tensorflow_dnn_model.py.

◆ c_mom_dec_rate

c_mom_dec_rate

momentum decay rate

Definition at line 258 of file tensorflow_dnn_model.py.

◆ c_mom_epochs

c_mom_epochs

momentum epochs

Definition at line 255 of file tensorflow_dnn_model.py.

◆ c_mom_init

c_mom_init

initial momentum

Definition at line 249 of file tensorflow_dnn_model.py.

◆ c_mom_max

c_mom_max

maximum momentum

Definition at line 252 of file tensorflow_dnn_model.py.

◆ c_staircase

c_staircase

use staircase

Definition at line 273 of file tensorflow_dnn_model.py.

◆ c_stop_epochs

c_stop_epochs

number of epochs without improvement for early termination

Definition at line 270 of file tensorflow_dnn_model.py.

◆ global_step

global_step

global step

Definition at line 245 of file tensorflow_dnn_model.py.

◆ is_initialized

is_initialized

check if initialized

Definition at line 310 of file tensorflow_dnn_model.py.

◆ max_epochs

max_epochs

max epochs

Definition at line 292 of file tensorflow_dnn_model.py.

◆ min_epochs

min_epochs

min epochs

Definition at line 289 of file tensorflow_dnn_model.py.

◆ mlp

mlp

mlp net

Definition at line 236 of file tensorflow_dnn_model.py.

◆ optimizer_change_epochs

optimizer_change_epochs

used optimizers

Definition at line 282 of file tensorflow_dnn_model.py.

◆ optimizers

optimizers

define multiple optimizers

Definition at line 279 of file tensorflow_dnn_model.py.

◆ recent_params

recent_params

recent params

Definition at line 298 of file tensorflow_dnn_model.py.

◆ smooth_cross_entropy

smooth_cross_entropy

True for a small epsilon addition, false for a clipped network output.

Definition at line 307 of file tensorflow_dnn_model.py.

◆ step_countdown

step_countdown

step countdown

Definition at line 304 of file tensorflow_dnn_model.py.

◆ termination_criterion

termination_criterion

termination criterion

Definition at line 295 of file tensorflow_dnn_model.py.

◆ wd_coeffs

wd_coeffs

weight decay coefficients

Definition at line 242 of file tensorflow_dnn_model.py.


The documentation for this class was generated from the following file: