#NEWLINE# def train_model( self , f_train , train_args , f_test , test_args , f_validate , validation_args , n_train_batches = 600 , n_valid_batches = 1 , n_test_batches = 1 , n_epochs = 100 ) : #NEWLINE# #INDENT# self . write_to_logger ( '### MODEL PARAMS ###' ) #NEWLINE# self . write_to_logger ( self . model . model_info ( ) ) #NEWLINE# self . write_to_logger ( '### TRAINING PARAMS ###' ) #NEWLINE# self . write_to_logger ( ( 'Train -> %s: %s' % ( ';' . join ( train_args [ 'inputs' ] . keys ( ) ) , str ( train_args [ 'inputs' ] . values ( ) ) ) ) ) #NEWLINE# self . write_to_logger ( ( 'Test -> %s: %s' % ( ';' . join ( test_args [ 'inputs' ] . keys ( ) ) , str ( test_args [ 'inputs' ] . values ( ) ) ) ) ) #NEWLINE# self . write_to_logger ( ( 'Anneal LR %0.4f after %i.' % ( self . anneal_lr , int ( self . anneal_lr_freq ) ) ) ) #NEWLINE# self . write_to_logger ( '### TRAINING MODEL ###' ) #NEWLINE# done_looping = False #NEWLINE# epoch = 0 #NEWLINE# while ( ( epoch < n_epochs ) and ( not done_looping ) ) : #NEWLINE# #INDENT# epoch += 1 #NEWLINE# if ( ( epoch % self . anneal_lr_freq ) == 0 ) : #NEWLINE# #INDENT# train_args [ 'inputs' ] [ 'learningrate' ] *= self . anneal_lr #NEWLINE# #UNINDENT# start_time = time . time ( ) #NEWLINE# train_outputs = [ ] #NEWLINE# for i in xrange ( n_train_batches ) : #NEWLINE# #INDENT# train_output = f_train ( i , * train_args [ 'inputs' ] . values ( ) ) #NEWLINE# train_outputs . append ( train_output ) #NEWLINE# #UNINDENT# self . eval_train [ epoch ] = np . mean ( np . array ( train_outputs ) , axis = 0 ) #NEWLINE# self . model . after_epoch ( ) #NEWLINE# end_time = ( time . time ( ) - start_time ) #NEWLINE# if ( ( epoch % self . output_freq ) == 0 ) : #NEWLINE# #INDENT# if ( n_test_batches == 1 ) : #NEWLINE# #INDENT# self . eval_test [ epoch ] = f_test ( * test_args [ 'inputs' ] . values ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# test_outputs = [ ] #NEWLINE# for i in xrange ( n_test_batches ) : #NEWLINE# #INDENT# test_output = f_test ( i , * test_args [ 'inputs' ] . values ( ) ) #NEWLINE# test_outputs . append ( test_output ) #NEWLINE# #UNINDENT# self . eval_test [ epoch ] = np . mean ( np . array ( test_outputs ) , axis = 0 ) #NEWLINE# #UNINDENT# if ( f_validate is not None ) : #NEWLINE# #INDENT# if ( n_valid_batches == 1 ) : #NEWLINE# #INDENT# self . eval_validation [ epoch ] = f_validate ( * validation_args [ 'inputs' ] . values ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# valid_outputs = [ ] #NEWLINE# for i in xrange ( n_valid_batches ) : #NEWLINE# #INDENT# valid_output = f_validate ( i , * validation_args [ 'inputs' ] . values ( ) ) #NEWLINE# valid_outputs . append ( valid_output ) #NEWLINE# #UNINDENT# self . eval_validation [ epoch ] = np . mean ( np . array ( valid_outputs ) , axis = 0 ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# self . eval_validation [ epoch ] = ( [ 0.0 ] * len ( validation_args [ 'outputs' ] . keys ( ) ) ) #NEWLINE# #UNINDENT# output_str = ( '[epoch,time,train[%s],test[%s],valid[%s]];%i;%0.2f;' % ( ',' . join ( train_args [ 'outputs' ] . keys ( ) ) , ',' . join ( test_args [ 'outputs' ] . keys ( ) ) , ',' . join ( validation_args [ 'outputs' ] . keys ( ) ) , epoch , end_time ) ) #NEWLINE# output_str += ( ( ( ( ';' . join ( train_args [ 'outputs' ] . values ( ) ) + ';' ) + ';' . join ( test_args [ 'outputs' ] . values ( ) ) ) + ';' ) + ';' . join ( validation_args [ 'outputs' ] . values ( ) ) ) #NEWLINE# outputs = [ float ( o ) for o in self . eval_train [ epoch ] ] #NEWLINE# outputs += [ float ( o ) for o in self . eval_test [ epoch ] ] #NEWLINE# outputs += [ float ( o ) for o in self . eval_validation [ epoch ] ] #NEWLINE# output_str %= tuple ( outputs ) #NEWLINE# self . write_to_logger ( test_args ) #NEWLINE# #UNINDENT# if ( ( self . pickle_f_custom_freq is not None ) and ( ( epoch % self . pickle_f_custom_freq ) == 0 ) ) : #NEWLINE# #INDENT# if ( self . custom_eval_func is not None ) : #NEWLINE# #INDENT# self . custom_eval_func ( self . model , paths . get_custom_eval_path ( epoch , self . model . root_path ) ) #NEWLINE# #UNINDENT# self . plot_eval ( self . eval_train , train_args [ 'outputs' ] . keys ( ) , '_train' ) #NEWLINE# self . plot_eval ( self . eval_test , test_args [ 'outputs' ] . keys ( ) , '_test' ) #NEWLINE# self . plot_eval ( self . eval_validation , validation_args [ 'outputs' ] . keys ( ) , '_validation' ) #NEWLINE# self . dump_dicts ( ) #NEWLINE# self . model . dump_model ( ) #NEWLINE# #UNINDENT# #UNINDENT# if ( self . pickle_f_custom_freq is not None ) : #NEWLINE# #INDENT# self . model . dump_model ( )