#NEWLINE# def test_lr_scalers( ) : #NEWLINE# #INDENT# '\n    Tests that SGD respects Model.get_lr_scalers\n    ' #NEWLINE# cost = SumOfCosts ( [ SumOfParams ( ) , ( 0.0 , DummyCost ( ) ) ] ) #NEWLINE# scales = [ 0.01 , 0.02 , 0.05 , 1.0 , 5.0 ] #NEWLINE# shapes = [ ( 1 , ) , ( 9 , ) , ( 8 , 7 ) , ( 6 , 5 , 4 ) , ( 3 , 2 , 2 , 2 ) ] #NEWLINE# learning_rate = 0.001 #NEWLINE# class ModelWithScalers ( Model ) : #NEWLINE# #INDENT# def __init__( self ) : #NEWLINE# #INDENT# super ( ModelWithScalers , self ) . __init__ ( ) #NEWLINE# self . _params = [ sharedX ( np . zeros ( shape ) ) for shape in shapes ] #NEWLINE# self . input_space = VectorSpace ( 1 ) #NEWLINE# #UNINDENT# def __call__( self , X ) : #NEWLINE# #INDENT# return X #NEWLINE# #UNINDENT# def get_lr_scalers( self ) : #NEWLINE# #INDENT# return dict ( zip ( self . _params , scales ) ) #NEWLINE# #UNINDENT# #UNINDENT# model = ModelWithScalers ( ) #NEWLINE# dataset = ArangeDataset ( 1 ) #NEWLINE# sgd = SGD ( cost = cost , learning_rate = learning_rate , learning_rule = Momentum ( 0.0 ) , batch_size = 1 ) #NEWLINE# sgd . setup ( model = model , dataset = dataset ) #NEWLINE# manual = [ param . get_value ( ) for param in model . get_params ( ) ] #NEWLINE# manual = [ ( param - ( learning_rate * scale ) ) for ( param , scale ) in zip ( manual , manual_param ) ] #NEWLINE# sgd . train ( dataset = dataset ) #NEWLINE# assert all ( ( np . allclose ( manual_param , sgd_param . get_value ( ) ) for ( manual_param , sgd_param ) in zip ( manual , model . get_params ( ) ) ) ) #NEWLINE# manual = [ ( param - ( learning_rate * scale ) ) for ( param , scale ) in zip ( manual , scales ) ] #NEWLINE# sgd . train ( dataset = dataset ) #NEWLINE# assert all ( ( np . allclose ( manual_param , sgd_param . get_value ( ) ) for ( manual_param , sgd_param ) in zip ( manual , model . get_params ( ) ) ) )