#NEWLINE# def _runner( layer_class ) : #NEWLINE# #INDENT# '\n    All the recurrent layers share the same interface,\n    so we can run through them with a single function.\n    ' #NEWLINE# layer_test ( layer_class , kwargs = { #NEWLINE# #INDENT# 'output_dim' : output_dim , #NEWLINE# 'return_sequences' : True , #NEWLINE# #UNINDENT# } , input_shape = ( 3 , 2 , 3 ) ) #NEWLINE# layer_test ( layer_class , kwargs = { #NEWLINE# #INDENT# 'output_dim' : output_dim , #NEWLINE# 'dropout_U' : 0.1 , #NEWLINE# 'dropout_W' : 0.1 , #NEWLINE# #UNINDENT# } , input_shape = ( 3 , 2 , 3 ) ) #NEWLINE# model = Sequential ( ) #NEWLINE# model . add ( embeddings . Embedding ( embedding_num , embedding_dim , mask_zero = True , input_length = timesteps , batch_input_shape = ( nb_samples , timesteps ) ) ) #NEWLINE# layer = layer_class ( output_dim , return_sequences = False , stateful = True , weights = None ) #NEWLINE# model . add ( layer ) #NEWLINE# model . compile ( optimizer = 'sgd' , loss = 'mse' ) #NEWLINE# out1 = model . predict ( np . ones ( ( nb_samples , timesteps ) ) ) #NEWLINE# assert ( out6 . shape == ( nb_samples , output_dim ) ) #NEWLINE# model . train_on_batch ( np . ones ( ( nb_samples , timesteps ) ) , np . ones ( ( nb_samples , output_dim ) ) ) #NEWLINE# out2 = model . predict ( np . ones ( ( nb_samples , timesteps ) ) ) #NEWLINE# assert ( out1 . max ( ) != out2 . max ( ) ) #NEWLINE# layer . reset_states ( ) #NEWLINE# out3 = model . predict ( np . ones ( ( nb_samples , timesteps ) ) ) #NEWLINE# assert ( out2 . max ( ) != out3 . max ( ) ) #NEWLINE# model . reset_states ( ) #NEWLINE# out4 = model . predict ( np . ones ( ( nb_samples , timesteps ) ) ) #NEWLINE# assert_allclose ( out3 , out4 , atol = 1e-05 ) #NEWLINE# out5 = model . predict ( np . ones ( ( nb_samples , timesteps ) ) ) #NEWLINE# assert ( out4 . max ( ) != out5 . max ( ) ) #NEWLINE# layer . reset_states ( ) #NEWLINE# left_padded_input = np . ones ( ( nb_samples , timesteps ) ) #NEWLINE# left_padded_input [ 0 , : 1 ] = 0 #NEWLINE# left_padded_input [ 1 , : 2 ] = 0 #NEWLINE# left_padded_input [ 2 , : 3 ] = 0 #NEWLINE# out6 = model . predict ( left_padded_input ) #NEWLINE# layer . reset_states ( ) #NEWLINE# right_padded_input = np . ones ( ( nb_samples , timesteps ) ) #NEWLINE# right_padded_input [ 0 , ( - 1 ) : ] = 0 #NEWLINE# right_padded_input [ 1 , ( - 2 ) : ] = 0 #NEWLINE# right_padded_input [ 2 , ( - 3 ) : ] = 0 #NEWLINE# out7 = model . predict ( right_padded_input ) #NEWLINE# assert_allclose ( out7 , out6 , atol = 1e-05 ) #NEWLINE# layer = layer_class ( output_dim , return_sequences = False , weights = None , batch_input_shape = ( nb_samples , timesteps , embedding_dim ) , W_regularizer = regularizers . WeightRegularizer ( l1 = 0.01 ) , U_regularizer = regularizers . WeightRegularizer ( l1 = 0.01 ) , b_regularizer = 'l2' ) #NEWLINE# shape = ( nb_samples , timesteps , embedding_dim ) #NEWLINE# layer . set_input ( K . variable ( np . ones ( shape ) ) , shape = shape ) #NEWLINE# K . eval ( layer . output )