<s> from six . moves import xrange <EOL> import numpy as N <EOL> import theano <EOL> from theano . tensor import basic as T <EOL> from theano . misc import strutil <EOL> from theano . gradient import grad_undefined <EOL> from theano . gradient import DisconnectedType <EOL> class ConvGrad3D ( theano . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:1> , ) <EOL> def make_node ( self , V , d , WShape , dCdH ) : <EOL> V_ = T . as_tensor_variable ( V ) <EOL> d_ = T . as_tensor_variable ( d ) <EOL> WShape_ = T . as_tensor_variable ( WShape ) <EOL> dCdH_ = T . as_tensor_variable ( dCdH ) <EOL> return theano . Apply ( self , <EOL> inputs = [ V_ , d_ , WShape_ , dCdH_ ] , <EOL> outputs = [ T . TensorType ( <EOL> V_ . dtype , <EOL> ( False , False , False , False , False ) ) ( ) ] ) <EOL> def infer_shape ( self , node , input_shapes ) : <EOL> V , d , W_shape , dCdH = node . inputs <EOL> return [ ( W_shape [ <NUM_LIT:0> ] , W_shape [ <NUM_LIT:1> ] , W_shape [ <NUM_LIT:2> ] , W_shape [ <NUM_LIT:3> ] , W_shape [ <NUM_LIT:4> ] ) ] <EOL> def connection_pattern ( self , node ) : <EOL> return [ [ True ] , [ True ] , [ False ] , [ True ] ] <EOL> def grad ( self , inputs , output_gradients ) : <EOL> C , d , WShape , B = inputs <EOL> dLdA , = output_gradients <EOL> z = T . zeros_like ( C [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , : ] ) <EOL> dLdC = theano . tensor . nnet . convTransp3D ( dLdA , z , d , B , C . shape [ <NUM_LIT:1> : <NUM_LIT:4> ] ) <EOL> dLdd = grad_undefined ( self , <NUM_LIT:1> , d ) <EOL> dLdWShape = DisconnectedType ( ) ( ) <EOL> dLdB = theano . tensor . nnet . conv3D ( C , dLdA , T . zeros_like ( B [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , : ] ) , d ) <EOL> return [ dLdC , dLdd , dLdWShape , dLdB ] <EOL> def perform ( self , node , inputs , output_storage ) : <EOL> V , d , WShape , dCdH = inputs <EOL> batchSize = dCdH . shape [ <NUM_LIT:0> ] <EOL> outputHeight = dCdH . shape [ <NUM_LIT:1> ] <EOL> outputWidth = dCdH . shape [ <NUM_LIT:2> ] <EOL> outputDur = dCdH . shape [ <NUM_LIT:3> ] <EOL> assert V . shape [ <NUM_LIT:0> ] == batchSize <EOL> dr , dc , dt = d <EOL> dCdW = N . zeros ( WShape , dtype = V . dtype ) <EOL> for k in xrange ( <NUM_LIT:0> , WShape [ <NUM_LIT:1> ] ) : <EOL> for l in xrange ( <NUM_LIT:0> , WShape [ <NUM_LIT:2> ] ) : <EOL> for m in xrange ( <NUM_LIT:0> , WShape [ <NUM_LIT:3> ] ) : <EOL> for i in xrange ( <NUM_LIT:0> , batchSize ) : <EOL> for p in xrange ( <NUM_LIT:0> , outputHeight ) : <EOL> for q in xrange ( <NUM_LIT:0> , outputWidth ) : <EOL> for r in xrange ( <NUM_LIT:0> , outputDur ) : <EOL> for j in xrange ( <NUM_LIT:0> , WShape [ <NUM_LIT:0> ] ) : <EOL> for z in xrange ( <NUM_LIT:0> , WShape [ <NUM_LIT:4> ] ) : <EOL> dCdW [ j , k , l , m , z ] += ( <EOL> dCdH [ i , p , q , r , j ] * <EOL> V [ i , dr * p + k , dc * q + l , <EOL> dt * r + m , z ] ) <EOL> output_storage [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = dCdW <EOL> def c_code ( self , node , nodename , inputs , outputs , sub ) : <EOL> V , d , WShape , dCdH = inputs <EOL> fail = sub [ '<STR_LIT>' ] <EOL> dCdW = outputs [ <NUM_LIT:0> ] <EOL> codeSource = """<STR_LIT>""" <EOL> return strutil . render_string ( codeSource , locals ( ) ) <EOL> convGrad3D = ConvGrad3D ( ) </s>
