<s> from __future__ import print_function <EOL> import nose <EOL> import unittest <EOL> import numpy as np <EOL> from theano . compat . six . moves import xrange <EOL> import theano <EOL> from . localdot import LocalDot <EOL> from . . test_matrixmul import SymbolicSelfTestMixin <EOL> class TestLocalDot32x32 ( unittest . TestCase , SymbolicSelfTestMixin ) : <EOL> channels = <NUM_LIT:3> <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT:32> , <NUM_LIT:32> ) <EOL> ksize = <NUM_LIT:5> <EOL> nkern_per_group = <NUM_LIT:16> <EOL> subsample_stride = <NUM_LIT:1> <EOL> ngroups = <NUM_LIT:1> <EOL> def rand ( self , shp ) : <EOL> return np . random . rand ( * shp ) . astype ( '<STR_LIT>' ) <EOL> def setUp ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> assert self . imshp [ <NUM_LIT:0> ] == self . imshp [ <NUM_LIT:1> ] <EOL> fModulesR = ( self . imshp [ <NUM_LIT:0> ] - self . ksize + <NUM_LIT:1> ) // self . subsample_stride <EOL> fModulesC = fModulesR <EOL> self . fshape = ( fModulesR , fModulesC , self . channels // self . ngroups , <EOL> self . ksize , self . ksize , self . ngroups , self . nkern_per_group ) <EOL> self . ishape = ( self . ngroups , self . channels // self . ngroups , <EOL> self . imshp [ <NUM_LIT:0> ] , self . imshp [ <NUM_LIT:1> ] , self . bsize ) <EOL> self . hshape = ( self . ngroups , self . nkern_per_group , fModulesR , fModulesC , <EOL> self . bsize ) <EOL> filters = theano . shared ( self . rand ( self . fshape ) ) <EOL> self . A = LocalDot ( filters , self . imshp [ <NUM_LIT:0> ] , self . imshp [ <NUM_LIT:1> ] , <EOL> subsample = ( self . subsample_stride , self . subsample_stride ) ) <EOL> self . xlval = self . rand ( ( self . hshape [ - <NUM_LIT:1> ] , ) + self . hshape [ : - <NUM_LIT:1> ] ) <EOL> self . xrval = self . rand ( self . ishape ) <EOL> self . xl = theano . shared ( self . xlval ) <EOL> self . xr = theano . shared ( self . xrval ) <EOL> class TestLocalDotLargeGray ( TestLocalDot32x32 ) : <EOL> channels = <NUM_LIT:1> <EOL> bsize = <NUM_LIT> <EOL> imshp = ( <NUM_LIT> , <NUM_LIT> ) <EOL> ksize = <NUM_LIT:9> <EOL> nkern_per_group = <NUM_LIT:16> <EOL> subsample_stride = <NUM_LIT:2> <EOL> ngroups = <NUM_LIT:1> <EOL> n_patches = <NUM_LIT> <EOL> def rand ( self , shp ) : <EOL> return np . random . rand ( * shp ) . astype ( '<STR_LIT>' ) <EOL> def run_autoencoder ( <EOL> self , <EOL> n_train_iter = <NUM_LIT> , <EOL> rf_shape = ( <NUM_LIT:9> , <NUM_LIT:9> ) , <EOL> n_filters = <NUM_LIT> , <EOL> dtype = '<STR_LIT>' , <EOL> module_stride = <NUM_LIT:2> , <EOL> lr = <NUM_LIT> , <EOL> show_filters = True , <EOL> ) : <EOL> if show_filters : <EOL> import matplotlib . pyplot as plt <EOL> try : <EOL> import skdata . vanhateren . dataset <EOL> except ImportError : <EOL> raise nose . SkipTest ( ) <EOL> print ( '<STR_LIT>' ) <EOL> n_images = <NUM_LIT:50> <EOL> vh = skdata . vanhateren . dataset . Calibrated ( n_images ) <EOL> patches = vh . raw_patches ( ( self . n_patches , ) + self . imshp , <EOL> items = vh . meta [ : n_images ] , <EOL> rng = np . random . RandomState ( <NUM_LIT> ) , <EOL> ) <EOL> patches = patches . astype ( '<STR_LIT>' ) <EOL> patches /= patches . reshape ( self . n_patches , self . imshp [ <NUM_LIT:0> ] * self . imshp [ <NUM_LIT:1> ] ) . max ( axis = <NUM_LIT:1> ) [ : , None , None ] <EOL> if <NUM_LIT:0> and show_filters : <EOL> plt . subplot ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ) ; plt . imshow ( patches [ <NUM_LIT:0> ] , cmap = '<STR_LIT>' ) <EOL> plt . subplot ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) ; plt . imshow ( patches [ <NUM_LIT:1> ] , cmap = '<STR_LIT>' ) <EOL> plt . subplot ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ) ; plt . imshow ( patches [ <NUM_LIT:2> ] , cmap = '<STR_LIT>' ) <EOL> plt . subplot ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ) ; plt . imshow ( patches [ <NUM_LIT:3> ] , cmap = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> patches5 = patches [ : , : , : , None , None ] . transpose ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> print ( '<STR_LIT>' , patches . shape , self . n_patches , patches5 . shape ) <EOL> print ( '<STR_LIT>' ) <EOL> hid = theano . tensor . tanh ( self . A . rmul ( self . xl ) ) <EOL> out = self . A . rmul_T ( hid ) <EOL> cost = ( ( out - self . xl ) ** <NUM_LIT:2> ) . sum ( ) <EOL> params = self . A . params ( ) <EOL> gparams = theano . tensor . grad ( cost , params ) <EOL> train_updates = [ ( p , p - lr / self . bsize * gp ) <EOL> for ( p , gp ) in zip ( params , gparams ) ] <EOL> if <NUM_LIT:1> : <EOL> train_fn = theano . function ( [ ] , [ cost ] , updates = train_updates ) <EOL> else : <EOL> train_fn = theano . function ( [ ] , [ ] , updates = train_updates ) <EOL> theano . printing . debugprint ( train_fn ) <EOL> params [ <NUM_LIT:0> ] . set_value ( <NUM_LIT> * params [ <NUM_LIT:0> ] . get_value ( ) ) <EOL> for ii in xrange ( <NUM_LIT:0> , self . n_patches , self . bsize ) : <EOL> self . xl . set_value ( patches5 [ : , : , : , : , ii : ii + self . bsize ] , borrow = True ) <EOL> cost_ii , = train_fn ( ) <EOL> print ( '<STR_LIT>' , ii , cost_ii ) <EOL> if <NUM_LIT:0> and show_filters : <EOL> self . A . imshow_gray ( ) <EOL> plt . show ( ) <EOL> assert cost_ii < <NUM_LIT:0> </s>
