<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import numpy <EOL> from theano . compat . six . moves import xrange <EOL> import theano <EOL> try : <EOL> grad_not_implemented = theano . gradient . grad_not_implemented <EOL> except : <EOL> def grad_not_implemented ( op , idx , ipt ) : <EOL> return None <EOL> def any_symbolic ( * args ) : <EOL> """<STR_LIT>""" <EOL> for a in args : <EOL> if isinstance ( a , theano . Variable ) : <EOL> return True <EOL> return False <EOL> def not_symbolic ( * args ) : <EOL> return not any_symbolic ( * args ) <EOL> class Base ( theano . Op ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> module_stride = <NUM_LIT:1> , <EOL> ) : <EOL> self . module_stride = module_stride <EOL> def _attributes ( self ) : <EOL> return ( <EOL> self . module_stride , <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> return ( type ( self ) == type ( other ) <EOL> and self . _attributes ( ) == other . _attributes ( ) ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( type ( self ) , self . _attributes ( ) ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . module_stride , <EOL> ) <EOL> class FilterActs ( Base ) : <EOL> """<STR_LIT>""" <EOL> def make_node ( self , images , filters ) : <EOL> images = theano . tensor . as_tensor_variable ( images ) <EOL> filters = theano . tensor . as_tensor_variable ( filters ) <EOL> ibcast = images . broadcastable <EOL> fbcast = filters . broadcastable <EOL> igroups , icolors_per_group , irows , icols , icount = ibcast <EOL> fmodulesR , fmodulesC , fcolors , frows , fcols = fbcast [ : - <NUM_LIT:2> ] <EOL> fgroups , filters_per_group = fbcast [ - <NUM_LIT:2> : ] <EOL> hbcast = ( fgroups , filters_per_group , fmodulesR , fmodulesC , icount ) <EOL> htype = theano . tensor . TensorType ( <EOL> dtype = images . dtype , <EOL> broadcastable = hbcast ) <EOL> if images . dtype != filters . dtype : <EOL> raise TypeError ( '<STR_LIT>' , ( images , images . dtype , filters , <EOL> filters . dtype ) ) <EOL> return theano . gof . Apply ( self , <EOL> [ images , filters ] , <EOL> [ htype ( ) ] ) <EOL> def perform ( self , node , iargs , ostor ) : <EOL> images , filters = iargs <EOL> igroups , icolors_per_group , irows , icols , icount = images . shape <EOL> fmodulesR , fmodulesC , fcolors , frows , fcols = filters . shape [ : - <NUM_LIT:2> ] <EOL> fgroups , filters_per_group = filters . shape [ - <NUM_LIT:2> : ] <EOL> hshape = self . infer_shape ( node , ( images . shape , filters . shape ) ) [ <NUM_LIT:0> ] <EOL> hidacts = numpy . zeros ( hshape , dtype = images . dtype ) <EOL> for mR in xrange ( fmodulesR ) : <EOL> for mC in xrange ( fmodulesC ) : <EOL> for gg in xrange ( igroups ) : <EOL> img_r_offset = mR * self . module_stride <EOL> img_c_offset = mC * self . module_stride <EOL> rc_images = images [ gg , : , <EOL> img_r_offset : img_r_offset + frows , <EOL> img_c_offset : img_c_offset + fcols , <EOL> : ] <EOL> rc_filters = filters [ mR , mC , : , : , : , gg , : ] <EOL> left_arg = rc_filters . reshape ( - <NUM_LIT:1> , filters_per_group ) . T <EOL> right_arg = rc_images . reshape ( - <NUM_LIT:1> , icount ) <EOL> try : <EOL> rc_hidacts = numpy . dot ( left_arg , right_arg ) <EOL> except ValueError as e : <EOL> if '<STR_LIT>' in str ( e ) : <EOL> raise ValueError ( "<STR_LIT>" + str ( left_arg . shape ) + '<STR_LIT>' + str ( right_arg . shape ) ) <EOL> else : <EOL> raise <EOL> hidacts [ gg , : , mR , mC , : ] = rc_hidacts <EOL> ostor [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = hidacts <EOL> print_sizes = <NUM_LIT:0> <EOL> if print_sizes : <EOL> print ( '<STR_LIT>' , images . shape ) <EOL> print ( '<STR_LIT>' , filters . shape ) <EOL> print ( '<STR_LIT>' , hidacts . shape ) <EOL> def grad ( self , inputs , goutputs ) : <EOL> images , filters = inputs <EOL> frows = filters . shape [ <NUM_LIT:3> ] <EOL> fcols = filters . shape [ <NUM_LIT:4> ] <EOL> irows = images . shape [ <NUM_LIT:2> ] <EOL> icols = images . shape [ <NUM_LIT:3> ] <EOL> hidacts = goutputs [ <NUM_LIT:0> ] <EOL> if filters . dtype == '<STR_LIT>' and hidacts . dtype == '<STR_LIT>' : <EOL> filters = theano . tensor . cast ( filters , '<STR_LIT>' ) <EOL> gimages = ImgActs ( module_stride = self . module_stride ) ( <EOL> filters , hidacts , irows , icols ) <EOL> if images . dtype == '<STR_LIT>' and hidacts . dtype == '<STR_LIT>' : <EOL> images = theano . tensor . cast ( images , '<STR_LIT>' ) <EOL> gfilters = WeightActs ( module_stride = self . module_stride ) ( <EOL> images , hidacts , frows , fcols ) <EOL> return [ gimages , gfilters ] <EOL> def infer_shape ( self , node , shapes ) : <EOL> ishape , fshape = shapes <EOL> igroups , icolors_per_group , irows , icols , icount = ishape <EOL> fmodulesR , fmodulesC , fcolors , frows , fcols = fshape [ : - <NUM_LIT:2> ] <EOL> fgroups , filters_per_group = fshape [ - <NUM_LIT:2> : ] <EOL> if not any_symbolic ( irows , icols ) and irows != icols : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> ( irows , icols ) ) <EOL> if not any_symbolic ( frows , fcols ) and frows != fcols : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> ( frows , fcols ) ) <EOL> if not any_symbolic ( fmodulesR , fmodulesC ) and fmodulesR != fmodulesC : <EOL> raise ValueError ( '<STR_LIT>' , <EOL> ( fmodulesR , fmodulesC ) ) <EOL> if ( not any_symbolic ( icolors_per_group , fcolors ) <EOL> and icolors_per_group != fcolors ) : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> ( icolors_per_group , fcolors ) ) <EOL> hshape = ( fgroups , filters_per_group , fmodulesR , fmodulesC , icount ) <EOL> return [ hshape ] <EOL> class WeightActs ( Base ) : <EOL> """<STR_LIT>""" <EOL> def make_node ( self , images , hidacts , frows , fcols ) : <EOL> images , hidacts , frows , fcols = map ( theano . tensor . as_tensor_variable , <EOL> [ images , hidacts , frows , fcols ] ) <EOL> if frows . dtype [ : <NUM_LIT:3> ] not in ( '<STR_LIT:int>' , '<STR_LIT>' ) : <EOL> raise TypeError ( frows ) <EOL> if fcols . dtype [ : <NUM_LIT:3> ] not in ( '<STR_LIT:int>' , '<STR_LIT>' ) : <EOL> raise TypeError ( frows ) <EOL> if frows . ndim : <EOL> raise TypeError ( '<STR_LIT>' , frows ) <EOL> if fcols . ndim : <EOL> raise TypeError ( '<STR_LIT>' , fcols ) <EOL> if images . dtype != hidacts . dtype : <EOL> raise TypeError ( '<STR_LIT>' , <EOL> ( images . dtype , hidacts . dtype ) ) <EOL> igroups , icolors , irows , icols , icount = images . type . broadcastable <EOL> hgroups , hcolors , hrows , hcols , hcount = hidacts . type . broadcastable <EOL> otype = theano . tensor . TensorType ( <EOL> dtype = images . dtype , <EOL> broadcastable = ( hrows , hcols , icolors , <EOL> False , False , hgroups , hcolors ) ) <EOL> return theano . Apply ( self , <EOL> [ images , hidacts , frows , fcols ] , <EOL> [ otype ( ) ] ) <EOL> def perform ( self , node , iargs , ostor ) : <EOL> images , hidacts , frows , fcols = iargs <EOL> if frows != fcols : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( frows , fcols ) ) <EOL> hgroups , hcolors_per_group , hrows , hcols , hcount = hidacts . shape <EOL> fshape = list ( self . infer_shape ( node , <EOL> ( images . shape , hidacts . shape , ( ) , ( ) ) ) [ <NUM_LIT:0> ] ) <EOL> fcolors = fshape [ <NUM_LIT:2> ] <EOL> fshape [ <NUM_LIT:3> ] = frows <EOL> fshape [ <NUM_LIT:4> ] = fcols <EOL> filters = numpy . zeros ( fshape , dtype = images . dtype ) <EOL> for mR in xrange ( hrows ) : <EOL> for mC in xrange ( hcols ) : <EOL> for gg in xrange ( hgroups ) : <EOL> img_r_offset = mR * self . module_stride <EOL> img_c_offset = mC * self . module_stride <EOL> rc_images = images [ gg , : , <EOL> img_r_offset : img_r_offset + frows , <EOL> img_c_offset : img_c_offset + fcols , <EOL> : ] <EOL> rc_hidacts = hidacts [ gg , : , mR , mC , : ] <EOL> rc_filters = numpy . dot ( <EOL> rc_images . reshape ( - <NUM_LIT:1> , hcount ) , <EOL> rc_hidacts . T ) <EOL> filters [ mR , mC , : , : , : , gg , : ] = rc_filters . reshape ( <EOL> ( fcolors , frows , fcols , hcolors_per_group ) ) <EOL> ostor [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = filters <EOL> def grad ( self , inputs , goutputs ) : <EOL> images , hidacts , frows , fcols = inputs <EOL> gfilters , = goutputs <EOL> irows = images . shape [ <NUM_LIT:2> ] <EOL> icols = images . shape [ <NUM_LIT:3> ] <EOL> gimages = ImgActs ( module_stride = self . module_stride ) ( <EOL> gfilters , hidacts , irows , icols ) <EOL> ghidacts = FilterActs ( module_stride = self . module_stride ) ( <EOL> images , gfilters ) <EOL> return [ gimages , ghidacts , grad_not_implemented ( self , <NUM_LIT:2> , inputs [ <NUM_LIT:2> ] ) , <EOL> grad_not_implemented ( self , <NUM_LIT:3> , inputs [ <NUM_LIT:3> ] ) ] <EOL> def infer_shape ( self , node , shapes ) : <EOL> images , hidacts , frows , fcols = node . inputs <EOL> ishape , hshape , frowshp , fcolshp = shapes <EOL> igroups , icolors_per_group , irows , icols , icount = ishape <EOL> hgroups , hcolors_per_group , hrows , hcols , hcount = hshape <EOL> fmodulesR = hrows <EOL> fmodulesC = hcols <EOL> fcolors = icolors_per_group <EOL> fgroups = hgroups <EOL> filters_per_group = hcolors_per_group <EOL> fshape = ( fmodulesR , fmodulesC , fcolors , frows , fcols , fgroups , <EOL> filters_per_group ) <EOL> if not_symbolic ( irows , icols ) and irows != icols : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( irows , icols ) ) <EOL> if not_symbolic ( hrows , hcols ) and hrows != hcols : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( hrows , hcols ) ) <EOL> if not_symbolic ( icount , hcount ) and icount != hcount : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( icount , hcount ) ) <EOL> if not_symbolic ( igroups , hgroups ) and igroups != hgroups : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> igroups , hgroups ) <EOL> return [ fshape ] <EOL> class ImgActs ( Base ) : <EOL> """<STR_LIT>""" <EOL> def make_node ( self , filters , hidacts , irows , icols ) : <EOL> filters , hidacts , irows , icols = map ( theano . tensor . as_tensor_variable , <EOL> [ filters , hidacts , irows , icols ] ) <EOL> if irows . dtype [ : <NUM_LIT:3> ] not in ( '<STR_LIT:int>' , '<STR_LIT>' ) : <EOL> raise TypeError ( irows ) <EOL> if icols . dtype [ : <NUM_LIT:3> ] not in ( '<STR_LIT:int>' , '<STR_LIT>' ) : <EOL> raise TypeError ( irows ) <EOL> if irows . ndim : <EOL> raise TypeError ( '<STR_LIT>' , irows ) <EOL> if icols . ndim : <EOL> raise TypeError ( '<STR_LIT>' , icols ) <EOL> if filters . ndim != <NUM_LIT:7> : <EOL> raise TypeError ( '<STR_LIT>' , filters ) <EOL> if hidacts . ndim != <NUM_LIT:5> : <EOL> raise TypeError ( '<STR_LIT>' , filters ) <EOL> if filters . dtype != hidacts . dtype : <EOL> raise TypeError ( '<STR_LIT>' , <EOL> ( filters , filters . dtype , hidacts , hidacts . dtype ) ) <EOL> return theano . gof . Apply ( self , <EOL> [ filters , hidacts , irows , icols ] , <EOL> [ hidacts . type ( ) ] ) <EOL> def perform ( self , node , iargs , ostor ) : <EOL> filters , hidacts , irows , icols = iargs <EOL> hgroups , hcolors_per_group , hrows , hcols , hcount = hidacts . shape <EOL> fmodulesR , fmodulesC , fcolors , frows , fcols = filters . shape [ : - <NUM_LIT:2> ] <EOL> fgroups , filters_per_group = filters . shape [ - <NUM_LIT:2> : ] <EOL> igroups = fgroups <EOL> icolors_per_group = fcolors <EOL> icount = hcount <EOL> if hrows != hcols : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( hrows , hcols ) ) <EOL> if frows != fcols : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( frows , fcols ) ) <EOL> if fmodulesR != fmodulesC : <EOL> raise NotImplementedError ( '<STR_LIT>' , <EOL> ( fmodulesR , fmodulesC ) ) <EOL> if hcolors_per_group != filters_per_group : <EOL> raise ValueError ( "<STR_LIT>" , <EOL> ( hcolors_per_group , filters_per_group ) ) <EOL> if irows != icols : <EOL> raise NotImplementedError ( "<STR_LIT>" , <EOL> ( irows , icols ) ) <EOL> images = numpy . zeros ( <EOL> ( igroups , icolors_per_group , irows , icols , icount ) , <EOL> dtype = hidacts . dtype ) <EOL> for mR in xrange ( fmodulesR ) : <EOL> for mC in xrange ( fmodulesC ) : <EOL> for gg in xrange ( igroups ) : <EOL> rc_filters = filters [ mR , mC , : , : , : , gg , : ] <EOL> rc_hidacts = hidacts [ gg , : , mR , mC , : ] <EOL> img_r_offset = mR * self . module_stride <EOL> img_c_offset = mC * self . module_stride <EOL> images [ gg , : , <EOL> img_r_offset : img_r_offset + frows , <EOL> img_c_offset : img_c_offset + fcols , <EOL> : ] += numpy . dot ( <EOL> rc_filters . reshape ( - <NUM_LIT:1> , filters_per_group ) , <EOL> rc_hidacts <EOL> ) . reshape ( <EOL> ( fcolors , frows , fcols , icount ) ) <EOL> ostor [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = images <EOL> def grad ( self , inputs , goutputs ) : <EOL> filters , hidacts , irows , icols = inputs <EOL> gimages , = goutputs <EOL> frows = filters . shape [ <NUM_LIT:3> ] <EOL> fcols = filters . shape [ <NUM_LIT:4> ] <EOL> gfilters = WeightActs ( module_stride = self . module_stride ) ( <EOL> gimages , hidacts , frows , fcols ) <EOL> ghidacts = FilterActs ( module_stride = self . module_stride ) ( <EOL> gimages , filters ) <EOL> return [ gfilters , ghidacts , grad_not_implemented ( self , <NUM_LIT:2> , inputs [ <NUM_LIT:2> ] ) , <EOL> grad_not_implemented ( self , <NUM_LIT:3> , inputs [ <NUM_LIT:3> ] ) ] </s>
