#NEWLINE# def test_cost( self ) : #NEWLINE# #INDENT# '\n        Use an RNN to calculate Mersenne number sequences of different\n        lengths and check whether the costs make sense.\n        ' #NEWLINE# rnn = RNN ( input_space = SequenceSpace ( VectorSpace ( dim = 1 ) ) , layers = [ Recurrent ( dim = 1 , layer_name = 'recurrent' , irange = 0 , nonlinearity = ( lambda x : x ) ) , Linear ( dim = 1 , layer_name = 'linear' , irange = 0 ) ] ) #NEWLINE# ( W , U , b ) = rnn . layers [ 0 ] . get_params ( ) #NEWLINE# W . set_value ( [ [ 1 ] ] ) #NEWLINE# U . set_value ( [ [ 2 ] ] ) #NEWLINE# ( W , b ) = rnn . layers [ 1 ] . get_params ( ) #NEWLINE# W . set_value ( [ [ 1 ] ] ) #NEWLINE# ( X_data , X_mask ) = rnn . get_input_space ( ) . make_theano_batch ( ) #NEWLINE# ( y_data , y_mask ) = rnn . get_output_space ( ) . make_theano_batch ( ) #NEWLINE# ( y_data_hat , y_mask_hat ) = rnn . fprop ( ( X_data , X_mask ) ) #NEWLINE# seq_len = 20 #NEWLINE# X_data_vals = np . ones ( ( seq_len , seq_len , 1 ) ) #NEWLINE# X_mask_vals = np . triu ( np . ones ( ( seq_len , seq_len ) ) ) #NEWLINE# y_data_vals = np . tile ( ( ( 2 ** np . arange ( 1 , ( seq_len + 1 ) ) ) - 1 ) , ( seq_len , 1 ) ) . T [ : , : , np . newaxis ] #NEWLINE# y_mask_vals = np . triu ( np . ones ( ( seq_len , seq_len ) ) ) #NEWLINE# f = function ( [ X_data , X_mask , y_data , y_mask ] , rnn . cost ( ( y_data , y_mask ) , ( y_data_hat , y_mask_hat ) ) , allow_input_downcast = True ) #NEWLINE# assert ( f ( X_data_vals , X_mask_vals , y_data_vals , y_mask_vals ) == 0 ) #NEWLINE# assert ( f ( ( X_data_vals + 1 ) , X_mask_vals , y_data_vals , y_mask_vals ) != 0 ) #NEWLINE# assert ( f ( X_data_vals , X_mask_vals , ( y_data_vals + 1 ) , y_mask_vals ) == 1 ) #NEWLINE# X_data_vals_plus = ( X_data_vals + ( 1 - X_mask_vals [ : , : , None ] ) ) #NEWLINE# assert ( f ( X_data_vals_plus , X_mask_vals , y_data_vals , y_mask_vals ) == 0 ) #NEWLINE# y_data_vals_plus = ( y_data_vals + ( 1 - y_mask_vals [ : , : , None ] ) ) #NEWLINE# assert ( f ( X_data_vals , X_mask_vals , y_data_vals_plus , y_mask_vals ) == 0 )