<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 theano . compat . six import Iterator <EOL> from pylearn2 . datasets . dataset import Dataset <EOL> from pylearn2 . space import CompositeSpace <EOL> from pylearn2 . utils . data_specs import is_flat_specs <EOL> from pylearn2 . utils import wraps <EOL> class TransformerDataset ( Dataset ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , raw , transformer , cpu_only = False , <EOL> space_preserving = False ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( locals ( ) ) <EOL> del self . self <EOL> def get_batch_design ( self , batch_size , include_labels = False ) : <EOL> """<STR_LIT>""" <EOL> raw = self . raw . get_batch_design ( batch_size , include_labels ) <EOL> if include_labels : <EOL> X , y = raw <EOL> else : <EOL> X = raw <EOL> X = self . transformer . perform ( X ) <EOL> if include_labels : <EOL> return X , y <EOL> return X <EOL> def get_test_set ( self ) : <EOL> """<STR_LIT>""" <EOL> return TransformerDataset ( raw = self . raw . get_test_set ( ) , <EOL> transformer = self . transformer , <EOL> cpu_only = self . cpu_only , <EOL> space_preserving = self . space_preserving ) <EOL> def get_batch_topo ( self , batch_size ) : <EOL> """<STR_LIT>""" <EOL> X = self . get_batch_design ( batch_size ) <EOL> if self . space_preserving : <EOL> return self . raw . get_topological_view ( X ) <EOL> return X . reshape ( X . shape [ <NUM_LIT:0> ] , X . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def iterator ( self , mode = None , batch_size = None , num_batches = None , <EOL> rng = None , data_specs = None , <EOL> return_tuple = False ) : <EOL> """<STR_LIT>""" <EOL> if data_specs is not None : <EOL> assert is_flat_specs ( data_specs ) <EOL> space , source = data_specs <EOL> if not isinstance ( source , tuple ) : <EOL> source = ( source , ) <EOL> if isinstance ( space , CompositeSpace ) : <EOL> space = tuple ( space . components ) <EOL> else : <EOL> space = ( space , ) <EOL> if '<STR_LIT>' not in source : <EOL> raw_data_specs = data_specs <EOL> else : <EOL> feature_idx = source . index ( '<STR_LIT>' ) <EOL> if self . space_preserving : <EOL> feature_input_space = space [ feature_idx ] <EOL> else : <EOL> feature_input_space = self . transformer . get_input_space ( ) <EOL> raw_space = CompositeSpace ( ( feature_input_space , ) <EOL> + space [ : feature_idx ] <EOL> + space [ feature_idx + <NUM_LIT:1> : ] ) <EOL> raw_source = ( ( '<STR_LIT>' , ) <EOL> + source [ : feature_idx ] <EOL> + source [ feature_idx + <NUM_LIT:1> : ] ) <EOL> raw_data_specs = ( raw_space , raw_source ) <EOL> else : <EOL> raw_data_specs = None <EOL> raw_iterator = self . raw . iterator ( <EOL> mode = mode , batch_size = batch_size , <EOL> num_batches = num_batches , rng = rng , <EOL> data_specs = raw_data_specs , return_tuple = return_tuple ) <EOL> final_iterator = TransformerIterator ( raw_iterator , self , <EOL> data_specs = data_specs ) <EOL> return final_iterator <EOL> def has_targets ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . raw . has_targets ( ) <EOL> def adjust_for_viewer ( self , X ) : <EOL> """<STR_LIT>""" <EOL> if self . space_preserving : <EOL> return self . raw . adjust_for_viewer ( X ) <EOL> return X <EOL> def get_weights_view ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . space_preserving : <EOL> return self . raw . get_weights_view ( * args , ** kwargs ) <EOL> raise NotImplementedError ( ) <EOL> def get_topological_view ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . space_preserving : <EOL> return self . raw . get_weights_view ( * args , ** kwargs ) <EOL> raise NotImplementedError ( ) <EOL> def adjust_to_be_viewed_with ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . raw . adjust_to_be_viewed_with ( * args , ** kwargs ) <EOL> @ wraps ( Dataset . get_num_examples ) <EOL> def get_num_examples ( self ) : <EOL> return self . raw . get_num_examples ( ) <EOL> class TransformerIterator ( Iterator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , raw_iterator , transformer_dataset , data_specs ) : <EOL> """<STR_LIT>""" <EOL> self . raw_iterator = raw_iterator <EOL> self . transformer_dataset = transformer_dataset <EOL> self . stochastic = raw_iterator . stochastic <EOL> self . uneven = raw_iterator . uneven <EOL> self . data_specs = data_specs <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def __next__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raw_batch = self . raw_iterator . next ( ) <EOL> transformer = self . transformer_dataset . transformer <EOL> out_space = self . data_specs [ <NUM_LIT:0> ] <EOL> if isinstance ( out_space , CompositeSpace ) : <EOL> out_space = out_space . components [ <NUM_LIT:0> ] <EOL> if self . transformer_dataset . space_preserving : <EOL> rval_space = out_space <EOL> else : <EOL> rval_space = transformer . get_output_space ( ) <EOL> def transform ( X_batch ) : <EOL> rval = transformer . perform ( X_batch ) <EOL> if rval_space != out_space : <EOL> rval = rval_space . np_format_as ( rval , out_space ) <EOL> return rval <EOL> if not isinstance ( raw_batch , tuple ) : <EOL> rval = transform ( raw_batch ) <EOL> else : <EOL> rval = ( transform ( raw_batch [ <NUM_LIT:0> ] ) , ) + raw_batch [ <NUM_LIT:1> : ] <EOL> return rval <EOL> @ property <EOL> def num_examples ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . raw_iterator . num_examples </s>
