<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 numpy as N <EOL> np = N <EOL> from theano . compat . six . moves import xrange <EOL> from pylearn2 . datasets import dense_design_matrix <EOL> from pylearn2 . datasets import control <EOL> from pylearn2 . datasets import cache <EOL> from pylearn2 . utils import serial <EOL> from pylearn2 . utils . mnist_ubyte import read_mnist_images <EOL> from pylearn2 . utils . mnist_ubyte import read_mnist_labels <EOL> from pylearn2 . utils . rng import make_np_rng <EOL> class MNIST ( dense_design_matrix . DenseDesignMatrix ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , which_set , center = False , shuffle = False , <EOL> binarize = False , start = None , stop = None , <EOL> axes = [ '<STR_LIT:b>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' ] , <EOL> preprocessor = None , <EOL> fit_preprocessor = False , <EOL> fit_test_preprocessor = False ) : <EOL> self . args = locals ( ) <EOL> if which_set not in [ '<STR_LIT:train>' , '<STR_LIT:test>' ] : <EOL> if which_set == '<STR_LIT>' : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( which_set , ) + <EOL> '<STR_LIT>' ) <EOL> def dimshuffle ( b01c ) : <EOL> """<STR_LIT>""" <EOL> default = ( '<STR_LIT:b>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' ) <EOL> return b01c . transpose ( * [ default . index ( axis ) for axis in axes ] ) <EOL> if control . get_load_data ( ) : <EOL> path = "<STR_LIT>" <EOL> if which_set == '<STR_LIT:train>' : <EOL> im_path = path + '<STR_LIT>' <EOL> label_path = path + '<STR_LIT>' <EOL> else : <EOL> assert which_set == '<STR_LIT:test>' <EOL> im_path = path + '<STR_LIT>' <EOL> label_path = path + '<STR_LIT>' <EOL> im_path = serial . preprocess ( im_path ) <EOL> label_path = serial . preprocess ( label_path ) <EOL> datasetCache = cache . datasetCache <EOL> im_path = datasetCache . cache_file ( im_path ) <EOL> label_path = datasetCache . cache_file ( label_path ) <EOL> topo_view = read_mnist_images ( im_path , dtype = '<STR_LIT>' ) <EOL> y = np . atleast_2d ( read_mnist_labels ( label_path ) ) . T <EOL> else : <EOL> if which_set == '<STR_LIT:train>' : <EOL> size = <NUM_LIT> <EOL> elif which_set == '<STR_LIT:test>' : <EOL> size = <NUM_LIT> <EOL> else : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( which_set , ) + <EOL> '<STR_LIT>' ) <EOL> topo_view = np . random . rand ( size , <NUM_LIT> , <NUM_LIT> ) <EOL> y = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:10> , ( size , <NUM_LIT:1> ) ) <EOL> if binarize : <EOL> topo_view = ( topo_view > <NUM_LIT:0.5> ) . astype ( '<STR_LIT>' ) <EOL> y_labels = <NUM_LIT:10> <EOL> m , r , c = topo_view . shape <EOL> assert r == <NUM_LIT> <EOL> assert c == <NUM_LIT> <EOL> topo_view = topo_view . reshape ( m , r , c , <NUM_LIT:1> ) <EOL> if which_set == '<STR_LIT:train>' : <EOL> assert m == <NUM_LIT> <EOL> elif which_set == '<STR_LIT:test>' : <EOL> assert m == <NUM_LIT> <EOL> else : <EOL> assert False <EOL> if center : <EOL> topo_view -= topo_view . mean ( axis = <NUM_LIT:0> ) <EOL> if shuffle : <EOL> self . shuffle_rng = make_np_rng ( <EOL> None , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , which_method = "<STR_LIT>" ) <EOL> for i in xrange ( topo_view . shape [ <NUM_LIT:0> ] ) : <EOL> j = self . shuffle_rng . randint ( m ) <EOL> tmp = topo_view [ i , : , : , : ] . copy ( ) <EOL> topo_view [ i , : , : , : ] = topo_view [ j , : , : , : ] <EOL> topo_view [ j , : , : , : ] = tmp <EOL> tmp = y [ i : i + <NUM_LIT:1> ] . copy ( ) <EOL> y [ i ] = y [ j ] <EOL> y [ j ] = tmp <EOL> super ( MNIST , self ) . __init__ ( topo_view = dimshuffle ( topo_view ) , y = y , <EOL> axes = axes , y_labels = y_labels ) <EOL> assert not N . any ( N . isnan ( self . X ) ) <EOL> if start is not None : <EOL> assert start >= <NUM_LIT:0> <EOL> if stop > self . X . shape [ <NUM_LIT:0> ] : <EOL> raise ValueError ( '<STR_LIT>' + str ( stop ) + '<STR_LIT:>>' + <EOL> '<STR_LIT>' + str ( self . X . shape [ <NUM_LIT:0> ] ) ) <EOL> assert stop > start <EOL> self . X = self . X [ start : stop , : ] <EOL> if self . X . shape [ <NUM_LIT:0> ] != stop - start : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % ( self . X . shape [ <NUM_LIT:0> ] , start , stop ) ) <EOL> if len ( self . y . shape ) > <NUM_LIT:1> : <EOL> self . y = self . y [ start : stop , : ] <EOL> else : <EOL> self . y = self . y [ start : stop ] <EOL> assert self . y . shape [ <NUM_LIT:0> ] == stop - start <EOL> if which_set == '<STR_LIT:test>' : <EOL> assert fit_test_preprocessor is None or ( fit_preprocessor == fit_test_preprocessor ) <EOL> if self . X is not None and preprocessor : <EOL> preprocessor . apply ( self , fit_preprocessor ) <EOL> def adjust_for_viewer ( self , X ) : <EOL> """<STR_LIT>""" <EOL> return N . clip ( X * <NUM_LIT> - <NUM_LIT:1.> , - <NUM_LIT:1.> , <NUM_LIT:1.> ) <EOL> def adjust_to_be_viewed_with ( self , X , other , per_example = False ) : <EOL> """<STR_LIT>""" <EOL> return self . adjust_for_viewer ( X ) <EOL> def get_test_set ( self ) : <EOL> """<STR_LIT>""" <EOL> args = { } <EOL> args . update ( self . args ) <EOL> del args [ '<STR_LIT>' ] <EOL> args [ '<STR_LIT>' ] = '<STR_LIT:test>' <EOL> args [ '<STR_LIT:start>' ] = None <EOL> args [ '<STR_LIT>' ] = None <EOL> args [ '<STR_LIT>' ] = args [ '<STR_LIT>' ] <EOL> args [ '<STR_LIT>' ] = None <EOL> return MNIST ( ** args ) <EOL> class MNIST_rotated_background ( dense_design_matrix . DenseDesignMatrix ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , which_set , center = False ) : <EOL> path = "<STR_LIT>" + which_set <EOL> obj = serial . load ( path ) <EOL> X = obj [ '<STR_LIT:data>' ] <EOL> X = N . cast [ '<STR_LIT>' ] ( X ) <EOL> y = N . asarray ( obj [ '<STR_LIT>' ] ) <EOL> if center : <EOL> X -= X . mean ( axis = <NUM_LIT:0> ) <EOL> view_converter = dense_design_matrix . DefaultViewConverter ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> super ( MNIST_rotated_background , self ) . __init__ ( <EOL> X = X , y = y , y_labels = <NUM_LIT:10> , view_converter = view_converter ) <EOL> assert not N . any ( N . isnan ( self . X ) ) </s>
