<s> from __future__ import division , print_function , unicode_literals <EOL> from collections import OrderedDict <EOL> from brainstorm . layers . base_layer import Layer <EOL> from brainstorm . structure . buffer_structure import ( BufferStructure , <EOL> StructureTemplate ) <EOL> from brainstorm . structure . construction import ConstructionWrapper <EOL> from brainstorm . utils import flatten_time <EOL> def Convolution2D ( num_filters , kernel_size , stride = ( <NUM_LIT:1> , <NUM_LIT:1> ) , padding = <NUM_LIT:0> , <EOL> activation = '<STR_LIT>' , name = None ) : <EOL> """<STR_LIT>""" <EOL> return ConstructionWrapper . create ( Convolution2DLayerImpl , <EOL> num_filters = num_filters , <EOL> kernel_size = kernel_size , <EOL> stride = stride , <EOL> padding = padding , <EOL> activation = activation , <EOL> name = name ) <EOL> class Convolution2DLayerImpl ( Layer ) : <EOL> expected_inputs = { '<STR_LIT:default>' : StructureTemplate ( '<STR_LIT:T>' , '<STR_LIT:B>' , '<STR_LIT>' ) } <EOL> expected_kwargs = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' } <EOL> def setup ( self , kwargs , in_shapes ) : <EOL> self . activation = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in kwargs , "<STR_LIT>" "<STR_LIT>" <EOL> assert '<STR_LIT>' in kwargs , "<STR_LIT>" "<STR_LIT>" <EOL> num_filters = kwargs [ '<STR_LIT>' ] <EOL> kernel_size = kwargs [ '<STR_LIT>' ] <EOL> stride = kwargs . get ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> padding = kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> assert type ( padding ) is int and padding >= <NUM_LIT:0> , "<STR_LIT>" . format ( padding ) <EOL> assert type ( kernel_size ) in [ list , tuple ] and len ( kernel_size ) == <NUM_LIT:2> , "<STR_LIT>" "<STR_LIT>" . format ( kernel_size ) <EOL> assert type ( stride ) in [ list , tuple ] and len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> in_shape = self . in_shapes [ '<STR_LIT:default>' ] . feature_shape <EOL> assert stride [ <NUM_LIT:0> ] >= <NUM_LIT:0> and stride [ <NUM_LIT:1> ] >= <NUM_LIT:0> , "<STR_LIT>" . format ( stride ) <EOL> assert isinstance ( in_shape , tuple ) and len ( in_shape ) == <NUM_LIT:3> , "<STR_LIT>" "<STR_LIT>" . format ( in_shape ) <EOL> self . num_filters = num_filters <EOL> self . kernel_size = tuple ( kernel_size ) <EOL> self . stride = tuple ( stride ) <EOL> self . padding = padding <EOL> kernel_x , kernel_y = self . kernel_size <EOL> num_input_maps = in_shape [ <NUM_LIT:2> ] <EOL> output_height = ( ( in_shape [ <NUM_LIT:0> ] + <NUM_LIT:2> * padding - kernel_x ) // <EOL> stride [ <NUM_LIT:0> ] ) + <NUM_LIT:1> <EOL> output_width = ( ( in_shape [ <NUM_LIT:1> ] + <NUM_LIT:2> * padding - kernel_y ) // <EOL> stride [ <NUM_LIT:1> ] ) + <NUM_LIT:1> <EOL> out_shape = ( output_height , output_width , num_filters ) <EOL> outputs = OrderedDict ( ) <EOL> outputs [ '<STR_LIT:default>' ] = BufferStructure ( '<STR_LIT:T>' , '<STR_LIT:B>' , * out_shape ) <EOL> parameters = OrderedDict ( ) <EOL> parameters [ '<STR_LIT>' ] = BufferStructure ( num_filters , kernel_x , kernel_y , <EOL> num_input_maps ) <EOL> parameters [ '<STR_LIT>' ] = BufferStructure ( num_filters ) <EOL> internals = OrderedDict ( ) <EOL> return outputs , parameters , internals <EOL> def forward_pass ( self , buffers , training_pass = True ) : <EOL> _h = self . handler <EOL> W , bias = buffers . parameters <EOL> inputs = buffers . inputs . default <EOL> outputs = buffers . outputs . default <EOL> flat_inputs = flatten_time ( inputs ) <EOL> flat_outputs = flatten_time ( outputs ) <EOL> _h . conv2d_forward_batch ( flat_inputs , W , bias , flat_outputs , <EOL> self . padding , self . stride ) <EOL> _h . inplace_act_func [ self . activation ] ( outputs ) <EOL> def backward_pass ( self , buffers ) : <EOL> _h = self . handler <EOL> W , bias = buffers . parameters <EOL> dW , dbias = buffers . gradients <EOL> inputs = buffers . inputs . default <EOL> outputs = buffers . outputs . default <EOL> in_deltas = buffers . input_deltas . default <EOL> out_deltas = buffers . output_deltas . default <EOL> flat_inputs = flatten_time ( inputs ) <EOL> flat_in_deltas = flatten_time ( in_deltas ) <EOL> flat_out_deltas = flatten_time ( out_deltas ) <EOL> _h . inplace_act_func_deriv [ self . activation ] ( outputs , out_deltas ) <EOL> _h . conv2d_backward_batch ( flat_inputs , W , self . padding , self . stride , <EOL> flat_in_deltas , flat_out_deltas , dW , dbias ) </s>
