77import npdl
88
99
10- def main (max_iter , corpus_path = os .path .join (os .path .dirname (__file__ ), 'data/lm/tiny_shakespeare.txt' )):
10+ def get_data ():
11+ corpus_path = os .path .join (os .path .dirname (__file__ ), 'data/lm/tiny_shakespeare.txt' )
12+
1113 raw_text = open (corpus_path , 'r' ).read ()
1214 chars = list (set (raw_text ))
1315 data_size , vocab_size = len (raw_text ), len (chars )
@@ -16,7 +18,6 @@ def main(max_iter, corpus_path=os.path.join(os.path.dirname(__file__), 'data/lm/
1618 index_to_char = {i : ch for i , ch in enumerate (chars )}
1719
1820 time_steps , batch_size = 30 , 40
19-
2021 length = batch_size * 20
2122 text_pointers = np .random .randint (data_size - time_steps - 1 , size = length )
2223 batch_in = np .zeros ([length , time_steps , vocab_size ])
@@ -26,6 +27,12 @@ def main(max_iter, corpus_path=os.path.join(os.path.dirname(__file__), 'data/lm/
2627 batch_in [i , range (time_steps ), b_ [:- 1 ]] = 1
2728 batch_out [i , b_ [- 1 ]] = 1
2829
30+ return batch_size , vocab_size , time_steps , batch_in , batch_out
31+
32+
33+ def main1 (max_iter ):
34+ batch_size , vocab_size , time_steps , batch_in , batch_out = get_data ()
35+
2936 print ("Building model ..." )
3037 net = npdl .Model ()
3138 net .add (npdl .layers .BatchLSTM (n_out = 300 , n_in = vocab_size , return_sequence = True ,
@@ -39,5 +46,21 @@ def main(max_iter, corpus_path=os.path.join(os.path.dirname(__file__), 'data/lm/
3946 net .fit (batch_in , batch_out , max_iter = max_iter , batch_size = batch_size )
4047
4148
49+ def main2 (max_iter ):
50+ batch_size , vocab_size , time_steps , batch_in , batch_out = get_data ()
51+
52+ print ("Building model ..." )
53+ net = npdl .Model ()
54+ net .add (npdl .layers .BatchLSTM (n_out = 300 , n_in = vocab_size , return_sequence = False ,
55+ nb_batch = batch_size , nb_seq = time_steps ))
56+ # net.add(npdl.layers.MeanPooling(pool_size=(time_steps, 1)))
57+ # net.add(npdl.layers.Flatten())
58+ net .add (npdl .layers .Softmax (n_out = vocab_size ))
59+ net .compile (loss = npdl .objectives .SCCE (), optimizer = npdl .optimizers .SGD (lr = 0.00001 , clip = 5 ))
60+
61+ print ("Train model ..." )
62+ net .fit (batch_in , batch_out , max_iter = max_iter , batch_size = batch_size )
63+
64+
4265if __name__ == '__main__' :
43- main (100 )
66+ main1 (100 )
0 commit comments