<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> from datetime import datetime <EOL> import os <EOL> import sys <EOL> import logging <EOL> import warnings <EOL> from pylearn2 . utils import serial <EOL> from pylearn2 . utils . string_utils import preprocess <EOL> from pylearn2 . monitor import Monitor <EOL> from pylearn2 . space import NullSpace <EOL> from pylearn2 . utils . timing import log_timing , total_seconds <EOL> from pylearn2 . utils import sharedX <EOL> log = logging . getLogger ( __name__ ) <EOL> class Train ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , model , algorithm = None , save_path = None , <EOL> save_freq = <NUM_LIT:0> , extensions = None , allow_overwrite = True ) : <EOL> self . allow_overwrite = allow_overwrite <EOL> self . first_save = True <EOL> self . dataset = dataset <EOL> self . model = model <EOL> self . algorithm = algorithm <EOL> if save_path is not None : <EOL> if save_freq == <NUM_LIT:0> : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . save_path = preprocess ( save_path ) <EOL> else : <EOL> if save_freq > <NUM_LIT:0> : <EOL> phase_variable = '<STR_LIT>' <EOL> if phase_variable in os . environ : <EOL> phase = '<STR_LIT>' % os . environ [ phase_variable ] <EOL> tokens = [ os . environ [ '<STR_LIT>' ] , <EOL> phase , '<STR_LIT>' ] <EOL> else : <EOL> tokens = os . environ [ '<STR_LIT>' ] , '<STR_LIT>' <EOL> self . save_path = '<STR_LIT:.>' . join ( tokens ) <EOL> self . save_freq = save_freq <EOL> if hasattr ( self . dataset , '<STR_LIT>' ) : <EOL> self . model . dataset_yaml_src = self . dataset . yaml_src <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> self . extensions = extensions if extensions is not None else [ ] <EOL> self . training_seconds = sharedX ( value = <NUM_LIT:0> , <EOL> name = '<STR_LIT>' ) <EOL> self . total_seconds = sharedX ( value = <NUM_LIT:0> , name = '<STR_LIT>' ) <EOL> def setup_extensions ( self ) : <EOL> """<STR_LIT>""" <EOL> for ext in self . extensions : <EOL> ext . setup ( self . model , self . dataset , self . algorithm ) <EOL> def exceeded_time_budget ( self , t0 , time_budget ) : <EOL> """<STR_LIT>""" <EOL> dt = total_seconds ( datetime . now ( ) - t0 ) <EOL> if time_budget is not None and dt >= time_budget : <EOL> log . warning ( "<STR_LIT>" , <EOL> dt , time_budget ) <EOL> self . model . monitor . time_budget_exceeded = True <EOL> return True <EOL> else : <EOL> return False <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . model . monitor = Monitor . get_monitor ( self . model ) <EOL> self . model . monitor . time_budget_exceeded = False <EOL> if self . algorithm is not None : <EOL> self . algorithm . setup ( model = self . model , dataset = self . dataset ) <EOL> self . setup_extensions ( ) <EOL> self . model . enforce_constraints ( ) <EOL> def main_loop ( self , time_budget = None ) : <EOL> """<STR_LIT>""" <EOL> t0 = datetime . now ( ) <EOL> self . setup ( ) <EOL> if self . algorithm is None : <EOL> extension_continue = self . run_callbacks_and_monitoring ( ) <EOL> continue_learning = ( self . model . continue_learning ( ) and <EOL> extension_continue ) <EOL> assert continue_learning in [ True , False , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> while continue_learning : <EOL> if self . exceeded_time_budget ( t0 , time_budget ) : <EOL> break <EOL> rval = self . model . train_all ( dataset = self . dataset ) <EOL> if rval is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . model . monitor . report_epoch ( ) <EOL> extension_continue = self . run_callbacks_and_monitoring ( ) <EOL> freq = self . save_freq <EOL> if freq > <NUM_LIT:0> and self . model . monitor . get_epochs_seen ( ) % freq == <NUM_LIT:0> : <EOL> self . save ( ) <EOL> continue_learning = ( self . model . continue_learning ( ) and <EOL> extension_continue ) <EOL> assert continue_learning in [ True , False , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> else : <EOL> if not hasattr ( self . model , '<STR_LIT>' ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if len ( self . model . monitor . _datasets ) > <NUM_LIT:0> : <EOL> self . training_seconds . __doc__ = """<STR_LIT>""" <EOL> self . model . monitor . add_channel ( <EOL> name = "<STR_LIT>" , <EOL> ipt = None , <EOL> val = self . training_seconds , <EOL> data_specs = ( NullSpace ( ) , '<STR_LIT>' ) , <EOL> dataset = self . model . monitor . _datasets [ <NUM_LIT:0> ] ) <EOL> self . total_seconds . __doc__ = """<STR_LIT>""" <EOL> self . model . monitor . add_channel ( <EOL> name = "<STR_LIT>" , <EOL> ipt = None , <EOL> val = self . total_seconds , <EOL> data_specs = ( NullSpace ( ) , '<STR_LIT>' ) , <EOL> dataset = self . model . monitor . _datasets [ <NUM_LIT:0> ] ) <EOL> extension_continue = self . run_callbacks_and_monitoring ( ) <EOL> continue_learning = ( <EOL> self . algorithm . continue_learning ( self . model ) and <EOL> extension_continue <EOL> ) <EOL> assert continue_learning in [ True , False , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> while continue_learning : <EOL> if self . exceeded_time_budget ( t0 , time_budget ) : <EOL> break <EOL> with log_timing ( log , None , level = logging . DEBUG , <EOL> callbacks = [ self . total_seconds . set_value ] ) : <EOL> with log_timing ( <EOL> log , None , final_msg = '<STR_LIT>' , <EOL> callbacks = [ self . training_seconds . set_value ] <EOL> ) : <EOL> rval = self . algorithm . train ( dataset = self . dataset ) <EOL> if rval is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> self . model . monitor . report_epoch ( ) <EOL> extension_continue = self . run_callbacks_and_monitoring ( ) <EOL> if self . save_freq > <NUM_LIT:0> and self . model . monitor . get_epochs_seen ( ) % self . save_freq == <NUM_LIT:0> : <EOL> self . save ( ) <EOL> continue_learning = ( <EOL> self . algorithm . continue_learning ( self . model ) and <EOL> extension_continue <EOL> ) <EOL> assert continue_learning in [ True , False , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> self . model . monitor . training_succeeded = True <EOL> if self . save_freq > <NUM_LIT:0> : <EOL> self . save ( ) <EOL> def run_callbacks_and_monitoring ( self ) : <EOL> """<STR_LIT>""" <EOL> self . model . monitor ( ) <EOL> continue_learning = True <EOL> for extension in self . extensions : <EOL> try : <EOL> extension . on_monitor ( self . model , self . dataset , self . algorithm ) <EOL> except TypeError : <EOL> logging . warning ( '<STR_LIT>' + str ( extension ) ) <EOL> raise <EOL> except StopIteration : <EOL> log . info ( "<STR_LIT>" ) <EOL> continue_learning = False <EOL> return continue_learning <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> for extension in self . extensions : <EOL> extension . on_save ( self . model , self . dataset , self . algorithm ) <EOL> if self . save_path is not None : <EOL> with log_timing ( log , '<STR_LIT>' + self . save_path ) : <EOL> if self . first_save and ( not self . allow_overwrite ) and os . path . exists ( self . save_path ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> try : <EOL> self . dataset . _serialization_guard = SerializationGuard ( ) <EOL> serial . save ( self . save_path , self . model , <EOL> on_overwrite = '<STR_LIT>' ) <EOL> finally : <EOL> self . dataset . _serialization_guard = None <EOL> self . first_save = False <EOL> class SerializationGuard ( object ) : <EOL> """<STR_LIT>""" <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> log . error ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
