<s> from __future__ import division , print_function , unicode_literals <EOL> from collections import OrderedDict <EOL> from brainstorm . handlers . base_handler import Handler <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 , flatten_time_and_features <EOL> def L2Decay ( name = None ) : <EOL> """<STR_LIT>""" <EOL> return ConstructionWrapper . create ( L2DecayLayerImpl , name = name ) <EOL> class L2DecayLayerImpl ( Layer ) : <EOL> expected_inputs = { '<STR_LIT:default>' : StructureTemplate ( '<STR_LIT:T>' , '<STR_LIT:B>' , '<STR_LIT>' ) } <EOL> expected_kwargs = { } <EOL> def setup ( self , kwargs , in_shapes ) : <EOL> outputs = OrderedDict ( ) <EOL> outputs [ '<STR_LIT>' ] = BufferStructure ( '<STR_LIT:T>' , '<STR_LIT:B>' , <NUM_LIT:1> ) <EOL> parameters = OrderedDict ( ) <EOL> internals = OrderedDict ( ) <EOL> internals [ '<STR_LIT>' ] = in_shapes [ '<STR_LIT:default>' ] <EOL> internals [ '<STR_LIT>' ] = BufferStructure ( <EOL> * in_shapes [ '<STR_LIT:default>' ] . shape , is_backward_only = True ) <EOL> return outputs , parameters , internals <EOL> def forward_pass ( self , buffers , training_pass = True ) : <EOL> _h = self . handler <EOL> assert isinstance ( _h , Handler ) <EOL> inputs = buffers . inputs . default <EOL> tmp = buffers . internals . tmp <EOL> outputs = buffers . outputs . loss <EOL> flat_inputs = flatten_time_and_features ( inputs ) <EOL> flat_tmp = flatten_time_and_features ( tmp ) <EOL> flat_outputs = flatten_time ( outputs ) <EOL> _h . mult_tt ( flat_inputs , flat_inputs , flat_tmp ) <EOL> _h . mult_st ( <NUM_LIT:0.5> , flat_tmp , flat_tmp ) <EOL> _h . sum_t ( flat_tmp , <NUM_LIT:1> , flat_outputs ) <EOL> def backward_pass ( self , buffers ) : <EOL> _h = self . handler <EOL> assert isinstance ( _h , Handler ) <EOL> inputs = buffers . inputs . default <EOL> tmp = buffers . internals . tmp <EOL> output_deltas = buffers . output_deltas . loss <EOL> input_deltas = buffers . input_deltas . default <EOL> flat_inputs = flatten_time_and_features ( inputs ) <EOL> flat_tmp = flatten_time_and_features ( tmp ) <EOL> flat_output_deltas = flatten_time ( output_deltas ) <EOL> flat_input_deltas = flatten_time_and_features ( input_deltas ) <EOL> _h . mult_mv ( flat_inputs , flat_output_deltas , flat_tmp ) <EOL> _h . add_tt ( flat_tmp , flat_input_deltas , flat_input_deltas ) </s>
