<s> from quagga . matrix import Matrix <EOL> from quagga . context import Context <EOL> from quagga . connector import Connector <EOL> class LstmBlock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , W , R , b , grad_clipping , x , mask , prev_c , prev_h , device_id = None ) : <EOL> self . f_context = Context ( device_id ) <EOL> device_id = self . f_context . device_id <EOL> if W . bpropagable : <EOL> self . W , self . dL_dW = W . register_usage ( device_id , device_id ) <EOL> self . W_b_context = Context ( device_id ) <EOL> else : <EOL> self . W = W . register_usage ( device_id ) <EOL> if R . bpropagable : <EOL> self . R , self . dL_dR = R . register_usage ( device_id , device_id ) <EOL> self . R_b_context = Context ( device_id ) <EOL> else : <EOL> self . R = R . register_usage ( device_id ) <EOL> if b . bpropagable : <EOL> self . b , self . dL_db = b . register_usage ( device_id , device_id ) <EOL> self . b_b_context = Context ( device_id ) <EOL> else : <EOL> self . b = b . register_usage ( device_id ) <EOL> self . grad_clipping = grad_clipping <EOL> if x . bpropagable : <EOL> self . x , self . dL_dx = x . register_usage ( device_id , device_id ) <EOL> self . x_b_context = Context ( device_id ) <EOL> else : <EOL> self . x = x . register_usage ( device_id ) <EOL> if mask : <EOL> self . mask = mask . register_usage ( device_id ) <EOL> if prev_c . bpropagable : <EOL> self . prev_c , self . dL_dprev_c = prev_c . register_usage ( device_id , device_id ) <EOL> self . prev_c_b_context = Context ( device_id ) <EOL> else : <EOL> self . prev_c = prev_c . register_usage ( device_id ) <EOL> if prev_h . bpropagable : <EOL> self . prev_h , self . dL_dprev_h = prev_h . register_usage ( device_id , device_id ) <EOL> self . prev_h_b_context = Context ( device_id ) <EOL> else : <EOL> self . prev_h = prev_h . register_usage ( device_id ) <EOL> self . learning = W . bpropagable or R . bpropagable or x . bpropagable or prev_c . bpropagable or prev_h . bpropagable <EOL> if self . learning : <EOL> self . b_context = Context ( device_id ) <EOL> dim = self . R . nrows <EOL> batch_size = self . x . nrows <EOL> self . zifo = Matrix . empty ( batch_size , <NUM_LIT:4> * dim , device_id = device_id ) <EOL> self . z = self . zifo [ : , <NUM_LIT:0> * dim : <NUM_LIT:1> * dim ] <EOL> self . i = self . zifo [ : , <NUM_LIT:1> * dim : <NUM_LIT:2> * dim ] <EOL> self . f = self . zifo [ : , <NUM_LIT:2> * dim : <NUM_LIT:3> * dim ] <EOL> self . o = self . zifo [ : , <NUM_LIT:3> * dim : <NUM_LIT:4> * dim ] <EOL> self . c = Matrix . empty_like ( self . prev_c , device_id ) <EOL> self . c = Connector ( self . c , device_id if self . learning else None ) <EOL> self . tanh_c = Matrix . empty_like ( self . c , device_id ) <EOL> self . h = Matrix . empty_like ( self . c , device_id ) <EOL> self . h = Connector ( self . h , device_id if self . learning else None ) <EOL> if self . learning : <EOL> self . _dzifo_dpre_zifo = Matrix . empty_like ( self . zifo ) <EOL> self . dz_dpre_z = self . _dzifo_dpre_zifo [ : , <NUM_LIT:0> * dim : <NUM_LIT:1> * dim ] <EOL> self . di_dpre_i = self . _dzifo_dpre_zifo [ : , <NUM_LIT:1> * dim : <NUM_LIT:2> * dim ] <EOL> self . df_dpre_f = self . _dzifo_dpre_zifo [ : , <NUM_LIT:2> * dim : <NUM_LIT:3> * dim ] <EOL> self . do_dpre_o = self . _dzifo_dpre_zifo [ : , <NUM_LIT:3> * dim : <NUM_LIT:4> * dim ] <EOL> self . dL_dpre_zifo = self . _dzifo_dpre_zifo <EOL> self . dL_dpre_z = self . dz_dpre_z <EOL> self . dL_dpre_i = self . di_dpre_i <EOL> self . dL_dpre_f = self . df_dpre_f <EOL> self . dL_dpre_o = self . do_dpre_o <EOL> self . _dtanh_c_dc = Matrix . empty_like ( self . c ) <EOL> @ property <EOL> def dzifo_dpre_zifo ( self ) : <EOL> if self . learning : <EOL> return self . _dzifo_dpre_zifo <EOL> @ property <EOL> def dtanh_c_dc ( self ) : <EOL> if self . learning : <EOL> return self . _dtanh_c_dc <EOL> def fprop ( self ) : <EOL> self . zifo . assign_dot ( self . f_context , self . x , self . W ) <EOL> self . zifo . add_dot ( self . f_context , self . prev_h , self . R ) <EOL> self . zifo . add ( self . f_context , self . b ) <EOL> self . zifo . tanh_sigm ( self . f_context , self . zifo , self . dzifo_dpre_zifo , axis = <NUM_LIT:1> ) <EOL> self . c . assign_sum_hprod ( self . f_context , self . i , self . z , self . f , self . prev_c ) <EOL> self . c . tanh ( self . f_context , self . tanh_c , self . dtanh_c_dc ) <EOL> self . h . assign_hprod ( self . f_context , self . o , self . tanh_c ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . c . assign_masked_addition ( self . f_context , self . mask , self . c , self . prev_c ) <EOL> self . h . assign_masked_addition ( self . f_context , self . mask , self . h , self . prev_h ) <EOL> self . c . fprop ( ) <EOL> self . h . fprop ( ) <EOL> def bprop ( self ) : <EOL> dL_dc = self . c . backward_matrix <EOL> dL_dh = self . h . backward_matrix <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dprev_c . add_hprod_one_minus_mask ( self . prev_c_b_context , self . mask , dL_dc ) <EOL> dL_dc . hprod ( self . prev_c_b_context , self . mask ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dprev_h . add_hprod_one_minus_mask ( self . prev_h_b_context , self . mask , dL_dh ) <EOL> dL_dh . hprod ( self . prev_h_b_context , self . mask ) <EOL> dL_dc . add_hprod ( self . b_context , dL_dh , self . o , self . dtanh_c_dc ) <EOL> self . b_context . wait ( self . f_context ) <EOL> self . dL_dpre_o . assign_hprod ( self . b_context , dL_dh , self . tanh_c , self . do_dpre_o ) <EOL> self . dL_dpre_f . assign_hprod ( self . b_context , dL_dc , self . prev_c , self . df_dpre_f ) <EOL> self . dL_dpre_i . assign_hprod ( self . b_context , dL_dc , self . z , self . di_dpre_i ) <EOL> self . dL_dpre_z . assign_hprod ( self . b_context , dL_dc , self . i , self . dz_dpre_z ) <EOL> if self . grad_clipping : <EOL> self . dL_dpre_zifo . clip ( self . b_context , - self . grad_clipping , self . grad_clipping ) <EOL> else : <EOL> self . dL_dpre_zifo . last_modif_context = self . b_context <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dW . add_dot ( self . W_b_context , self . x , self . dL_dpre_zifo , '<STR_LIT:T>' ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dR . add_dot ( self . R_b_context , self . prev_h , self . dL_dpre_zifo , '<STR_LIT:T>' ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_db . add_repeat_derivative ( self . b_b_context , self . dL_dpre_zifo , self . dL_dpre_zifo . nrows , axis = <NUM_LIT:0> ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dx . add_dot ( self . x_b_context , self . dL_dpre_zifo , self . W , '<STR_LIT:N>' , '<STR_LIT:T>' ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dprev_c . add_hprod ( self . prev_c_b_context , self . f , dL_dc ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dprev_h . add_dot ( self . prev_h_b_context , self . dL_dpre_zifo , self . R , '<STR_LIT:N>' , '<STR_LIT:T>' ) </s>
