<s> """<STR_LIT>""" <EOL> import signal <EOL> import logging <EOL> import traceback <EOL> from blocks . config import config <EOL> from blocks . log import BACKENDS <EOL> from blocks . utils import reraise_as , unpack , change_recursion_limit <EOL> from blocks . utils . profile import Profile , Timer <EOL> from blocks . algorithms import DifferentiableCostMinimizer <EOL> from blocks . extensions import CallbackName <EOL> from blocks . model import Model <EOL> logger = logging . getLogger ( __name__ ) <EOL> error_message = """<STR_LIT>""" <EOL> error_in_error_handling_message = """<STR_LIT>""" <EOL> epoch_interrupt_message = """<STR_LIT>""" <EOL> batch_interrupt_message = """<STR_LIT>""" <EOL> no_model_message = """<STR_LIT>""" <EOL> class MainLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , algorithm , data_stream , model = None , log = None , <EOL> log_backend = None , extensions = None ) : <EOL> if log is None : <EOL> if log_backend is None : <EOL> log_backend = config . log_backend <EOL> log = BACKENDS [ log_backend ] ( ) <EOL> if extensions is None : <EOL> extensions = [ ] <EOL> self . data_stream = data_stream <EOL> self . algorithm = algorithm <EOL> self . log = log <EOL> self . extensions = extensions <EOL> self . profile = Profile ( ) <EOL> self . _model = model <EOL> self . status [ '<STR_LIT>' ] = False <EOL> self . status [ '<STR_LIT>' ] = False <EOL> self . status [ '<STR_LIT>' ] = False <EOL> self . status [ '<STR_LIT>' ] = False <EOL> @ property <EOL> def model ( self ) : <EOL> if not self . _model : <EOL> raise AttributeError ( "<STR_LIT>" + <EOL> no_model_message ) <EOL> return self . _model <EOL> @ property <EOL> def iteration_state ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . data_stream , self . epoch_iterator ) <EOL> @ iteration_state . setter <EOL> def iteration_state ( self , value ) : <EOL> ( self . data_stream , self . epoch_iterator ) = value <EOL> @ property <EOL> def status ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . log . status <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( ) <EOL> self . profile . current = [ ] <EOL> if ( self . _model and isinstance ( self . _model , Model ) and <EOL> isinstance ( self . algorithm , DifferentiableCostMinimizer ) ) : <EOL> if not ( set ( self . _model . get_parameter_dict ( ) . values ( ) ) == <EOL> set ( self . algorithm . parameters ) ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> with change_recursion_limit ( config . recursion_limit ) : <EOL> self . original_sigint_handler = signal . signal ( <EOL> signal . SIGINT , self . _handle_epoch_interrupt ) <EOL> self . original_sigterm_handler = signal . signal ( <EOL> signal . SIGTERM , self . _handle_batch_interrupt ) <EOL> try : <EOL> logger . info ( "<STR_LIT>" ) <EOL> if not self . status [ '<STR_LIT>' ] : <EOL> for extension in self . extensions : <EOL> extension . main_loop = self <EOL> self . _run_extensions ( '<STR_LIT>' ) <EOL> with Timer ( '<STR_LIT>' , self . profile ) : <EOL> self . algorithm . initialize ( ) <EOL> self . status [ '<STR_LIT>' ] = True <EOL> if self . log . status [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> self . log . resume ( ) <EOL> self . _run_extensions ( '<STR_LIT>' ) <EOL> self . status [ '<STR_LIT>' ] = False <EOL> self . status [ '<STR_LIT>' ] = False <EOL> with Timer ( '<STR_LIT>' , self . profile ) : <EOL> while self . _run_epoch ( ) : <EOL> pass <EOL> except TrainingFinish : <EOL> self . log . current_row [ '<STR_LIT>' ] = True <EOL> except Exception as e : <EOL> self . _restore_signal_handlers ( ) <EOL> self . log . current_row [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . error ( "<STR_LIT>" + error_message ) <EOL> try : <EOL> self . _run_extensions ( '<STR_LIT>' ) <EOL> except Exception : <EOL> logger . error ( traceback . format_exc ( ) ) <EOL> logger . error ( "<STR_LIT>" + <EOL> error_in_error_handling_message ) <EOL> reraise_as ( e ) <EOL> finally : <EOL> self . _restore_signal_handlers ( ) <EOL> if self . log . current_row . get ( '<STR_LIT>' , False ) : <EOL> self . _run_extensions ( '<STR_LIT>' ) <EOL> if config . profile : <EOL> self . profile . report ( ) <EOL> def find_extension ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return unpack ( [ extension for extension in self . extensions <EOL> if extension . name == name ] , singleton = True ) <EOL> def _run_epoch ( self ) : <EOL> if not self . status . get ( '<STR_LIT>' , False ) : <EOL> try : <EOL> self . log . status [ '<STR_LIT>' ] = False <EOL> self . epoch_iterator = ( self . data_stream . <EOL> get_epoch_iterator ( as_dict = True ) ) <EOL> except StopIteration : <EOL> return False <EOL> self . status [ '<STR_LIT>' ] = True <EOL> self . _run_extensions ( '<STR_LIT>' ) <EOL> with Timer ( '<STR_LIT>' , self . profile ) : <EOL> while self . _run_iteration ( ) : <EOL> pass <EOL> self . status [ '<STR_LIT>' ] = False <EOL> self . status [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> self . status [ '<STR_LIT>' ] += [ self . status [ '<STR_LIT>' ] ] <EOL> self . _run_extensions ( '<STR_LIT>' ) <EOL> self . _check_finish_training ( '<STR_LIT>' ) <EOL> return True <EOL> def _run_iteration ( self ) : <EOL> try : <EOL> with Timer ( '<STR_LIT>' , self . profile ) : <EOL> batch = next ( self . epoch_iterator ) <EOL> except StopIteration : <EOL> if not self . log . status [ '<STR_LIT>' ] : <EOL> reraise_as ( ValueError ( "<STR_LIT>" ) ) <EOL> return False <EOL> self . log . status [ '<STR_LIT>' ] = True <EOL> self . _run_extensions ( '<STR_LIT>' , batch ) <EOL> with Timer ( '<STR_LIT:train>' , self . profile ) : <EOL> self . algorithm . process_batch ( batch ) <EOL> self . status [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> self . _run_extensions ( '<STR_LIT>' , batch ) <EOL> self . _check_finish_training ( '<STR_LIT>' ) <EOL> return True <EOL> def _run_extensions ( self , method_name , * args ) : <EOL> with Timer ( method_name , self . profile ) : <EOL> for extension in self . extensions : <EOL> with Timer ( type ( extension ) . __name__ , self . profile ) : <EOL> extension . dispatch ( CallbackName ( method_name ) , * args ) <EOL> def _check_finish_training ( self , level ) : <EOL> """<STR_LIT>""" <EOL> if ( self . log . current_row . get ( '<STR_LIT>' , False ) or <EOL> self . status . get ( '<STR_LIT>' , False ) ) : <EOL> raise TrainingFinish <EOL> if ( level == '<STR_LIT>' and <EOL> self . status . get ( '<STR_LIT>' , False ) ) : <EOL> raise TrainingFinish <EOL> def _handle_epoch_interrupt ( self , signal_number , frame ) : <EOL> logger . warning ( '<STR_LIT>' + <EOL> epoch_interrupt_message ) <EOL> signal . signal ( signal . SIGINT , self . _handle_batch_interrupt ) <EOL> self . log . current_row [ '<STR_LIT>' ] = True <EOL> self . status [ '<STR_LIT>' ] = True <EOL> def _handle_batch_interrupt ( self , signal_number , frame ) : <EOL> self . _restore_signal_handlers ( ) <EOL> logger . warning ( '<STR_LIT>' + <EOL> batch_interrupt_message ) <EOL> self . log . current_row [ '<STR_LIT>' ] = True <EOL> self . status [ '<STR_LIT>' ] = True <EOL> def _restore_signal_handlers ( self ) : <EOL> signal . signal ( signal . SIGINT , self . original_sigint_handler ) <EOL> signal . signal ( signal . SIGTERM , self . original_sigterm_handler ) <EOL> class TrainingFinish ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
