<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> import copy <EOL> import time <EOL> import warnings <EOL> import logging <EOL> import numpy as np <EOL> from theano . compat import six <EOL> from pylearn2 . compat import OrderedDict <EOL> import theano . sparse <EOL> from theano import config <EOL> from theano import tensor as T <EOL> from theano . printing import var_descriptor <EOL> from pylearn2 . config import yaml_parse <EOL> from pylearn2 . datasets . dataset import Dataset <EOL> from pylearn2 . space import Space , CompositeSpace , NullSpace <EOL> from pylearn2 . utils import function , sharedX , safe_zip , safe_izip <EOL> from pylearn2 . utils . exc import reraise_as <EOL> from pylearn2 . utils . iteration import is_stochastic <EOL> from pylearn2 . utils . data_specs import DataSpecsMapping <EOL> from pylearn2 . utils . string_utils import number_aware_alphabetical_key <EOL> from pylearn2 . utils . timing import log_timing <EOL> log = logging . getLogger ( __name__ ) <EOL> class Monitor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model ) : <EOL> self . training_succeeded = False <EOL> self . model = model <EOL> self . channels = OrderedDict ( ) <EOL> self . _num_batches_seen = <NUM_LIT:0> <EOL> self . _examples_seen = <NUM_LIT:0> <EOL> self . _epochs_seen = <NUM_LIT:0> <EOL> self . _datasets = [ ] <EOL> self . _iteration_mode = [ ] <EOL> self . _batch_size = [ ] <EOL> self . _num_batches = [ ] <EOL> self . _dirty = True <EOL> self . _rng_seed = [ ] <EOL> self . names_to_del = [ '<STR_LIT>' ] <EOL> self . t0 = time . time ( ) <EOL> self . theano_function_mode = None <EOL> self . on_channel_conflict = '<STR_LIT:error>' <EOL> self . _build_data_specs ( ) <EOL> def _build_data_specs ( self ) : <EOL> """<STR_LIT>""" <EOL> m_space , m_source = self . model . get_monitoring_data_specs ( ) <EOL> input_spaces = [ m_space ] <EOL> input_sources = [ m_source ] <EOL> for channel in self . channels . values ( ) : <EOL> space = channel . data_specs [ <NUM_LIT:0> ] <EOL> assert isinstance ( space , Space ) <EOL> input_spaces . append ( space ) <EOL> input_sources . append ( channel . data_specs [ <NUM_LIT:1> ] ) <EOL> nested_space = CompositeSpace ( input_spaces ) <EOL> nested_source = tuple ( input_sources ) <EOL> self . _nested_data_specs = ( nested_space , nested_source ) <EOL> self . _data_specs_mapping = DataSpecsMapping ( self . _nested_data_specs ) <EOL> flat_space = self . _data_specs_mapping . flatten ( nested_space , <EOL> return_tuple = True ) <EOL> flat_source = self . _data_specs_mapping . flatten ( nested_source , <EOL> return_tuple = True ) <EOL> self . _flat_data_specs = ( CompositeSpace ( flat_space ) , flat_source ) <EOL> def set_theano_function_mode ( self , mode ) : <EOL> """<STR_LIT>""" <EOL> if self . theano_function_mode != mode : <EOL> self . _dirty = True <EOL> self . theano_function_mode = mode <EOL> def add_dataset ( self , dataset , mode = '<STR_LIT>' , batch_size = None , <EOL> num_batches = None , seed = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( dataset , list ) : <EOL> dataset = [ dataset ] <EOL> if not isinstance ( mode , list ) : <EOL> mode = [ mode ] <EOL> if not isinstance ( batch_size , list ) : <EOL> batch_size = [ batch_size ] <EOL> if not isinstance ( num_batches , list ) : <EOL> num_batches = [ num_batches ] <EOL> if seed is None : <EOL> seed = [ None ] * len ( dataset ) <EOL> if not isinstance ( seed , list ) : <EOL> seed = [ seed ] <EOL> if len ( mode ) != len ( dataset ) : <EOL> raise ValueError ( "<STR_LIT>" + str ( len ( dataset ) ) + <EOL> "<STR_LIT>" + str ( len ( mode ) ) + "<STR_LIT>" ) <EOL> if any ( [ len ( l ) != len ( dataset ) for l in [ batch_size , seed ] ] ) : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> for ( d , m , b , n , sd ) in safe_izip ( dataset , mode , batch_size , <EOL> num_batches , seed ) : <EOL> try : <EOL> it = d . iterator ( mode = m , <EOL> batch_size = b , <EOL> num_batches = n , <EOL> data_specs = self . _flat_data_specs , <EOL> return_tuple = True , <EOL> rng = sd ) <EOL> except ValueError as exc : <EOL> reraise_as ( ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" + str ( exc ) ) ) <EOL> if it . stochastic : <EOL> if sd is None : <EOL> raise TypeError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if not isinstance ( sd , ( list , tuple , int ) ) : <EOL> raise TypeError ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> assert sd is None <EOL> if d not in self . _datasets : <EOL> self . _datasets . append ( d ) <EOL> self . _iteration_mode . append ( m ) <EOL> self . _batch_size . append ( b ) <EOL> self . _num_batches . append ( n ) <EOL> self . _rng_seed . append ( sd ) <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _dirty : <EOL> self . redo_theano ( ) <EOL> datasets = self . _datasets <EOL> self . begin_record_entry ( ) <EOL> for d , i , b , n , a , sd , ne in safe_izip ( datasets , <EOL> self . _iteration_mode , <EOL> self . _batch_size , <EOL> self . _num_batches , <EOL> self . accum , <EOL> self . _rng_seed , <EOL> self . num_examples ) : <EOL> if isinstance ( d , six . string_types ) : <EOL> d = yaml_parse . load ( d ) <EOL> raise NotImplementedError ( ) <EOL> myiterator = d . iterator ( mode = i , <EOL> batch_size = b , <EOL> num_batches = n , <EOL> data_specs = self . _flat_data_specs , <EOL> return_tuple = True , <EOL> rng = sd ) <EOL> if len ( self . _flat_data_specs [ <NUM_LIT:1> ] ) == <NUM_LIT:0> : <EOL> X = ( ) <EOL> self . run_prereqs ( X , d ) <EOL> a ( * X ) <EOL> else : <EOL> actual_ne = <NUM_LIT:0> <EOL> for X in myiterator : <EOL> self . run_prereqs ( X , d ) <EOL> a ( * X ) <EOL> actual_ne += self . _flat_data_specs [ <NUM_LIT:0> ] . np_batch_size ( X ) <EOL> if actual_ne != ne : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( ne , actual_ne ) ) <EOL> log . info ( "<STR_LIT>" ) <EOL> log . info ( "<STR_LIT>" % self . _epochs_seen ) <EOL> log . info ( "<STR_LIT>" % self . _num_batches_seen ) <EOL> log . info ( "<STR_LIT>" % self . _examples_seen ) <EOL> t = time . time ( ) - self . t0 <EOL> for channel_name in sorted ( self . channels . keys ( ) , <EOL> key = number_aware_alphabetical_key ) : <EOL> channel = self . channels [ channel_name ] <EOL> channel . time_record . append ( t ) <EOL> channel . batch_record . append ( self . _num_batches_seen ) <EOL> channel . example_record . append ( self . _examples_seen ) <EOL> channel . epoch_record . append ( self . _epochs_seen ) <EOL> val = channel . val_shared . get_value ( ) <EOL> channel . val_record . append ( val ) <EOL> if abs ( val ) < <NUM_LIT> : <EOL> val_str = str ( val ) <EOL> else : <EOL> val_str = '<STR_LIT>' % val <EOL> log . info ( "<STR_LIT>" % ( channel_name , val_str ) ) <EOL> def run_prereqs ( self , data , dataset ) : <EOL> """<STR_LIT>""" <EOL> if dataset not in self . prereqs : <EOL> return <EOL> for prereq in self . prereqs [ dataset ] : <EOL> prereq ( * data ) <EOL> def get_batches_seen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _num_batches_seen <EOL> def get_epochs_seen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _epochs_seen <EOL> def get_examples_seen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _examples_seen <EOL> def report_batch ( self , num_examples ) : <EOL> """<STR_LIT>""" <EOL> self . _examples_seen += num_examples <EOL> self . _num_batches_seen += <NUM_LIT:1> <EOL> def report_epoch ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _epochs_seen += <NUM_LIT:1> <EOL> def redo_theano ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _dirty = False <EOL> self . _build_data_specs ( ) <EOL> init_names = dir ( self ) <EOL> self . prereqs = OrderedDict ( ) <EOL> for channel in self . channels . values ( ) : <EOL> if channel . prereqs is not None : <EOL> dataset = channel . dataset <EOL> if dataset not in self . prereqs : <EOL> self . prereqs [ dataset ] = [ ] <EOL> prereqs = self . prereqs [ dataset ] <EOL> for prereq in channel . prereqs : <EOL> if prereq not in prereqs : <EOL> prereqs . append ( prereq ) <EOL> updates = OrderedDict ( ) <EOL> for channel in self . channels . values ( ) : <EOL> updates [ channel . val_shared ] = np . cast [ config . floatX ] ( <NUM_LIT:0.0> ) <EOL> with log_timing ( log , "<STR_LIT>" ) : <EOL> self . begin_record_entry = function ( <EOL> inputs = [ ] , <EOL> updates = updates , <EOL> mode = self . theano_function_mode , <EOL> name = '<STR_LIT>' <EOL> ) <EOL> updates = OrderedDict ( ) <EOL> givens = OrderedDict ( ) <EOL> batch_names = [ '<STR_LIT>' % s for s in self . _flat_data_specs [ <NUM_LIT:1> ] ] <EOL> theano_args = self . _flat_data_specs [ <NUM_LIT:0> ] . make_theano_batch ( batch_names ) <EOL> batch_size = self . _flat_data_specs [ <NUM_LIT:0> ] . batch_size ( theano_args ) <EOL> nested_theano_args = self . _data_specs_mapping . nest ( theano_args ) <EOL> if not isinstance ( nested_theano_args , tuple ) : <EOL> nested_theano_args = ( nested_theano_args , ) <EOL> assert len ( nested_theano_args ) == ( len ( self . channels ) + <NUM_LIT:1> ) <EOL> log . info ( '<STR_LIT>' ) <EOL> for key in sorted ( self . channels . keys ( ) ) : <EOL> mode = self . theano_function_mode <EOL> if mode is not None and hasattr ( mode , '<STR_LIT>' ) : <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> '<STR_LIT>' + key + '<STR_LIT:\n>' ) <EOL> log . info ( '<STR_LIT>' % key ) <EOL> it = [ ] <EOL> for d , i , n , b in safe_izip ( self . _datasets , self . _iteration_mode , <EOL> self . _num_batches , self . _batch_size ) : <EOL> it . append ( d . iterator ( mode = i , num_batches = n , batch_size = b , <EOL> data_specs = self . _flat_data_specs , <EOL> return_tuple = True ) ) <EOL> self . num_examples = [ i . num_examples for i in it ] <EOL> givens = [ OrderedDict ( ) for d in self . _datasets ] <EOL> updates = [ OrderedDict ( ) for d in self . _datasets ] <EOL> for i , channel in enumerate ( self . channels . values ( ) ) : <EOL> index = self . _datasets . index ( channel . dataset ) <EOL> d = self . _datasets [ index ] <EOL> g = givens [ index ] <EOL> cur_num_examples = self . num_examples [ index ] <EOL> u = updates [ index ] <EOL> c_mapping = DataSpecsMapping ( channel . data_specs ) <EOL> channel_inputs = c_mapping . flatten ( channel . graph_input , <EOL> return_tuple = True ) <EOL> inputs = c_mapping . flatten ( nested_theano_args [ i + <NUM_LIT:1> ] , <EOL> return_tuple = True ) <EOL> for ( channel_X , X ) in safe_izip ( channel_inputs , inputs ) : <EOL> assert channel_X not in g or g [ channel_X ] is X <EOL> assert channel_X . type == X . type , ( channel_X . type , X . type ) <EOL> g [ channel_X ] = X <EOL> if batch_size == <NUM_LIT:0> : <EOL> assert len ( self . _flat_data_specs [ <NUM_LIT:1> ] ) == <NUM_LIT:0> <EOL> val = channel . val <EOL> else : <EOL> if n == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> val = T . cast ( channel . val * T . cast ( batch_size , '<STR_LIT>' ) <EOL> / cur_num_examples , config . floatX ) <EOL> u [ channel . val_shared ] = channel . val_shared + val <EOL> with log_timing ( log , "<STR_LIT>" ) : <EOL> for up in updates : <EOL> for key in up : <EOL> if key . dtype != up [ key ] . dtype : <EOL> raise TypeError ( '<STR_LIT>' + <EOL> key . name + '<STR_LIT>' + key . dtype + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + up [ key ] . dtype ) <EOL> self . accum = [ ] <EOL> for idx , packed in enumerate ( safe_izip ( givens , updates ) ) : <EOL> g , u = packed <EOL> mode = self . theano_function_mode <EOL> if mode is not None and hasattr ( mode , '<STR_LIT>' ) : <EOL> for elem in g : <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> var_descriptor ( elem ) + '<STR_LIT:\n>' ) <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> var_descriptor ( g [ elem ] ) + '<STR_LIT:\n>' ) <EOL> for elem in u : <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> var_descriptor ( elem ) + '<STR_LIT:\n>' ) <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> var_descriptor ( u [ elem ] ) + '<STR_LIT:\n>' ) <EOL> function_name = '<STR_LIT>' % idx <EOL> if mode is not None and hasattr ( mode , '<STR_LIT>' ) : <EOL> mode . record . handle_line ( '<STR_LIT>' ) <EOL> self . accum . append ( function ( theano_args , <EOL> givens = g , <EOL> updates = u , <EOL> mode = self . theano_function_mode , <EOL> name = function_name ) ) <EOL> for a in self . accum : <EOL> if mode is not None and hasattr ( mode , '<STR_LIT>' ) : <EOL> for elem in a . maker . fgraph . outputs : <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> var_descriptor ( elem ) + '<STR_LIT:\n>' ) <EOL> log . info ( "<STR_LIT>" % len ( a . maker . fgraph . toposort ( ) ) ) <EOL> final_names = dir ( self ) <EOL> self . register_names_to_del ( [ name for name in final_names <EOL> if name not in init_names ] ) <EOL> def register_names_to_del ( self , names ) : <EOL> """<STR_LIT>""" <EOL> for name in names : <EOL> if name not in self . names_to_del : <EOL> self . names_to_del . append ( name ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _datasets = [ self . _dataset ] <EOL> del self . _dataset <EOL> temp = self . _datasets <EOL> if self . _datasets : <EOL> self . _datasets = [ ] <EOL> for dataset in temp : <EOL> if isinstance ( dataset , six . string_types ) : <EOL> self . _datasets . append ( dataset ) <EOL> else : <EOL> try : <EOL> self . _datasets . append ( dataset . yaml_src ) <EOL> except AttributeError : <EOL> warnings . warn ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> d = copy . copy ( self . __dict__ ) <EOL> self . _datasets = temp <EOL> for name in self . names_to_del : <EOL> if name in d : <EOL> del d [ name ] <EOL> return d <EOL> def __setstate__ ( self , d ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in d : <EOL> d [ '<STR_LIT>' ] = [ d [ '<STR_LIT>' ] ] <EOL> del d [ '<STR_LIT>' ] <EOL> self . __dict__ . update ( d ) <EOL> def add_channel ( self , name , ipt , val , dataset = None , prereqs = None , <EOL> data_specs = None ) : <EOL> """<STR_LIT>""" <EOL> if six . PY3 : <EOL> numeric = ( float , int ) <EOL> else : <EOL> numeric = ( float , int , long ) <EOL> if isinstance ( val , numeric ) : <EOL> val = np . cast [ theano . config . floatX ] ( val ) <EOL> val = T . as_tensor_variable ( val ) <EOL> if data_specs is None : <EOL> warnings . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> if isinstance ( ipt , list ) : <EOL> ipt = tuple ( ipt ) <EOL> if ipt is not None and not isinstance ( ipt , tuple ) : <EOL> ipt = ( ipt , ) <EOL> if ipt is None : <EOL> data_specs = ( NullSpace ( ) , '<STR_LIT>' ) <EOL> elif len ( ipt ) == <NUM_LIT:0> : <EOL> data_specs = ( CompositeSpace ( [ ] ) , ( ) ) <EOL> elif hasattr ( dataset , '<STR_LIT>' ) : <EOL> dataset_space , dataset_source = dataset . get_data_specs ( ) <EOL> if ( len ( ipt ) == <NUM_LIT:1> and <EOL> dataset_source is not None and <EOL> ( not isinstance ( dataset_source , tuple ) or <EOL> len ( dataset_source ) == <NUM_LIT:1> ) and <EOL> '<STR_LIT>' in dataset_source ) : <EOL> data_specs = ( dataset_space , dataset_source ) <EOL> elif ( len ( ipt ) == <NUM_LIT:2> and <EOL> dataset_source == ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> data_specs = ( dataset_space , dataset_source ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" <EOL> % ( ipt , dataset ) ) <EOL> data_specs [ <NUM_LIT:0> ] . validate ( ipt ) <EOL> mapping = DataSpecsMapping ( data_specs ) <EOL> flat_ipt = mapping . flatten ( ipt ) <EOL> if not isinstance ( flat_ipt , tuple ) : <EOL> flat_ipt = ( flat_ipt , ) <EOL> inputs = theano . gof . graph . inputs ( [ val ] ) <EOL> for elem in inputs : <EOL> if not hasattr ( elem , '<STR_LIT>' ) and not isinstance ( elem , theano . gof . graph . Constant ) : <EOL> if elem not in flat_ipt : <EOL> raise ValueError ( "<STR_LIT>" + str ( elem ) + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> mode = self . theano_function_mode <EOL> if mode is not None and hasattr ( mode , '<STR_LIT>' ) : <EOL> mode . record . handle_line ( '<STR_LIT>' + name + '<STR_LIT:\n>' ) <EOL> assert isinstance ( flat_ipt , tuple ) <EOL> if len ( flat_ipt ) != <NUM_LIT:1> : <EOL> for elem in flat_ipt : <EOL> mode . record . handle_line ( '<STR_LIT>' + <EOL> var_descriptor ( elem ) + '<STR_LIT:\n>' ) <EOL> else : <EOL> mode . record . handle_line ( name + '<STR_LIT>' + <EOL> var_descriptor ( flat_ipt [ <NUM_LIT:0> ] ) + '<STR_LIT:\n>' ) <EOL> mode . record . handle_line ( '<STR_LIT>' + name + '<STR_LIT>' + <EOL> var_descriptor ( val ) + '<STR_LIT:\n>' ) <EOL> if dataset is None : <EOL> if len ( self . _datasets ) == <NUM_LIT:1> : <EOL> dataset = self . _datasets [ <NUM_LIT:0> ] <EOL> elif len ( self . _datasets ) == <NUM_LIT:0> : <EOL> raise ValueError ( _err_no_data ) <EOL> else : <EOL> raise ValueError ( _err_ambig_data ) <EOL> try : <EOL> self . _datasets . index ( dataset ) <EOL> except ValueError : <EOL> reraise_as ( ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) ) <EOL> if ( ( self . on_channel_conflict not in <EOL> ( '<STR_LIT:error>' , '<STR_LIT>' , '<STR_LIT>' ) ) ) : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if name in self . channels and self . on_channel_conflict == '<STR_LIT:error>' : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> name ) <EOL> elif ( ( name in self . channels and <EOL> self . on_channel_conflict == '<STR_LIT>' ) ) : <EOL> self . channels [ name ] = MonitorChannel ( ipt , val , name , data_specs , <EOL> dataset , prereqs , <EOL> self . channels [ name ] ) <EOL> elif ( ( name not in self . channels or <EOL> self . on_channel_conflict == '<STR_LIT>' ) ) : <EOL> self . channels [ name ] = MonitorChannel ( ipt , val , name , data_specs , <EOL> dataset , prereqs ) <EOL> self . _dirty = True <EOL> def _sanity_check ( self ) : <EOL> """<STR_LIT>""" <EOL> for name in self . channels : <EOL> channel = self . channels [ name ] <EOL> assert hasattr ( channel , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def get_monitor ( cls , model ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( model , '<STR_LIT>' ) : <EOL> rval = model . monitor <EOL> rval . _sanity_check ( ) <EOL> else : <EOL> rval = Monitor ( model ) <EOL> model . monitor = rval <EOL> return rval <EOL> @ property <EOL> def batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _batch_size <EOL> @ property <EOL> def num_batches ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _num_batches <EOL> def setup ( self , dataset , cost , batch_size , num_batches = None , <EOL> extra_costs = None , mode = '<STR_LIT>' , obj_prereqs = None , <EOL> cost_monitoring_args = None ) : <EOL> """<STR_LIT>""" <EOL> if dataset is None : <EOL> return <EOL> if isinstance ( dataset , Dataset ) : <EOL> dataset = { '<STR_LIT>' : dataset } <EOL> else : <EOL> assert isinstance ( dataset , dict ) <EOL> assert all ( isinstance ( key , str ) for key in dataset ) <EOL> assert all ( isinstance ( dataset [ key ] , Dataset ) for key in dataset ) <EOL> if extra_costs is None : <EOL> costs = { } <EOL> else : <EOL> assert isinstance ( extra_costs , ( OrderedDict , dict ) ) <EOL> costs = extra_costs <EOL> assert '<STR_LIT>' not in costs <EOL> costs [ '<STR_LIT>' ] = cost <EOL> if cost_monitoring_args is None : <EOL> cost_monitoring_args = { } <EOL> model = self . model <EOL> cost_names = sorted ( costs . keys ( ) ) <EOL> spaces = [ ] <EOL> sources = [ ] <EOL> for c in cost_names : <EOL> c_space , c_source = costs [ c ] . get_data_specs ( model ) <EOL> spaces . append ( c_space ) <EOL> sources . append ( c_source ) <EOL> m_space , m_source = model . get_monitoring_data_specs ( ) <EOL> spaces . append ( m_space ) <EOL> sources . append ( m_source ) <EOL> nested_space = CompositeSpace ( spaces ) <EOL> nested_sources = tuple ( sources ) <EOL> mapping = DataSpecsMapping ( ( nested_space , nested_sources ) ) <EOL> space_tuple = mapping . flatten ( nested_space , return_tuple = True ) <EOL> source_tuple = mapping . flatten ( nested_sources , return_tuple = True ) <EOL> ipt = tuple ( space . make_theano_batch ( name = '<STR_LIT>' % source , <EOL> batch_size = None ) <EOL> for ( space , source ) in safe_zip ( space_tuple , source_tuple ) ) <EOL> nested_ipt = mapping . nest ( ipt ) <EOL> custom_channels = { } <EOL> for i , cost_name in enumerate ( cost_names ) : <EOL> if cost_name == '<STR_LIT>' : <EOL> prefix = '<STR_LIT>' <EOL> else : <EOL> prefix = cost_name + '<STR_LIT:_>' <EOL> cost = costs [ cost_name ] <EOL> cost_ipt = nested_ipt [ i ] <EOL> raw_channels = cost . get_monitoring_channels ( model , cost_ipt ) <EOL> channels = { } <EOL> for name in raw_channels : <EOL> channels [ prefix + name ] = ( raw_channels [ name ] , <EOL> cost_ipt , <EOL> ( spaces [ i ] , sources [ i ] ) ) <EOL> custom_channels . update ( channels ) <EOL> model_channels = model . get_monitoring_channels ( nested_ipt [ - <NUM_LIT:1> ] ) <EOL> channels = { } <EOL> for name in model_channels : <EOL> channels [ name ] = ( model_channels [ name ] , <EOL> nested_ipt [ - <NUM_LIT:1> ] , <EOL> ( spaces [ - <NUM_LIT:1> ] , sources [ - <NUM_LIT:1> ] ) ) <EOL> custom_channels . update ( channels ) <EOL> if is_stochastic ( mode ) : <EOL> seed = [ [ <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ] ] <EOL> else : <EOL> seed = None <EOL> for dataset_name in dataset : <EOL> cur_dataset = dataset [ dataset_name ] <EOL> self . add_dataset ( dataset = cur_dataset , <EOL> mode = mode , <EOL> batch_size = batch_size , <EOL> num_batches = num_batches , <EOL> seed = seed ) <EOL> if dataset_name == '<STR_LIT>' : <EOL> dprefix = '<STR_LIT>' <EOL> else : <EOL> dprefix = dataset_name + '<STR_LIT:_>' <EOL> for i , cost_name in enumerate ( cost_names ) : <EOL> cost = costs [ cost_name ] <EOL> cost_ipt = nested_ipt [ i ] <EOL> cost_value = cost . expr ( model , cost_ipt ) <EOL> if cost_value is not None : <EOL> if cost_name == '<STR_LIT>' : <EOL> name = dprefix + '<STR_LIT>' <EOL> prereqs = obj_prereqs <EOL> else : <EOL> name = dprefix + cost_name <EOL> prereqs = None <EOL> cost . get_data_specs ( model ) [ <NUM_LIT:0> ] . validate ( cost_ipt ) <EOL> self . add_channel ( name = name , <EOL> ipt = cost_ipt , <EOL> val = cost_value , <EOL> data_specs = cost . get_data_specs ( model ) , <EOL> dataset = cur_dataset , <EOL> prereqs = prereqs ) <EOL> for key in custom_channels : <EOL> val , ipt , data_specs = custom_channels [ key ] <EOL> data_specs [ <NUM_LIT:0> ] . validate ( ipt ) <EOL> self . add_channel ( name = dprefix + key , <EOL> ipt = ipt , <EOL> val = val , <EOL> data_specs = data_specs , <EOL> dataset = cur_dataset ) <EOL> class MonitorChannel ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph_input , val , name , data_specs , dataset , <EOL> prereqs = None , old_channel = None ) : <EOL> self . name = name <EOL> self . prereqs = prereqs <EOL> self . graph_input = graph_input <EOL> self . data_specs = data_specs <EOL> if isinstance ( val , float ) : <EOL> val = T . constant ( np . cast [ config . floatX ] ( val ) ) <EOL> self . val = val <EOL> self . val_shared = sharedX ( <NUM_LIT:0.0> , name + "<STR_LIT>" ) <EOL> assert self . val_shared . dtype == config . floatX , "<STR_LIT>" % ( config . floatX , self . val_shared . dtype ) <EOL> if not hasattr ( val , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' + name + '<STR_LIT>' + <EOL> str ( type ( val ) ) ) <EOL> if val . dtype != self . val_shared . dtype : <EOL> raise ValueError ( '<STR_LIT>' + <EOL> str ( self . val_shared . dtype ) + '<STR_LIT>' + name + <EOL> '<STR_LIT>' + str ( val . dtype ) ) <EOL> if val . ndim != <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' + <EOL> '<STR_LIT>' + name + '<STR_LIT>' + <EOL> str ( val . ndim ) ) <EOL> self . dataset = dataset <EOL> if old_channel is not None : <EOL> self . val_record = old_channel . val_record [ : - <NUM_LIT:1> ] <EOL> self . batch_record = old_channel . batch_record [ : - <NUM_LIT:1> ] <EOL> self . example_record = old_channel . example_record [ : - <NUM_LIT:1> ] <EOL> self . epoch_record = old_channel . epoch_record [ : - <NUM_LIT:1> ] <EOL> self . time_record = old_channel . time_record [ : - <NUM_LIT:1> ] <EOL> else : <EOL> self . val_record = [ ] <EOL> self . batch_record = [ ] <EOL> self . example_record = [ ] <EOL> self . epoch_record = [ ] <EOL> self . time_record = [ ] <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> graph_input_str = str ( self . graph_input ) <EOL> except Exception : <EOL> graph_input_str = '<STR_LIT>' <EOL> try : <EOL> val_str = str ( self . val ) <EOL> except Exception : <EOL> val_str = '<STR_LIT>' <EOL> try : <EOL> name_str = str ( self . name ) <EOL> except Exception : <EOL> name_str = '<STR_LIT>' <EOL> try : <EOL> prereqs_str = str ( self . prereqs ) <EOL> except Exception : <EOL> prereqs_str = '<STR_LIT>' <EOL> return "<STR_LIT>" % ( graph_input_str , <EOL> val_str , <EOL> name_str , <EOL> prereqs_str ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> doc = get_monitor_doc ( self . val ) <EOL> else : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> doc = self . doc <EOL> else : <EOL> doc = None <EOL> return { <EOL> '<STR_LIT>' : doc , <EOL> '<STR_LIT>' : self . example_record , <EOL> '<STR_LIT>' : self . batch_record , <EOL> '<STR_LIT>' : self . time_record , <EOL> '<STR_LIT>' : self . epoch_record , <EOL> '<STR_LIT>' : self . val_record <EOL> } <EOL> def __setstate__ ( self , d ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( d ) <EOL> if '<STR_LIT>' not in d : <EOL> self . batch_record = [ None ] * len ( self . val_record ) <EOL> if '<STR_LIT>' not in d : <EOL> self . epoch_record = range ( len ( self . val_record ) ) <EOL> if '<STR_LIT>' not in d : <EOL> self . time_record = [ None ] * len ( self . val_record ) <EOL> def push_monitor ( model , name , transfer_experience = False , <EOL> save_records = False ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( model , '<STR_LIT>' ) <EOL> old_monitor = model . monitor <EOL> setattr ( model , name , old_monitor ) <EOL> del model . monitor <EOL> if transfer_experience : <EOL> monitor = Monitor . get_monitor ( model ) <EOL> assert monitor is not old_monitor <EOL> monitor . _num_batches_seen = old_monitor . _num_batches_seen <EOL> monitor . _examples_seen = old_monitor . _examples_seen <EOL> monitor . _epochs_seen = old_monitor . _epochs_seen <EOL> if save_records : <EOL> monitor . on_channel_conflict = '<STR_LIT>' <EOL> monitor . channels = copy . copy ( old_monitor . channels ) <EOL> for key , value in list ( monitor . channels . items ( ) ) : <EOL> value . prereqs = None <EOL> return model <EOL> def read_channel ( model , channel_name , monitor_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( model , monitor_name ) . channels [ channel_name ] . val_record [ - <NUM_LIT:1> ] <EOL> def get_channel ( model , dataset , channel , cost , batch_size ) : <EOL> """<STR_LIT>""" <EOL> monitor = Monitor ( model ) <EOL> monitor . setup ( dataset = dataset , cost = cost , batch_size = batch_size ) <EOL> monitor ( ) <EOL> channels = monitor . channels <EOL> channel = channels [ channel ] <EOL> val_record = channel . val_record <EOL> value , = val_record <EOL> return value <EOL> def get_monitor_doc ( var ) : <EOL> """<STR_LIT>""" <EOL> doc = None <EOL> if var . __doc__ is not var . __class__ . __doc__ : <EOL> doc = var . __doc__ <EOL> return doc <EOL> _err_no_data = "<STR_LIT>" <EOL> _err_ambig_data = ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) </s>
