<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 collections import defaultdict <EOL> from theano . compat . six . moves import zip as izip_no_length_check <EOL> import numpy as np <EOL> from theano . compat import six <EOL> from theano import tensor as T <EOL> from pylearn2 . compat import OrderedDict <EOL> from pylearn2 . model_extensions . model_extension import ModelExtension <EOL> from pylearn2 . space import NullSpace <EOL> from pylearn2 . utils import function <EOL> from pylearn2 . utils import safe_zip <EOL> from pylearn2 . utils . exc import reraise_as <EOL> from pylearn2 . utils . track_version import MetaLibVersion <EOL> class Model ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = MetaLibVersion <EOL> _test_batch_size = <NUM_LIT:2> <EOL> def __init__ ( self , extensions = None ) : <EOL> super ( Model , self ) . __init__ ( ) <EOL> if extensions is None : <EOL> extensions = [ ] <EOL> else : <EOL> assert isinstance ( extensions , list ) <EOL> assert all ( isinstance ( extension , ModelExtension ) for extension in <EOL> extensions ) <EOL> self . __dict__ . update ( locals ( ) ) <EOL> del self . self <EOL> self . _disallow_censor_updates ( ) <EOL> self . names_to_del = set ( ) <EOL> def _disallow_censor_updates ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _overrides_censor_updates ( ) : <EOL> raise TypeError ( str ( type ( self ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _ensure_extensions ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> raise TypeError ( "<STR_LIT>" + str ( type ( self ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . extensions = [ ] <EOL> def __setstate__ ( self , d ) : <EOL> """<STR_LIT>""" <EOL> self . _disallow_censor_updates ( ) <EOL> self . __dict__ . update ( d ) <EOL> if '<STR_LIT>' not in d : <EOL> self . extensions = [ ] <EOL> def get_default_cost ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( str ( type ( self ) ) + <EOL> "<STR_LIT>" ) <EOL> def train_all ( self , dataset ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( str ( type ( self ) ) + <EOL> "<STR_LIT>" ) <EOL> def continue_learning ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( str ( type ( self ) ) + <EOL> "<STR_LIT>" ) <EOL> def train_batch ( self , dataset , batch_size ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_weights_view_shape ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( str ( type ( self ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def get_monitoring_channels ( self , data ) : <EOL> """<STR_LIT>""" <EOL> space , source = self . get_monitoring_data_specs ( ) <EOL> space . validate ( data ) <EOL> return OrderedDict ( ) <EOL> def get_monitoring_data_specs ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( NullSpace ( ) , '<STR_LIT>' ) <EOL> def set_batch_size ( self , batch_size ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_weights ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( str ( type ( self ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def get_weights_format ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( '<STR_LIT:v>' , '<STR_LIT:h>' ) <EOL> def get_weights_topo ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( str ( type ( self ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def score ( self , V ) : <EOL> """<STR_LIT>""" <EOL> return T . grad ( - self . free_energy ( V ) . sum ( ) , V ) <EOL> def get_lr_scalers ( self ) : <EOL> """<STR_LIT>""" <EOL> return OrderedDict ( ) <EOL> def _overrides_censor_updates ( self ) : <EOL> """<STR_LIT>""" <EOL> return type ( self ) . censor_updates != Model . censor_updates <EOL> def censor_updates ( self , updates ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def modify_updates ( self , updates ) : <EOL> """<STR_LIT>""" <EOL> self . _modify_updates ( updates ) <EOL> self . _ensure_extensions ( ) <EOL> for extension in self . extensions : <EOL> extension . post_modify_updates ( updates , self ) <EOL> def _modify_updates ( self , updates ) : <EOL> """<STR_LIT>""" <EOL> self . _disallow_censor_updates ( ) <EOL> def get_input_space ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . input_space <EOL> def get_output_space ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . output_space <EOL> def get_target_space ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _target_space <EOL> else : <EOL> return self . get_output_space ( ) <EOL> def get_input_source ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . input_source <EOL> else : <EOL> return '<STR_LIT>' <EOL> def get_target_source ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . target_source <EOL> else : <EOL> return '<STR_LIT>' <EOL> def free_energy ( self , V ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_params ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _params ) <EOL> def get_param_values ( self , borrow = False ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( self . get_params ( ) , set ) <EOL> return [ param . get_value ( borrow = borrow ) for param in self . get_params ( ) ] <EOL> def set_param_values ( self , values , borrow = False ) : <EOL> """<STR_LIT>""" <EOL> for param , value in zip ( self . get_params ( ) , values ) : <EOL> param . set_value ( value , borrow = borrow ) <EOL> def get_param_vector ( self ) : <EOL> """<STR_LIT>""" <EOL> values = self . get_param_values ( ) <EOL> values = [ value . reshape ( value . size ) for value in values ] <EOL> return np . concatenate ( values , axis = <NUM_LIT:0> ) <EOL> def set_param_vector ( self , vector ) : <EOL> """<STR_LIT>""" <EOL> params = self . get_params ( ) <EOL> cur_values = self . get_param_values ( ) <EOL> pos = <NUM_LIT:0> <EOL> for param , value in safe_zip ( params , cur_values ) : <EOL> size = value . size <EOL> new_value = vector [ pos : pos + size ] <EOL> param . set_value ( new_value . reshape ( * value . shape ) ) <EOL> pos += size <EOL> assert pos == vector . size <EOL> def redo_theano ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_input_dim ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_output_dim ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _disallow_censor_updates ( ) <EOL> d = OrderedDict ( ) <EOL> names_to_del = getattr ( self , '<STR_LIT>' , set ( ) ) <EOL> names_to_keep = set ( self . __dict__ . keys ( ) ) . difference ( names_to_del ) <EOL> for name in names_to_keep : <EOL> d [ name ] = self . __dict__ [ name ] <EOL> return d <EOL> def get_test_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _test_batch_size <EOL> def print_versions ( self , print_theano_config = False ) : <EOL> """<STR_LIT>""" <EOL> self . libv . print_versions ( ) <EOL> self . libv . print_exp_env_info ( print_theano_config ) <EOL> def register_names_to_del ( self , names ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( names , six . string_types ) : <EOL> names = [ names ] <EOL> try : <EOL> assert all ( isinstance ( n , six . string_types ) for n in iter ( names ) ) <EOL> except ( TypeError , AssertionError ) : <EOL> reraise_as ( ValueError ( '<STR_LIT>' ) ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . names_to_del = set ( ) <EOL> self . names_to_del = self . names_to_del . union ( names ) <EOL> def enforce_constraints ( self ) : <EOL> """<STR_LIT>""" <EOL> params = self . get_params ( ) <EOL> updates = OrderedDict ( izip_no_length_check ( params , params ) ) <EOL> self . modify_updates ( updates ) <EOL> f = function ( [ ] , updates = updates ) <EOL> f ( ) <EOL> @ property <EOL> def tag ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _tag = defaultdict ( dict ) <EOL> return self . _tag </s>
