<s> import ctypes as ct <EOL> from itertools import izip <EOL> from quagga . matrix import Matrix <EOL> from quagga . context import Context <EOL> class RmspropStep ( object ) : <EOL> def __init__ ( self , parameters , learning_rate_policy , ema_decay = <NUM_LIT> , epsilon = <NUM_LIT> ) : <EOL> self . parameters = parameters <EOL> self . grad_sqr = [ ] <EOL> for p in self . parameters : <EOL> grad_sqr = Matrix . empty_like ( p ) <EOL> grad_sqr . sync_fill ( <NUM_LIT:0.0> ) <EOL> self . grad_sqr . append ( grad_sqr ) <EOL> self . learning_rate_policy = learning_rate_policy <EOL> self . ema_decay = ema_decay <EOL> self . epsilon = epsilon <EOL> self . contexts = [ Context ( p . device_id ) for p in parameters ] <EOL> self . blocking_contexts = [ ] <EOL> def notify ( self ) : <EOL> del self . blocking_contexts [ : ] <EOL> learning_rate = ct . c_float ( - self . learning_rate_policy . value ) <EOL> for p , gsqr , context in izip ( self . parameters , self . grad_sqr , self . contexts ) : <EOL> dL_dp = p . backward_matrix <EOL> self . blocking_contexts . append ( dL_dp . last_modif_context ) <EOL> gsqr . add_scaled_hprod ( context , dL_dp , dL_dp , self . ema_decay , ( <NUM_LIT:1.0> - self . ema_decay ) ) <EOL> p . add_scaled_div_sqrt ( context , learning_rate , dL_dp , gsqr , self . epsilon ) </s>
