<s> from __future__ import absolute_import , print_function , division <EOL> import theano <EOL> from theano . gradient import DisconnectedType <EOL> from theano . gof import Op , Apply , TopoOptimizer <EOL> from theano . gof . opt import copy_stack_trace <EOL> from theano import tensor <EOL> import theano . sandbox . cuda as cuda <EOL> def get_diagonal_subtensor_view ( x , i0 , i1 ) : <EOL> """<STR_LIT>""" <EOL> i0 = int ( i0 ) <EOL> i1 = int ( i1 ) <EOL> if x . shape [ i0 ] < x . shape [ i1 ] : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> idx = [ slice ( None ) ] * x . ndim <EOL> idx [ i0 ] = slice ( x . shape [ i1 ] - <NUM_LIT:1> , None , None ) <EOL> xview = x . __getitem__ ( tuple ( idx ) ) <EOL> strides = list ( xview . strides ) <EOL> strides [ i1 ] -= strides [ i0 ] <EOL> xview . strides = strides <EOL> return xview <EOL> class DiagonalSubtensor ( Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __str__ ( self ) : <EOL> if self . inplace : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> return "<STR_LIT:%s>" % self . __class__ . __name__ <EOL> def __init__ ( self , inplace = False ) : <EOL> self . inplace = inplace <EOL> if inplace : <EOL> self . view_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , x , i0 , i1 ) : <EOL> _i0 = tensor . as_tensor_variable ( i0 ) <EOL> _i1 = tensor . as_tensor_variable ( i1 ) <EOL> return Apply ( self , [ x , _i0 , _i1 ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inputs , output_storage ) : <EOL> xview = get_diagonal_subtensor_view ( * inputs ) <EOL> if self . inplace : <EOL> output_storage [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = xview <EOL> else : <EOL> output_storage [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = xview . copy ( ) <EOL> def grad ( self , inputs , g_outputs ) : <EOL> z = tensor . zeros_like ( inputs [ <NUM_LIT:0> ] ) <EOL> gx = inc_diagonal_subtensor ( z , inputs [ <NUM_LIT:1> ] , inputs [ <NUM_LIT:2> ] , g_outputs [ <NUM_LIT:0> ] ) <EOL> return [ gx , DisconnectedType ( ) ( ) , DisconnectedType ( ) ( ) ] <EOL> def connection_pattern ( self , node ) : <EOL> rval = [ [ True ] , [ False ] , [ False ] ] <EOL> return rval <EOL> diagonal_subtensor = DiagonalSubtensor ( False ) <EOL> class IncDiagonalSubtensor ( Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __str__ ( self ) : <EOL> if self . inplace : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> return "<STR_LIT:%s>" % self . __class__ . __name__ <EOL> def __init__ ( self , inplace = False ) : <EOL> self . inplace = inplace <EOL> if inplace : <EOL> self . destroy_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , x , i0 , i1 , amt ) : <EOL> _i0 = tensor . as_tensor_variable ( i0 ) <EOL> _i1 = tensor . as_tensor_variable ( i1 ) <EOL> return Apply ( self , [ x , _i0 , _i1 , amt ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inputs , output_storage ) : <EOL> x , i0 , i1 , amt = inputs <EOL> if not self . inplace : <EOL> x = x . copy ( ) <EOL> xview = get_diagonal_subtensor_view ( x , i0 , i1 ) <EOL> xview += amt <EOL> output_storage [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = x <EOL> def grad ( self , inputs , g_outputs ) : <EOL> x , i0 , i1 , amt = inputs <EOL> gy = g_outputs [ <NUM_LIT:0> ] <EOL> return [ gy , DisconnectedType ( ) ( ) , DisconnectedType ( ) ( ) , <EOL> diagonal_subtensor ( gy , i0 , i1 ) ] <EOL> def connection_pattern ( self , node ) : <EOL> rval = [ [ True ] , [ False ] , [ False ] , [ True ] ] <EOL> return rval <EOL> inc_diagonal_subtensor = IncDiagonalSubtensor ( False ) <EOL> def conv3d ( signals , filters , <EOL> signals_shape = None , filters_shape = None , <EOL> border_mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( border_mode , str ) : <EOL> border_mode = ( border_mode , border_mode , border_mode ) <EOL> if signals_shape is None : <EOL> _signals_shape_5d = signals . shape <EOL> else : <EOL> _signals_shape_5d = signals_shape <EOL> if filters_shape is None : <EOL> _filters_shape_5d = filters . shape <EOL> else : <EOL> _filters_shape_5d = filters_shape <EOL> _signals_shape_4d = ( <EOL> _signals_shape_5d [ <NUM_LIT:0> ] * _signals_shape_5d [ <NUM_LIT:1> ] , <EOL> _signals_shape_5d [ <NUM_LIT:2> ] , <EOL> _signals_shape_5d [ <NUM_LIT:3> ] , <EOL> _signals_shape_5d [ <NUM_LIT:4> ] , <EOL> ) <EOL> _filters_shape_4d = ( <EOL> _filters_shape_5d [ <NUM_LIT:0> ] * _filters_shape_5d [ <NUM_LIT:1> ] , <EOL> _filters_shape_5d [ <NUM_LIT:2> ] , <EOL> _filters_shape_5d [ <NUM_LIT:3> ] , <EOL> _filters_shape_5d [ <NUM_LIT:4> ] , <EOL> ) <EOL> if border_mode [ <NUM_LIT:1> ] != border_mode [ <NUM_LIT:2> ] : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> conv2d_signal_shape = _signals_shape_4d <EOL> conv2d_filter_shape = _filters_shape_4d <EOL> if signals_shape is None : <EOL> conv2d_signal_shape = None <EOL> if filters_shape is None : <EOL> conv2d_filter_shape = None <EOL> out_4d = tensor . nnet . conv2d ( <EOL> signals . reshape ( _signals_shape_4d ) , <EOL> filters . reshape ( _filters_shape_4d ) , <EOL> input_shape = conv2d_signal_shape , <EOL> filter_shape = conv2d_filter_shape , <EOL> border_mode = border_mode [ <NUM_LIT:1> ] ) <EOL> if border_mode [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> out_tmp = out_4d . reshape ( ( <EOL> _signals_shape_5d [ <NUM_LIT:0> ] , <EOL> _signals_shape_5d [ <NUM_LIT:1> ] , <EOL> _filters_shape_5d [ <NUM_LIT:0> ] , <EOL> _filters_shape_5d [ <NUM_LIT:1> ] , <EOL> _signals_shape_5d [ <NUM_LIT:3> ] - _filters_shape_5d [ <NUM_LIT:3> ] + <NUM_LIT:1> , <EOL> _signals_shape_5d [ <NUM_LIT:4> ] - _filters_shape_5d [ <NUM_LIT:4> ] + <NUM_LIT:1> , <EOL> ) ) <EOL> elif border_mode [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> out_tmp = out_4d . reshape ( ( <EOL> _signals_shape_5d [ <NUM_LIT:0> ] , <EOL> _signals_shape_5d [ <NUM_LIT:1> ] , <EOL> _filters_shape_5d [ <NUM_LIT:0> ] , <EOL> _filters_shape_5d [ <NUM_LIT:1> ] , <EOL> _signals_shape_5d [ <NUM_LIT:3> ] + _filters_shape_5d [ <NUM_LIT:3> ] - <NUM_LIT:1> , <EOL> _signals_shape_5d [ <NUM_LIT:4> ] + _filters_shape_5d [ <NUM_LIT:4> ] - <NUM_LIT:1> , <EOL> ) ) <EOL> elif border_mode [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> raise NotImplementedError ( ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' , border_mode [ <NUM_LIT:1> ] ) <EOL> if border_mode [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> if _filters_shape_5d [ <NUM_LIT:1> ] != <NUM_LIT:1> : <EOL> out_5d = diagonal_subtensor ( out_tmp , <NUM_LIT:1> , <NUM_LIT:3> ) . sum ( axis = <NUM_LIT:3> ) <EOL> else : <EOL> out_5d = out_tmp . reshape ( ( <EOL> _signals_shape_5d [ <NUM_LIT:0> ] , <EOL> _signals_shape_5d [ <NUM_LIT:1> ] , <EOL> _filters_shape_5d [ <NUM_LIT:0> ] , <EOL> _signals_shape_5d [ <NUM_LIT:3> ] - _filters_shape_5d [ <NUM_LIT:3> ] + <NUM_LIT:1> , <EOL> _signals_shape_5d [ <NUM_LIT:4> ] - _filters_shape_5d [ <NUM_LIT:4> ] + <NUM_LIT:1> , <EOL> ) ) <EOL> elif border_mode [ <NUM_LIT:0> ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' , border_mode [ <NUM_LIT:0> ] ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' , border_mode [ <NUM_LIT:1> ] ) <EOL> return out_5d <EOL> def make_gpu_optimizer ( op , to_gpu ) : <EOL> """<STR_LIT>""" <EOL> @ theano . gof . local_optimizer ( [ op , cuda . gpu_from_host ] ) <EOL> def local_to_gpu ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , op ) : <EOL> if any ( node . inputs [ idx ] . owner and <EOL> isinstance ( node . inputs [ idx ] . owner . op , cuda . HostFromGpu ) <EOL> for idx in to_gpu ) : <EOL> new_inp = list ( node . inputs ) <EOL> for idx in to_gpu : <EOL> new_inp [ idx ] = cuda . gpu_from_host ( new_inp [ idx ] ) <EOL> result_node = op ( ) ( * new_inp ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , result_node ) <EOL> transfer_node = cuda . host_from_gpu ( result_node ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , transfer_node ) <EOL> return [ transfer_node ] <EOL> if node . op == cuda . gpu_from_host : <EOL> host_input = node . inputs [ <NUM_LIT:0> ] <EOL> if host_input . owner and isinstance ( host_input . owner . op , <EOL> op ) : <EOL> op_node = host_input . owner <EOL> new_inp = list ( op_node . inputs ) <EOL> for idx in to_gpu : <EOL> new_inp [ idx ] = cuda . gpu_from_host ( new_inp [ idx ] ) <EOL> new_node = op ( ) ( * new_inp ) <EOL> copy_stack_trace ( host_input , new_node ) <EOL> return [ new_node ] <EOL> return False <EOL> local_to_gpu . __name__ = "<STR_LIT>" + op . __name__ <EOL> cuda . opt . register_opt ( ) ( local_to_gpu ) <EOL> if cuda . cuda_available : <EOL> make_gpu_optimizer ( DiagonalSubtensor , [ <NUM_LIT:0> ] ) <EOL> make_gpu_optimizer ( IncDiagonalSubtensor , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> @ theano . gof . local_optimizer ( [ DiagonalSubtensor , IncDiagonalSubtensor ] ) <EOL> def local_inplace_DiagonalSubtensor ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , ( DiagonalSubtensor , IncDiagonalSubtensor ) ) and <EOL> not node . op . inplace ) : <EOL> new_op = node . op . __class__ ( inplace = True ) <EOL> new_node = new_op ( * node . inputs ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , new_node ) <EOL> return [ new_node ] <EOL> return False <EOL> theano . compile . optdb . register ( <EOL> '<STR_LIT>' , <EOL> TopoOptimizer ( <EOL> local_inplace_DiagonalSubtensor , <EOL> failure_callback = TopoOptimizer . warn_inplace ) , <EOL> <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) </s>
