#NEWLINE# def test_monitor_based_lr( ) : #NEWLINE# #INDENT# high_trigger = 1.0 #NEWLINE# shrink_amt = 0.99 #NEWLINE# low_trigger = 0.99 #NEWLINE# grow_amt = 1.01 #NEWLINE# min_lr = 1e-07 #NEWLINE# max_lr = 1.0 #NEWLINE# dim = 3 #NEWLINE# m = 10 #NEWLINE# rng = np . random . RandomState ( [ 25 , 9 , 2012 ] ) #NEWLINE# X = rng . randn ( m , dim ) #NEWLINE# dataset = DenseDesignMatrix ( X = X ) #NEWLINE# m = 15 #NEWLINE# X = rng . randn ( m , dim ) #NEWLINE# learning_rate = 0.01 #NEWLINE# batch_size = 5 #NEWLINE# epoch_num = 5 #NEWLINE# monitoring_dataset = DenseDesignMatrix ( X = X ) #NEWLINE# cost = DummyCost ( ) #NEWLINE# for i in xrange ( 2 ) : #NEWLINE# #INDENT# if ( i == 1 ) : #NEWLINE# #INDENT# high_trigger = 0.99 #NEWLINE# #UNINDENT# model = SoftmaxModel ( dim ) #NEWLINE# termination_criterion = EpochCounter ( epoch_num ) #NEWLINE# algorithm = SGD ( learning_rate , cost , batch_size = batch_size , monitoring_batches = 3 , monitoring_dataset = monitoring_dataset , termination_criterion = termination_criterion , update_callbacks = None , set_batch_size = False ) #NEWLINE# monitor_lr = MonitorBasedLRAdjuster ( high_trigger = high_trigger , shrink_amt = shrink_amt , low_trigger = low_trigger , grow_amt = grow_amt , min_lr = min_lr , max_lr = max_lr ) #NEWLINE# train = Train ( dataset , model , algorithm , save_path = None , save_freq = 0 , extensions = [ monitor_lr ] ) #NEWLINE# train . main_loop ( ) #NEWLINE# v = model . monitor . channels [ 'objective' ] . val_record #NEWLINE# lr = model . monitor . channels [ 'learning_rate' ] . val_record #NEWLINE# lr_monitor = learning_rate #NEWLINE# for i in xrange ( 2 , ( epoch_num + 1 ) ) : #NEWLINE# #INDENT# if ( v [ ( i - 1 ) ] > ( high_trigger * v [ ( i - 2 ) ] ) ) : #NEWLINE# #INDENT# lr_monitor *= shrink_amt #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( v [ ( i - 1 ) ] > ( low_trigger * v [ ( i - 2 ) ] ) ) : #NEWLINE# #INDENT# lr_monitor *= grow_amt #NEWLINE# #UNINDENT# #UNINDENT# lr_monitor = max ( min_lr , lr_monitor ) #NEWLINE# lr_monitor = min ( max_lr , lr_monitor ) #NEWLINE# assert np . allclose ( lr_monitor , lr [ i ] )