Belle II Software  release-06-01-15
tensorflow_dnn_model.py
1 #!/usr/bin/env python3
2 
3 
10 
11 
12 import os
13 import sys
14 import time
15 import numpy
16 import pickle
17 
18 import tensorflow as tf
19 
20 
21 class Layer:
22  """
23  definition of a layer obj
24  """
25 
26  def __init__(self, name, tf_activation_str, dim_input, dim_output, p_bias, p_w,
27  operation_seed=None):
28  """
29  :param name:
30  :param tf_activation: string, name of an available tensorflow activations function
31  :param dim_input:
32  :param dim_output:
33  :param p_bias: initial constant
34  :param p_w: stddev of uniform distribution to initialize
35  :return:
36  """
37 
38  self.namename = name
39 
40  tf_activation_dict = {
41  'tanh': tf.tanh,
42  'sigmoid': tf.sigmoid,
43  'relu': tf.nn.relu,
44  'leaky_relu': tf.nn.leaky_relu,
45  }
46 
47  if tf_activation_str not in tf_activation_dict:
48  raise ValueError
49 
50 
51  self.tf_activationtf_activation = tf_activation_dict[tf_activation_str]
52 
53 
54  self.seedseed = None
55 
56 
57  self.shapeshape = [dim_input, dim_output]
58 
59 
60  self.ww = self._init_weight_init_weight(self.shapeshape, p_w, operation_seed)
61 
62 
63  self.bb = self._init_bias_init_bias(self.shapeshape[1], p_bias)
64 
65 
66  self.inputinput = None
67 
68 
69  self.outputoutput = None
70 
71 
72  self.is_initializedis_initialized = False
73 
74 
75  self._add_all_to_summary_add_all_to_summary()
76 
77  def _init_bias(self, width, init_val, name=None):
78  """
79  define bias variables
80  """
81  if name is None:
82  name = self.namename + '_b'
83  initial = tf.constant(init_val, shape=[width], name=name)
84  return tf.Variable(initial, name=name)
85 
86  def _init_weight(self, shape, stddev, operation_seed, name=None):
87  """
88  define weight variables
89  """
90  if name is None:
91  name = self.namename + '_w'
92  initial = tf.truncated_normal(shape, stddev=stddev, seed=operation_seed, name=name)
93  return tf.Variable(initial, name=name)
94 
95  def initialize(self, layer_input):
96  """
97  initialization, requires layer input
98  """
99  if self.is_initializedis_initialized:
100  raise RuntimeError('Layer %s is already initialized.' % self.namename)
101 
102  self.inputinput = layer_input
103  name = self.namename + '_output'
104  with tf.name_scope('output'):
105  self.outputoutput = self.tf_activationtf_activation(tf.matmul(layer_input, self.ww) + self.bb, name)
106  tf.add_to_collection('output', self.outputoutput)
107 
108  # set initialized status
109  self.is_initializedis_initialized = True
110 
111  def _add_var_to_summary(self, var):
112  """
113  add variables of this layer to tensorboard
114  :param var: tensorflow variable
115  :return:
116  """
117  with tf.name_scope('summaries'):
118  mean = tf.reduce_mean(var)
119  tf.summary.scalar('mean', mean)
120 
121  with tf.name_scope('stddev'):
122  stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
123 
124  tf.summary.scalar('stddev', stddev)
125  tf.summary.scalar('max', tf.reduce_max(var))
126  tf.summary.scalar('min', tf.reduce_min(var))
127 
128  tf.summary.histogram('histogram', var)
129 
131  """
132  adds tuneable parameters to summary
133  """
134  with tf.name_scope(self.namename):
135  with tf.name_scope('weights'):
136  self._add_var_to_summary_add_var_to_summary(self.ww)
137  with tf.name_scope('biases'):
138  self._add_var_to_summary_add_var_to_summary(self.bb)
139 
140 
142  """
143  class combine layer obj
144  """
145 
146  def __init__(self, layers):
147  """
148  initialization
149  """
150 
151 
152  self.layerslayers = layers
153 
154 
155  self.inputinput = None
156 
157 
158  self.outputoutput = None
159 
160 
161  self.ww = None
162 
163 
164  self.bb = None
165 
166 
167  self.is_initializedis_initialized = False
168 
169  @classmethod
170  def from_list(cls, layers):
171  """
172  define layers from list
173  """
174  layer_obj = []
175  for layer in layers:
176  layer_obj.append(Layer(*layer))
177 
178  mlp = cls(layer_obj)
179  return mlp
180 
182  """
183  collect tunable parameters
184  """
185  self.ww = []
186  self.bb = []
187  for layer in self.layerslayers:
188  self.ww.append(layer.w)
189  self.bb.append(layer.b)
190 
191  def _connect_layers(self, layer_input):
192  """
193  connect layers
194  """
195  for layer in self.layerslayers:
196  layer.initialize(layer_input)
197  layer_input = layer.output
198 
199  def initialize(self, layer_input):
200  """
201  initialize
202  """
203  if self.is_initializedis_initialized:
204  raise RuntimeError
205 
206  self.is_initializedis_initialized = True
207  # check shape
208  for _idx in range(len(self.layerslayers) - 1):
209  assert self.layerslayers[_idx].shape[1] == self.layerslayers[_idx + 1].shape[0]
210 
211  self._connect_layers_connect_layers(layer_input)
212  self._set_weights_and_biases_set_weights_and_biases()
213 
214  self.inputinput = self.layerslayers[0].input
215  self.outputoutput = self.layerslayers[-1].output
216 
217 
219  """
220  define the default model
221  """
222 
223  def __init__(self, mlp, mom_init=.9, mom_max=.99, mom_epochs=200, lr_init=.05, lr_min=1e-6,
224  lr_dec_rate=.976,
225  stop_epochs=10, min_epochs=200, max_epochs=1000, wd_coeffs=None, change_optimizer=None,
226  staircase=True, smooth_cross_entropy=False):
227  """
228  initialization function
229  """
230 
231  if wd_coeffs is not None:
232  assert len(wd_coeffs) == len(mlp.layers)
233 
234 
235  self.wd_coeffswd_coeffs = wd_coeffs
236 
237 
238  self.mlpmlp = mlp
239 
240 
241  self.outputoutput = None
242 
243  with tf.name_scope('global_step'):
244 
245  self.global_stepglobal_step = tf.Variable(0, trainable=False)
246 
247  # optimizer params
248 
249  self.c_mom_initc_mom_init = mom_init
250 
251  self.c_mom_maxc_mom_max = mom_max
252 
253  self.c_mom_epochsc_mom_epochs = mom_epochs
254 
255 
256  self.c_lr_initc_lr_init = lr_init
257 
258  self.c_lr_minc_lr_min = lr_min
259 
260  self.c_lr_dec_ratec_lr_dec_rate = lr_dec_rate
261 
262 
263  self.c_stop_epochsc_stop_epochs = stop_epochs
264 
265 
266  self.c_staircasec_staircase = staircase
267 
268 
269  self.batches_per_epochbatches_per_epoch = None
270 
271 
272  self.optimizersoptimizers = []
273  # list with epochs in which optimizers will be changed, if None is given, only the default optimizer will be
274 
275  self.optimizer_change_epochsoptimizer_change_epochs = change_optimizer
276  if change_optimizer is not None:
277  self.optimizer_change_epochsoptimizer_change_epochs.insert(0, 0)
278  self.optimizer_change_epochsoptimizer_change_epochs.append(sys.maxsize)
279 
280 
281  self.monitoring_paramsmonitoring_params = None
282 
283  self.monitoring_labelsmonitoring_labels = None
284 
285 
286  self.mon_dictmon_dict = dict()
287 
288 
289  self.xx = None
290 
291  self.y_y_ = None
292 
293  self.weightsweights = None
294 
295 
296  self.max_epochsmax_epochs = max_epochs
297 
298  # termination criterion
299 
300  self.min_epochsmin_epochs = min_epochs
301 
302 
303  self.termination_criteriontermination_criterion = None
304 
305  self.recent_paramsrecent_params = []
306 
307  self.best_valuebest_value = numpy.inf
308 
309 
310  self.step_countdownstep_countdown = self.c_stop_epochsc_stop_epochs
311 
312 
313  self.smooth_cross_entropysmooth_cross_entropy = smooth_cross_entropy
314 
315 
316  self.is_initializedis_initialized = False
317 
318  def _set_optimizer(self):
319  """
320  set optimizer
321  """
322 
323  t_learning_rate = tf.train.exponential_decay(self.c_lr_initc_lr_init, self.global_stepglobal_step, self.batches_per_epochbatches_per_epoch,
324  self.c_lr_dec_ratec_lr_dec_rate, staircase=self.c_staircasec_staircase)
325 
326  t_lr_min = tf.Variable(self.c_lr_minc_lr_min, trainable=False)
327 
328  with tf.name_scope('learning_rate'):
329  t_limited_lr = tf.maximum(t_learning_rate, t_lr_min)
330  tf.summary.scalar('learning rate', t_limited_lr)
331 
332  # linear increasing momentum
333  c_mom_dec_rate = (self.c_mom_maxc_mom_max - self.c_mom_initc_mom_init) / self.c_mom_epochsc_mom_epochs
334 
335  t_mom_dec_rate = tf.Variable(c_mom_dec_rate, trainable=False, dtype=tf.float32)
336 
337  t_momentum = tf.Variable(self.c_mom_initc_mom_init, trainable=False, dtype=tf.float32)
338 
339  t_mom_max = tf.Variable(self.c_mom_maxc_mom_max, trainable=False, dtype=tf.float32)
340 
341  t_batches_per_epoch = tf.Variable(self.batches_per_epochbatches_per_epoch, trainable=False, dtype=tf.float32)
342 
343  with tf.name_scope('momentum'):
344  # tf.floor -- 'staircase = true'
345  global_step = tf.cast(self.global_stepglobal_step, tf.float32)
346  if self.c_staircasec_staircase:
347  t_limited_mom = tf.minimum(t_momentum + t_mom_dec_rate *
348  tf.floor(global_step / t_batches_per_epoch), t_mom_max)
349  else:
350  t_limited_mom = tf.minimum(t_momentum + t_mom_dec_rate *
351  (global_step / t_batches_per_epoch), t_mom_max)
352  tf.summary.scalar('momentum', t_limited_mom)
353 
354  self.mon_dictmon_dict['learning_rate'] = t_limited_lr
355  self.mon_dictmon_dict['momentum'] = t_limited_mom
356 
357  with tf.name_scope('optimizer'):
358  self.optimizersoptimizers.append(tf.train.MomentumOptimizer(t_limited_lr, t_limited_mom))
359  self.optimizersoptimizers.append(tf.train.GradientDescentOptimizer(t_limited_lr))
360 
361  def _set_loss(self):
362  """
363  set loss
364  """
365 
366  _y = self.mlpmlp.output
367  _y_ = self.y_y_
368 
369  epsilon = 1e-10
370  t_epsilon = tf.constant(epsilon)
371 
372  # sum over batch
373  with tf.name_scope('cross_entropy'):
374  if self.smooth_cross_entropysmooth_cross_entropy:
375  cross_entropy = -tf.reduce_mean(tf.reduce_sum((_y_ * tf.log(_y + t_epsilon) + (1 - _y_) *
376  tf.log(1 - _y + t_epsilon)), 1))
377  else:
378  cross_entropy = -tf.reduce_mean(tf.reduce_sum((_y_ * tf.log(tf.clip_by_value(_y, epsilon, 1))) +
379  ((1 - _y_) * tf.log(tf.clip_by_value((1 - _y),
380  epsilon, 1))), 1))
381  tf.summary.scalar('cross entropy', cross_entropy)
382 
383  loss = cross_entropy
384 
385  if self.wd_coeffswd_coeffs:
386  wd_coeffs = self.wd_coeffswd_coeffs
387  weights = self.mlpmlp.w
388 
389  wd = [tf.constant(coeff) * tf.nn.l2_loss(w) for coeff, w in zip(wd_coeffs, weights)]
390  loss += sum(wd)
391 
392  with tf.name_scope('loss_function'):
393 
394  self.lossloss = loss
395  tf.summary.scalar('loss', loss)
396 
398  """
399  sets monitoring params
400  """
401 
402  _y = self.mlpmlp.output
403  _y_ = self.y_y_
404 
405  epsilon = 1e-10
406  t_epsilon = tf.constant(epsilon)
407 
408  with tf.name_scope('mean_cross_entropy'):
409  if self.smooth_cross_entropysmooth_cross_entropy:
410  mean_cross_entropy = -tf.reduce_mean(tf.reduce_sum((_y_ * tf.log(_y + t_epsilon) + (1 - _y_) *
411  tf.log(1 - _y + t_epsilon)), 1))
412  else:
413  mean_cross_entropy = -tf.reduce_mean(tf.reduce_sum((_y_ * tf.log(tf.clip_by_value(_y, epsilon, 1))) +
414  ((1 - _y_) * tf.log(tf.clip_by_value((1 - _y),
415  epsilon, 1))), 1))
416  tf.summary.scalar('mean cross entropy', mean_cross_entropy)
417  loss = self.lossloss
418 
419  self.monitoring_labelsmonitoring_labels = ['mean_cross_entropy', 'loss']
420  self.monitoring_paramsmonitoring_params = [mean_cross_entropy, loss]
421 
422  def _default_termination_criterion(self, monitoring_params, epoch, label_name='mean_cross_entropy',
423  prop_dec=1e-5):
424  """
425  :param monitoring_params:
426  :param epoch:
427  :param label_name:
428  :param steps:
429  :param prop_dec:
430  :return:
431  """
432 
433  loss_idx = self.monitoring_labelsmonitoring_labels.index(label_name)
434  mon_param = monitoring_params[loss_idx]
435 
436  if epoch < self.min_epochsmin_epochs:
437  return False
438 
439  if mon_param < self.best_valuebest_value * (1. - prop_dec):
440  self.step_countdownstep_countdown = self.c_stop_epochsc_stop_epochs
441  self.best_valuebest_value = mon_param
442  else:
443  self.step_countdownstep_countdown -= 1
444 
445  if self.step_countdownstep_countdown > 0:
446  return False
447 
448  return True
449 
450  def get_minimizer(self, epoch=0):
451  """
452  get minimizer
453  """
454 
455  if self.optimizer_change_epochsoptimizer_change_epochs is None:
456  return self.optimizersoptimizers[0].minimize(self.lossloss, self.global_stepglobal_step)
457 
458  if len(self.optimizer_change_epochsoptimizer_change_epochs) > len(self.optimizersoptimizers) + 1:
459  raise RuntimeError
460 
461  # switch optimizer for given epoch
462  for i in range(1, len(self.optimizer_change_epochsoptimizer_change_epochs)):
463  if self.optimizer_change_epochsoptimizer_change_epochs[i - 1] <= epoch < self.optimizer_change_epochsoptimizer_change_epochs[i]:
464  return self.optimizersoptimizers[i - 1].minimize(self.lossloss, self.global_stepglobal_step)
465 
466  # needs to be called since parameters depends on number of batches
467  def initialize(self, data_set, input_placeholders=None):
468  """
469  initialized
470  """
471  if self.is_initializedis_initialized:
472  raise RuntimeError
473  self.is_initializedis_initialized = True
474 
475  self.batches_per_epochbatches_per_epoch = data_set.batches
476  # placeholders
477  if input_placeholders is None:
478  with tf.name_scope('input'):
479  self.xx = tf.placeholder(tf.float32, shape=[None, data_set.feature_number], name='x-input')
480  self.y_y_ = tf.placeholder(tf.float32, shape=[None, 1], name="y-input")
481  # TODO: implement weights
482  self.weightsweights = tf.placeholder(tf.float32, shape=[None, 1], name="weight-input")
483  else:
484  self.xx = input_placeholders[0]
485  self.y_y_ = input_placeholders[1]
486 
487  # layer input, calls cascade initialization
488  self.mlpmlp.initialize(self.xx)
489 
490  self._set_optimizer_set_optimizer()
491  self._set_loss_set_loss()
492  self._set_monitoring_params_set_monitoring_params()
493 
494  # termination criterion
495  self.termination_criteriontermination_criterion = self._default_termination_criterion_default_termination_criterion
496 
497 
498 class Trainer:
499  """
500  handling the training of the network model
501  """
502 
503  def __init__(self, model, data_set, sess, log_dir=None, save_name=None, monitoring_size=100000,
504  input_placeholders=None):
505  """
506  class to train a predefined model
507  :param model: DefaultModel obj
508  :param data_set: TFData obj
509  :param sess: tensorflow.Session obj
510  :param log_dir: str, directory name of tensorboard logging
511  :param save_name: str, path and name for saving the weightfiles
512  :param monitoring_size: int, number of events of training fraction used for monitoring
513  :param input_placeholders: list of tf.placeholders, [features, targets]
514  """
515 
516 
517  self._time_time = time.time()
518 
519  self.modelmodel = model
520 
521  self.data_setdata_set = data_set
522 
523  self.monitoring_sizemonitoring_size = monitoring_size
524 
525 
526  self.sesssess = sess
527 
528 
529  self.log_dirlog_dir = log_dir
530 
531  if input_placeholders is None:
532  with tf.name_scope('input'):
533 
534  self.xx = tf.placeholder(tf.float32, shape=[None, data_set.feature_number], name='x-input')
535 
536 
537  self.y_y_ = tf.placeholder(tf.float32, shape=[None, 1], name="y-input")
538  else:
539 
540  self.xx = input_placeholders[0]
541 
542  self.y_y_ = input_placeholders[1]
543 
544  self.modelmodel.initialize(data_set, input_placeholders=[self.xx, self.y_y_])
545 
546 
547  self.monitoring_paramsmonitoring_params = self.modelmodel.monitoring_params
548 
549  self.termination_criteriontermination_criterion = self.modelmodel.termination_criterion
550 
551 
552  self.max_epochsmax_epochs = self.modelmodel.max_epochs
553 
554 
555  self.current_epochcurrent_epoch = 0
556 
557 
558  self.minimizerminimizer = self.modelmodel.get_minimizer()
559 
560 
561  self.train_log_dicttrain_log_dict = {}
562  for label in self.modelmodel.monitoring_labels:
563  self.train_log_dicttrain_log_dict['train_' + label] = []
564  self.train_log_dicttrain_log_dict['valid_' + label] = []
565 
566  for label in self.modelmodel.mon_dict.keys():
567  self.train_log_dicttrain_log_dict[label] = []
568 
569  self._add_to_basf2_collections_add_to_basf2_collections()
570 
571  if log_dir is not None:
572  self._prepare_tensorboard_prepare_tensorboard(log_dir)
573 
574 
575  self.saversaver = tf.train.Saver()
576 
577  init_op = tf.global_variables_initializer()
578 
579  self.sesssess.run(init_op)
580 
581  if save_name is None:
582  time_str = time.strftime("%Y%m%d-%H%M%S")
583 
584  self.save_namesave_name = os.path.join(os.getcwd(), '_'.join([self.data_setdata_set.selection, time_str,
585  'model.ckpt']))
586 
587  else:
588 
589  self.save_namesave_name = save_name
590 
591  self._prepare_monitoring_prepare_monitoring()
592 
594  """
595  checking dataset sizes for evaluation
596  """
597 
598  self.train_monitortrain_monitor = -1
599 
600  self.valid_monitorvalid_monitor = -1
601  if self.data_setdata_set.train_events > self.monitoring_sizemonitoring_size:
602  self.train_monitortrain_monitor = self.monitoring_sizemonitoring_size
603 
604  if self.data_setdata_set.valid_events > self.monitoring_sizemonitoring_size:
605  self.valid_monitorvalid_monitor = self.monitoring_sizemonitoring_size
606 
607  def _prepare_tensorboard(self, log_dir):
608  """
609  prepare tensorboard
610  """
611  log_dir_train = os.path.join(log_dir, 'train')
612  log_dir_test = os.path.join(log_dir, 'test')
613 
614 
615  self.train_writertrain_writer = tf.summary.FileWriter(log_dir_train, self.sesssess.graph)
616 
617  self.test_writertest_writer = tf.summary.FileWriter(log_dir_test, self.sesssess.graph)
618 
619 
620  self.merged_summarymerged_summary = tf.summary.merge_all()
621 
623  """
624  add to basf2 collection
625  """
626  tf.add_to_collection('x', self.xx)
627  tf.add_to_collection('y', self.y_y_)
628  tf.add_to_collection('activation', self.modelmodel.mlp.output)
629  tf.add_to_collection('cost', self.modelmodel.loss)
630  tf.add_to_collection('optimizer', self.minimizerminimizer)
631 
632  def _save_best_state(self, monitoring_params, label_name='mean_cross_entropy'):
633  """
634  save model only if a global minimum is reached on validation set
635  :return:
636  """
637  # current last state
638  self.saversaver.save(self.sesssess, self.save_namesave_name.replace('.ckpt', '_current.ckpt'))
639 
640  # check for a not set best value
641  if self.modelmodel.best_value == numpy.inf:
642  return
643 
644  loss_idx = self.modelmodel.monitoring_labels.index(label_name)
645  mon_param = monitoring_params[loss_idx]
646 
647  if mon_param < self.modelmodel.best_value:
648  self.train_log_dicttrain_log_dict['best_epoch'] = self.current_epochcurrent_epoch
649  self.saversaver.save(self.sesssess, self.save_namesave_name)
650 
651  def _closing_ops(self):
652  """
653  closing ops
654  """
655  if self.log_dirlog_dir is not None:
656  self.train_writertrain_writer.close()
657  self.test_writertest_writer.close()
658  with open(os.path.join(self.log_dirlog_dir, 'training_params.pkl'), 'wb') as f:
659  pickle.dump(self.train_log_dicttrain_log_dict, f)
660 
661  # FIXME: raises an exception when calling session._del -> session.TF_DeleteStatus() !
662  # FIXME: not closing a session can cause memory leaks!
663  # self.sess.close()
664  # del self.sess
665 
666  def _train_epoch(self, current_epoch):
667  """
668  train epoch
669  """
670 
671  # set minimizer for this epoch
672  self.minimizerminimizer = self.modelmodel.get_minimizer(current_epoch)
673 
674  batch_iter = self.data_setdata_set.batch_iterator()
675 
676  for i in range(self.data_setdata_set.batches):
677  batch = next(batch_iter)
678  feed = {self.xx: batch[0], self.y_y_: batch[1]}
679  self.sesssess.run(self.minimizerminimizer, feed_dict=feed)
680  if i % int(.2 * self.data_setdata_set.batches) == 0:
681  print('Epoch status: %1.2f' % (i / self.data_setdata_set.batches))
682 
683  train_mon_dict = {self.xx: self.data_setdata_set.train_x[:self.train_monitortrain_monitor],
684  self.y_y_: self.data_setdata_set.train_y[:self.train_monitortrain_monitor]}
685 
686  valid_dict = {self.xx: self.data_setdata_set.valid_x[:self.valid_monitorvalid_monitor],
687  self.y_y_: self.data_setdata_set.valid_y[:self.valid_monitorvalid_monitor]}
688 
689  # depending on the fact, if the log should be monitored or not
690  if self.log_dirlog_dir is not None:
691  # todo raise exception if not empty
692 
693  summary = self.sesssess.run(self.merged_summarymerged_summary, feed_dict=train_mon_dict)
694  self.train_writertrain_writer.add_summary(summary, current_epoch)
695 
696 
697  self.epoch_parametersepoch_parameters = self.sesssess.run(self.monitoring_paramsmonitoring_params + [self.merged_summarymerged_summary],
698  feed_dict=valid_dict)
699  epoch_parameters_train = self.sesssess.run(
700  self.monitoring_paramsmonitoring_params + [self.merged_summarymerged_summary], feed_dict=train_mon_dict)
701 
702  # merged summary will be at last position
703  self.test_writertest_writer.add_summary(self.epoch_parametersepoch_parameters.pop(-1), current_epoch)
704  else:
705  self.epoch_parametersepoch_parameters = self.sesssess.run(self.monitoring_paramsmonitoring_params,
706  feed_dict=valid_dict)
707  epoch_parameters_train = self.sesssess.run(self.monitoring_paramsmonitoring_params, feed_dict=train_mon_dict)
708 
709  model_mon_params = []
710  for key, entry in self.modelmodel.mon_dict.items():
711  model_mon_params.append((key, self.sesssess.run(entry)))
712 
713  print('Current Epoch: %d' % self.current_epochcurrent_epoch)
714  for label, param in zip(self.modelmodel.monitoring_labels, self.epoch_parametersepoch_parameters):
715  self.train_log_dicttrain_log_dict['valid_' + label].append(param)
716  print(f"valid: {label}: {param:1.5f}")
717  for label, param in zip(self.modelmodel.monitoring_labels, epoch_parameters_train):
718  print(f"train: {label}: {param:1.5f}")
719  self.train_log_dicttrain_log_dict['train_' + label].append(param)
720 
721  for val in model_mon_params:
722  print("%s: %f" % val)
723  self.train_log_dicttrain_log_dict[val[0]].append(val[1])
724 
725  print('Epoch training time: %.1f' % (time.time() - self._time_time))
726  self._time_time = time.time()
727  print('\n')
728  self.current_epochcurrent_epoch += 1
729 
730  def train_model(self):
731  """
732  train model
733  """
734  for epoch in range(self.max_epochsmax_epochs):
735  self._train_epoch_train_epoch(epoch)
736 
737  self._save_best_state_save_best_state(self.epoch_parametersepoch_parameters)
738 
739  if self.termination_criteriontermination_criterion(self.epoch_parametersepoch_parameters, epoch):
740  break
741 
742  self._closing_ops_closing_ops()
best_value
the best value will be set a default start value, then updated with the termination criterion
def _default_termination_criterion(self, monitoring_params, epoch, label_name='mean_cross_entropy', prop_dec=1e-5)
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)
c_stop_epochs
number of epochs until stopping
batches_per_epoch
batches per epoch unknown.
def initialize(self, data_set, input_placeholders=None)
loss
cross entropy with weight decay
smooth_cross_entropy
True for a small epsilon addition, false for a clipped network output.
is_initialized
check if initialized (input and output are connected)
def _init_bias(self, width, init_val, name=None)
def __init__(self, name, tf_activation_str, dim_input, dim_output, p_bias, p_w, operation_seed=None)
def _init_weight(self, shape, stddev, operation_seed, name=None)
def initialize(self, layer_input)
w
init parameters for uniform distribution
def _save_best_state(self, monitoring_params, label_name='mean_cross_entropy')
monitoring_params
monitoring params for early stopping criterion, loss function, etc
max_epochs
global_training_parameters
def _train_epoch(self, current_epoch)
def __init__(self, model, data_set, sess, log_dir=None, save_name=None, monitoring_size=100000, input_placeholders=None)
termination_criterion
termination criterion