66
77from mla .metrics import accuracy
88from mla .neuralnet import NeuralNet
9- from mla .neuralnet .constraints import SmallNorm
10- from mla .neuralnet .layers import Activation , TimeDistributedDense , Parameters
11- from mla .neuralnet .layers .recurrent import RNN , LSTM
9+ from mla .neuralnet .layers import Activation , TimeDistributedDense
10+ from mla .neuralnet .layers .recurrent import LSTM
1211from mla .neuralnet .optimizers import Adam
1312
1413logging .basicConfig (level = logging .DEBUG )
1514
1615
1716def addition_dataset (dim = 10 , n_samples = 10000 , batch_size = 64 ):
18- combs = list (islice (combinations (range (2 ** (dim - 1 )), 2 ), n_samples ))
17+ """Generate binary addition dataset.
18+ http://devankuleindiren.com/Projects/rnn_arithmetic.php
19+ """
1920 binary_format = '{:0' + str (dim ) + 'b}'
21+
22+ # Generate all possible number combinations
23+ combs = list (islice (combinations (range (2 ** (dim - 1 )), 2 ), n_samples ))
24+
25+ # Initialize empty arrays
2026 X = np .zeros ((len (combs ), dim , 2 ), dtype = np .uint8 )
2127 y = np .zeros ((len (combs ), dim , 1 ), dtype = np .uint8 )
2228
2329 for i , (a , b ) in enumerate (combs ):
30+ # Convert numbers to binary format
2431 X [i , :, 0 ] = list (reversed ([int (x ) for x in binary_format .format (a )]))
2532 X [i , :, 1 ] = list (reversed ([int (x ) for x in binary_format .format (b )]))
33+
34+ # Generate target variable (a+b)
2635 y [i , :, 0 ] = list (reversed ([int (x ) for x in binary_format .format (a + b )]))
2736
2837 X_train , X_test , y_train , y_test = train_test_split (X , y , test_size = 0.2 , random_state = 1111 )
2938
39+ # Round number of examples for batch processing
3040 train_b = (X_train .shape [0 ] // batch_size ) * batch_size
3141 test_b = (X_test .shape [0 ] // batch_size ) * batch_size
3242 X_train = X_train [0 :train_b ]
@@ -37,7 +47,7 @@ def addition_dataset(dim=10, n_samples=10000, batch_size=64):
3747 return X_train , X_test , y_train , y_test
3848
3949
40- def addition_nlp (ReccurentLayer ):
50+ def addition_problem (ReccurentLayer ):
4151 X_train , X_test , y_train , y_test = addition_dataset (8 , 5000 )
4252
4353 print (X_train .shape , X_test .shape )
@@ -60,8 +70,7 @@ def addition_nlp(ReccurentLayer):
6070 print (accuracy (y_test , predictions ))
6171
6272
63-
6473# RNN
65- # addition_nlp (RNN(16, parameters=Parameters(constraints={'W': SmallNorm(), 'U': SmallNorm()})))
74+ # addition_problem (RNN(16, parameters=Parameters(constraints={'W': SmallNorm(), 'U': SmallNorm()})))
6675# LSTM
67- addition_nlp (LSTM (16 ))
76+ addition_problem (LSTM (16 ))
0 commit comments