<s> from recurrent import * <EOL> from lstm_old import * <EOL> import stage_tests <EOL> import unittest <EOL> from map import * <EOL> from active import * <EOL> from elem_prod import * <EOL> from sum import * <EOL> class Recurrent_Tests ( stage_tests . StageTests ) : <EOL> def setUp ( self ) : <EOL> self . N = <NUM_LIT:5> <EOL> self . T = <NUM_LIT:5> <EOL> self . D = <NUM_LIT:10> <EOL> self . D2 = <NUM_LIT:5> <EOL> self . sigm_ = Map ( <EOL> name = '<STR_LIT>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = self . D2 , <EOL> activeFn = SigmoidActiveFn ( ) , <EOL> initRange = <NUM_LIT:1> , <EOL> initSeed = <NUM_LIT:5> , <EOL> learningRate = <NUM_LIT> <EOL> ) <EOL> self . sigm = RecurrentAdapter ( <EOL> stage = self . sigm_ ) <EOL> self . stage = self . sigm . getStage ( time = <NUM_LIT:0> ) <EOL> self . model = RecurrentContainer ( <EOL> stages = [ self . sigm ] , <EOL> timespan = self . T , <EOL> inputDim = self . D , <EOL> outputDim = self . D2 , <EOL> outputStageNames = [ '<STR_LIT>' ] , <EOL> multiOutput = True , <EOL> name = '<STR_LIT>' , <EOL> outputdEdX = True ) <EOL> self . testInputErr = True <EOL> self . costFn = meanSqErr <EOL> def test_grad ( self ) : <EOL> random = np . random . RandomState ( <NUM_LIT:1> ) <EOL> X = random . rand ( self . N , self . T , self . D ) <EOL> T = random . rand ( self . N , self . T , self . D2 ) <EOL> dEdW , dEdWTmp , dEdX , dEdXTmp = self . calcgrd ( X , T ) <EOL> self . chkgrd ( dEdW , dEdWTmp ) <EOL> self . chkgrd ( dEdX , dEdXTmp ) <EOL> def test_forward ( self ) : <EOL> random = np . random . RandomState ( <NUM_LIT:1> ) <EOL> X = random . rand ( self . N , self . T , self . D ) <EOL> tolerance = <NUM_LIT> <EOL> Y2 = self . realForward ( X ) <EOL> Y = self . model . forward ( X ) <EOL> Y2 = Y2 . reshape ( Y2 . size ) <EOL> Y = Y . reshape ( Y . size ) <EOL> for i in range ( Y . size ) : <EOL> self . assertTrue ( ( Y [ i ] == <NUM_LIT:0> and Y2 [ i ] == <NUM_LIT:0> ) or ( np . abs ( Y [ i ] / Y2 [ i ] - <NUM_LIT:1> ) < tolerance ) ) <EOL> def realForward ( self , X ) : <EOL> Y2 = np . zeros ( ( self . N , self . T , self . D2 ) ) <EOL> for t in range ( self . T ) : <EOL> Y2 [ : , t , : ] = self . sigm_ . forward ( <EOL> np . concatenate ( ( X [ : , t , : ] , Y2 [ : , t - <NUM_LIT:1> , : ] , Y2 [ : , t - <NUM_LIT:2> , : ] ) , axis = - <NUM_LIT:1> ) ) <EOL> return Y2 <EOL> class LSTM_Recurrent_Random_Tests ( unittest . TestCase ) : <EOL> def test_singleErr ( self ) : <EOL> self . func ( False ) <EOL> def test_multiErr ( self ) : <EOL> self . func ( True ) <EOL> def func ( self , multiOutput ) : <EOL> N = <NUM_LIT:5> <EOL> D = <NUM_LIT:10> <EOL> D2 = <NUM_LIT:5> <EOL> Time = <NUM_LIT:5> <EOL> I = RecurrentAdapter ( Map ( <EOL> name = '<STR_LIT:I>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 , <EOL> activeFn = SigmoidActiveFn ( ) , <EOL> initRange = <NUM_LIT:0.1> , <EOL> initSeed = <NUM_LIT:5> , <EOL> biasInitConst = <NUM_LIT:1.0> , <EOL> learningRate = <NUM_LIT> , <EOL> momentum = <NUM_LIT> <EOL> ) ) <EOL> F = RecurrentAdapter ( Map ( <EOL> name = '<STR_LIT:F>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 , <EOL> activeFn = SigmoidActiveFn ( ) , <EOL> initRange = <NUM_LIT:0.1> , <EOL> initSeed = <NUM_LIT:6> , <EOL> biasInitConst = <NUM_LIT:1.0> , <EOL> learningRate = <NUM_LIT> , <EOL> momentum = <NUM_LIT> <EOL> ) ) <EOL> Z = RecurrentAdapter ( Map ( <EOL> name = '<STR_LIT>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 , <EOL> activeFn = TanhActiveFn ( ) , <EOL> initRange = <NUM_LIT:0.1> , <EOL> initSeed = <NUM_LIT:7> , <EOL> biasInitConst = <NUM_LIT:0.0> , <EOL> learningRate = <NUM_LIT> , <EOL> momentum = <NUM_LIT> <EOL> ) ) <EOL> FC = RecurrentAdapter ( ElementProduct ( <EOL> name = '<STR_LIT>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 <EOL> ) ) <EOL> IZ = RecurrentAdapter ( ElementProduct ( <EOL> name = '<STR_LIT>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 <EOL> ) ) <EOL> C = RecurrentAdapter ( Sum ( <EOL> name = '<STR_LIT:C>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> numComponents = <NUM_LIT:2> , <EOL> outputDim = D2 <EOL> ) ) <EOL> O = RecurrentAdapter ( Map ( <EOL> name = '<STR_LIT:O>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 , <EOL> activeFn = SigmoidActiveFn ( ) , <EOL> initRange = <NUM_LIT:0.1> , <EOL> initSeed = <NUM_LIT:8> , <EOL> biasInitConst = <NUM_LIT:1.0> , <EOL> learningRate = <NUM_LIT> , <EOL> momentum = <NUM_LIT> <EOL> ) ) <EOL> U = RecurrentAdapter ( Active ( <EOL> name = '<STR_LIT>' , <EOL> inputNames = [ '<STR_LIT>' ] , <EOL> outputDim = D2 , <EOL> activeFn = TanhActiveFn ( ) <EOL> ) ) <EOL> H = RecurrentAdapter ( ElementProduct ( <EOL> name = '<STR_LIT:H>' , <EOL> inputNames = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputDim = D2 <EOL> ) ) <EOL> lstm = RecurrentContainer ( <EOL> name = '<STR_LIT>' , <EOL> stages = [ I , F , Z , FC , IZ , C , O , U , H ] , <EOL> timespan = Time , <EOL> inputDim = D , <EOL> outputDim = D2 , <EOL> outputStageNames = [ '<STR_LIT:H>' ] , <EOL> multiOutput = multiOutput , <EOL> outputdEdX = True ) <EOL> lstm2 = LSTM_Old ( <EOL> name = '<STR_LIT>' , <EOL> inputDim = D , <EOL> outputDim = D2 , <EOL> needInit = False , <EOL> initRange = <NUM_LIT:0.1> , <EOL> initSeed = <NUM_LIT:0> , <EOL> cutOffZeroEnd = True , <EOL> multiErr = multiOutput , <EOL> learningRate = <NUM_LIT> , <EOL> momentum = <NUM_LIT> <EOL> ) <EOL> random = np . random . RandomState ( <NUM_LIT:1> ) <EOL> costFn = crossEntOne <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> X = random . rand ( N , Time , D ) * <NUM_LIT:0.1> <EOL> if multiOutput : <EOL> T = random . rand ( N , Time , D2 ) * <NUM_LIT:0.1> <EOL> else : <EOL> T = random . rand ( N , D2 ) * <NUM_LIT:0.1> <EOL> Y = lstm . forward ( X ) <EOL> E , dEdY = costFn ( Y , T ) <EOL> dEdX = lstm . backward ( dEdY ) <EOL> if i == <NUM_LIT:0> : <EOL> if I . stages [ <NUM_LIT:0> ] . gpu : <EOL> W = np . concatenate ( ( <EOL> gpu . as_numpy_array ( I . getWeights ( ) ) , <EOL> gpu . as_numpy_array ( F . getWeights ( ) ) , <EOL> gpu . as_numpy_array ( Z . getWeights ( ) ) , <EOL> gpu . as_numpy_array ( O . getWeights ( ) ) ) , axis = <NUM_LIT:0> ) <EOL> else : <EOL> W = np . concatenate ( ( I . getWeights ( ) , <EOL> F . getWeights ( ) , Z . getWeights ( ) , <EOL> O . getWeights ( ) ) , axis = <NUM_LIT:0> ) <EOL> lstm2 . W = W . transpose ( ) <EOL> if multiOutput : <EOL> Y2 = lstm2 . forward ( X ) [ : , : - <NUM_LIT:1> ] <EOL> else : <EOL> Y2 = lstm2 . forward ( X ) <EOL> E , dEdY2 = costFn ( Y2 , T ) <EOL> if multiOutput : <EOL> dEdX2 = lstm2 . backward ( np . concatenate ( ( dEdY2 , np . zeros ( ( N , <NUM_LIT:1> , D2 ) ) ) , axis = <NUM_LIT:1> ) ) <EOL> else : <EOL> dEdX2 = lstm2 . backward ( dEdY2 ) <EOL> if I . stages [ <NUM_LIT:0> ] . gpu : <EOL> dEdW = np . concatenate ( ( <EOL> gpu . as_numpy_array ( I . getGradient ( ) ) , <EOL> gpu . as_numpy_array ( F . getGradient ( ) ) , <EOL> gpu . as_numpy_array ( Z . getGradient ( ) ) , <EOL> gpu . as_numpy_array ( O . getGradient ( ) ) ) , axis = <NUM_LIT:0> ) <EOL> else : <EOL> dEdW = np . concatenate ( ( I . getGradient ( ) , <EOL> F . getGradient ( ) , <EOL> Z . getGradient ( ) , <EOL> O . getGradient ( ) ) , axis = <NUM_LIT:0> ) <EOL> dEdW2 = lstm2 . dEdW <EOL> lstm . updateWeights ( ) <EOL> lstm2 . updateWeights ( ) <EOL> self . chkEqual ( dEdW . transpose ( ) , dEdW2 ) <EOL> if I . stages [ <NUM_LIT:0> ] . gpu : <EOL> W = np . concatenate ( ( <EOL> gpu . as_numpy_array ( I . getWeights ( ) ) , <EOL> gpu . as_numpy_array ( F . getWeights ( ) ) , <EOL> gpu . as_numpy_array ( Z . getWeights ( ) ) , <EOL> gpu . as_numpy_array ( O . getWeights ( ) ) ) , axis = <NUM_LIT:0> ) <EOL> else : <EOL> W = np . concatenate ( ( I . getWeights ( ) , <EOL> F . getWeights ( ) , Z . getWeights ( ) , <EOL> O . getWeights ( ) ) , axis = <NUM_LIT:0> ) <EOL> W2 = lstm2 . W <EOL> def chkEqual ( self , a , b ) : <EOL> tolerance = <NUM_LIT> <EOL> a = a . reshape ( a . size ) <EOL> b = b . reshape ( b . size ) <EOL> for i in range ( a . size ) : <EOL> if not ( ( a [ i ] == <NUM_LIT:0> and b [ i ] == <NUM_LIT:0> ) or <EOL> ( np . abs ( a [ i ] ) < <NUM_LIT> and np . abs ( b [ i ] ) < <NUM_LIT> ) or <EOL> ( np . abs ( a [ i ] / b [ i ] - <NUM_LIT:1> ) < tolerance ) ) : <EOL> print a [ i ] , b [ i ] , a [ i ] / b [ i ] <EOL> self . assertTrue ( <EOL> ( a [ i ] == <NUM_LIT:0> and b [ i ] == <NUM_LIT:0> ) or <EOL> ( np . abs ( a [ i ] ) < <NUM_LIT> and np . abs ( b [ i ] ) < <NUM_LIT> ) or <EOL> ( np . abs ( a [ i ] / b [ i ] - <NUM_LIT:1> ) < tolerance ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
