'Run the main training loop.'

def main_loop(self):

    with self.sess.as_default():
        self._global_step = get_global_step_value()
        try:
            self._callbacks.before_train()
            self._global_step = get_global_step_value()
            for self._epoch_num in range(self.config.starting_epoch, (self.config.max_epoch + 1)):
                logger.info('Start    Epoch    {}    ...'.format(self._epoch_num))
                start_time = time.time()
                self._callbacks.before_epoch()
                for self.local_step in range(self.config.steps_per_epoch):
                    if self.hooked_sess.should_stop():
                        return
                    self.run_step()
                    self._callbacks.trigger_step()
                self._callbacks.after_epoch()
                logger.info('Epoch    {}    (global_step    {})    finished,    time:{:.2f}    sec.'.format(self._epoch_num, self.global_step, (time.time() - start_time)))
                self._callbacks.trigger_epoch()
            logger.info('Training    has    finished!')
        except (StopTraining, tf.errors.OutOfRangeError):
            logger.info('Training    was    stopped.')
        except KeyboardInterrupt:
            logger.info('Detected    Ctrl-C    and    exiting    main    loop.')
        except:
            raise
        finally:
            self._callbacks.after_train()
            self.hooked_sess.close()
