#NEWLINE# def main( max_seq_length , lstm_dim , batch_size , num_batches , num_epochs ) : #NEWLINE# #INDENT# dataset_train = IterableDataset ( generate_data ( max_seq_length , batch_size , num_batches ) ) #NEWLINE# dataset_test = IterableDataset ( generate_data ( max_seq_length , batch_size , 100 ) ) #NEWLINE# stream_train = DataStream ( dataset = dataset_train ) #NEWLINE# stream_test = DataStream ( dataset = dataset_test ) #NEWLINE# x = T . tensor3 ( 'x' ) #NEWLINE# y = T . matrix ( 'y' ) #NEWLINE# x_to_h = Linear ( 1 , ( lstm_dim * 4 ) , name = 'x_to_h' , weights_init = IsotropicGaussian ( ) , biases_init = Constant ( 0.0 ) ) #NEWLINE# lstm = LSTM ( lstm_dim , name = 'lstm' , weights_init = IsotropicGaussian ( ) , biases_init = Constant ( 0.0 ) ) #NEWLINE# h_to_o = Linear ( lstm_dim , 1 , name = 'h_to_o' , weights_init = IsotropicGaussian ( ) , biases_init = Constant ( 0.0 ) ) #NEWLINE# x_transform = x_to_h . apply ( x ) #NEWLINE# ( h , c ) = lstm . apply ( x_transform ) #NEWLINE# y_hat = h_to_o . apply ( h [ ( - 1 ) ] ) #NEWLINE# y_hat = Logistic ( ) . apply ( y_hat ) #NEWLINE# cost = BinaryCrossEntropy ( ) . apply ( y , y_hat ) #NEWLINE# cost . name = 'cost' #NEWLINE# lstm . initialize ( ) #NEWLINE# x_to_h . initialize ( ) #NEWLINE# h_to_o . initialize ( ) #NEWLINE# cg = ComputationGraph ( cost ) #NEWLINE# algorithm = GradientDescent ( cost = cost , parameters = cg . parameters , step_rule = Adam ( ) ) #NEWLINE# test_monitor = DataStreamMonitoring ( variables = [ cost ] , data_stream = stream_test , prefix = 'test' ) #NEWLINE# train_monitor = TrainingDataMonitoring ( variables = [ cost ] , prefix = 'train' , after_epoch = True ) #NEWLINE# main_loop = MainLoop ( algorithm , stream_train , extensions = [ test_monitor , train_monitor , FinishAfter ( after_n_epochs = num_epochs ) , Printing ( ) , ProgressBar ( ) ] ) #NEWLINE# main_loop . run ( ) #NEWLINE# print ( 'Learned weights:' ) #NEWLINE# for layer in ( x_to_h , lstm , h_to_o ) : #NEWLINE# #INDENT# print ( ( "Layer '%s':" % layer . name ) ) #NEWLINE# for param in layer . parameters : #NEWLINE# #INDENT# print ( param . name , ': ' , param . get_value ( ) ) #NEWLINE# #UNINDENT# print ( ) #NEWLINE# #UNINDENT# return main_loop