## 编程知识 cdmana.com

### DL之LSTM：基于《wonderland爱丽丝梦游仙境记》小说数据集利用LSTM算法(基于keras)对word实现预测

DL之LSTM：基于《wonderland爱丽丝梦游仙境记》小说数据集利用LSTM算法(基于keras)对word实现预测

# 基于《wonderland爱丽丝梦游仙境记》小说数据集利用LSTM算法(基于keras)对word实现预测

## 输出结果

``````rawtext_BySpaceConnect: ALICE'S ADVENTURES IN WONDERLAND  Lewis Carroll  THE MILLENNIUM FULCRUM EDITION 3.0  CHAPTER I. Down the Rabbit-Hole  Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversations?'  So she was considering in her own mind (as well as she could, for the hot day
rawtext_BySpace: ALICE'S ADVENTURES IN WONDERLAND Lewis Carroll THE MILLENNIUM FULCRUM EDITION 3.0 CHAPTER I Down the Rabbit Hole Alice was beginning to get very tired of sitting by her sister on the bank and of having nothing to do once or twice she had peeped into the book her sister was reading but it had no pictures or conversations in it and what is the use of a book thought Alice without pictures or conversations So she was considering in her own mind as well as she could for the hot day made her feel very
words_num: 26694
vocab_num: 3063
dataX： 26594 100 [[19, 18, 238, 547, 278, 84, 469, 294, 160, 133, 16, 74, 227, 125, 2713, 393, 223, 31, 2932, 769, 2773, 1456, 2905, 2770, 2006, 2500, 862, 1569, 2495, 2019, 2713, 733, 660, 2006, 1543, 1988, 2773, 1144, 2020, 2035, 2841, 2434, 1513, 2091, 1663, 2713, 810, 1569, 2495, 2932, 2258, 856, 1675, 1513, 1977, 2111, 2035, 1006, 1640, 1675, 660, 2960, 1673, 2713, 2886, 2006, 594, 810, 2741, 31, 3004, 2111, 2035, 1006, 440, 2434, 2932, 996, 1640, 1569, 2051, 1897, 701, 2954, 701, 2434, 1012, 1402, 2713, 1603, 1083, 1847, 1569, 1328, 2905, 2513, 660, 2637, 2969, 2713], [18, 238, 547, 278, 84, 469, 294, 160, 133, 16, 74, 227, 125, 2713, 393, 223, 31, 2932, 769, 2773, 1456, 2905, 2770, 2006, 2500, 862, 1569, 2495, 2019, 2713, 733, 660, 2006, 1543, 1988, 2773, 1144, 2020, 2035, 2841, 2434, 1513, 2091, 1663, 2713, 810, 1569, 2495, 2932, 2258, 856, 1675, 1513, 1977, 2111, 2035, 1006, 1640, 1675, 660, 2960, 1673, 2713, 2886, 2006, 594, 810, 2741, 31, 3004, 2111, 2035, 1006, 440, 2434, 2932, 996, 1640, 1569, 2051, 1897, 701, 2954, 701, 2434, 1012, 1402, 2713, 1603, 1083, 1847, 1569, 1328, 2905, 2513, 660, 2637, 2969, 2713, 2144], [238, 547, 278, 84, 469, 294, 160, 133, 16, 74, 227, 125, 2713, 393, 223, 31, 2932, 769, 2773, 1456, 2905, 2770, 2006, 2500, 862, 1569, 2495, 2019, 2713, 733, 660, 2006, 1543, 1988, 2773, 1144, 2020, 2035, 2841, 2434, 1513, 2091, 1663, 2713, 810, 1569, 2495, 2932, 2258, 856, 1675, 1513, 1977, 2111, 2035, 1006, 1640, 1675, 660, 2960, 1673, 2713, 2886, 2006, 594, 810, 2741, 31, 3004, 2111, 2035, 1006, 440, 2434, 2932, 996, 1640, 1569, 2051, 1897, 701, 2954, 701, 2434, 1012, 1402, 2713, 1603, 1083, 1847, 1569, 1328, 2905, 2513, 660, 2637, 2969, 2713, 2144, 2006], [547, 278, 84, 469, 294, 160, 133, 16, 74, 227, 125, 2713, 393, 223, 31, 2932, 769, 2773, 1456, 2905, 2770, 2006, 2500, 862, 1569, 2495, 2019, 2713, 733, 660, 2006, 1543, 1988, 2773, 1144, 2020, 2035, 2841, 2434, 1513, 2091, 1663, 2713, 810, 1569, 2495, 2932, 2258, 856, 1675, 1513, 1977, 2111, 2035, 1006, 1640, 1675, 660, 2960, 1673, 2713, 2886, 2006, 594, 810, 2741, 31, 3004, 2111, 2035, 1006, 440, 2434, 2932, 996, 1640, 1569, 2051, 1897, 701, 2954, 701, 2434, 1012, 1402, 2713, 1603, 1083, 1847, 1569, 1328, 2905, 2513, 660, 2637, 2969, 2713, 2144, 2006, 1851], [278, 84, 469, 294, 160, 133, 16, 74, 227, 125, 2713, 393, 223, 31, 2932, 769, 2773, 1456, 2905, 2770, 2006, 2500, 862, 1569, 2495, 2019, 2713, 733, 660, 2006, 1543, 1988, 2773, 1144, 2020, 2035, 2841, 2434, 1513, 2091, 1663, 2713, 810, 1569, 2495, 2932, 2258, 856, 1675, 1513, 1977, 2111, 2035, 1006, 1640, 1675, 660, 2960, 1673, 2713, 2886, 2006, 594, 810, 2741, 31, 3004, 2111, 2035, 1006, 440, 2434, 2932, 996, 1640, 1569, 2051, 1897, 701, 2954, 701, 2434, 1012, 1402, 2713, 1603, 1083, 1847, 1569, 1328, 2905, 2513, 660, 2637, 2969, 2713, 2144, 2006, 1851, 594]]
dataY： 26594 [2144, 2006, 1851, 594, 1074]
Total patterns: 26594
X_train.shape (26594, 100, 1)
Y_train.shape (26594, 3063)
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm_1 (LSTM)                (None, 256)               264192
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0
_________________________________________________________________
dense_1 (Dense)              (None, 3063)              787191
=================================================================
Total params: 1,051,383
Trainable params: 1,051,383
Non-trainable params: 0
_________________________________________________________________
LSTM_Model
None

……

Epoch 00005: loss improved from 6.26403 to 6.26198, saving model to hdf5/word-weights-improvement-05-6.2620.hdf5
Epoch 6/10

128/26594 [..............................] - ETA: 2:09 - loss: 6.8378
256/26594 [..............................] - ETA: 2:06 - loss: 6.4136
384/26594 [..............................] - ETA: 2:01 - loss: 6.3299
512/26594 [..............................] - ETA: 1:57 - loss: 6.4469
640/26594 [..............................] - ETA: 1:57 - loss: 6.4133

……

Epoch 00008: loss improved from 6.25725 to 6.25487, saving model to hdf5/word-weights-improvement-08-6.2549.hdf5
Epoch 9/10

128/26594 [..............................] - ETA: 1:57 - loss: 6.2336
256/26594 [..............................] - ETA: 2:02 - loss: 6.1897
384/26594 [..............................] - ETA: 2:04 - loss: 6.3229
512/26594 [..............................] - ETA: 2:01 - loss: 6.3550
640/26594 [..............................] - ETA: 2:02 - loss: 6.3279
768/26594 [..............................] - ETA: 2:05 - loss: 6.2614
896/26594 [>.............................] - ETA: 2:06 - loss: 6.2433
1024/26594 [>.............................] - ETA: 2:07 - loss: 6.2477
……

25216/26594 [===========================>..] - ETA: 6s - loss: 6.2456
25344/26594 [===========================>..] - ETA: 6s - loss: 6.2469
25472/26594 [===========================>..] - ETA: 5s - loss: 6.2477
25600/26594 [===========================>..] - ETA: 4s - loss: 6.2486
25728/26594 [============================>.] - ETA: 4s - loss: 6.2480
25856/26594 [============================>.] - ETA: 3s - loss: 6.2483
25984/26594 [============================>.] - ETA: 2s - loss: 6.2487
26112/26594 [============================>.] - ETA: 2s - loss: 6.2485
26240/26594 [============================>.] - ETA: 1s - loss: 6.2483
26368/26594 [============================>.] - ETA: 1s - loss: 6.2482
26496/26594 [============================>.] - ETA: 0s - loss: 6.2485
26594/26594 [==============================] - 129s 5ms/step - loss: 6.2499

Epoch 00009: loss improved from 6.25487 to 6.24987, saving model to hdf5/word-weights-improvement-09-6.2499.hdf5
Epoch 10/10

128/26594 [..............................] - ETA: 1:56 - loss: 6.4864
256/26594 [..............................] - ETA: 2:04 - loss: 6.2577
384/26594 [..............................] - ETA: 2:07 - loss: 6.2857
512/26594 [..............................] - ETA: 2:10 - loss: 6.3230
……

25856/26594 [============================>.] - ETA: 3s - loss: 6.2426
25984/26594 [============================>.] - ETA: 3s - loss: 6.2447
26112/26594 [============================>.] - ETA: 2s - loss: 6.2446
26240/26594 [============================>.] - ETA: 1s - loss: 6.2449
26368/26594 [============================>.] - ETA: 1s - loss: 6.2467
26496/26594 [============================>.] - ETA: 0s - loss: 6.2461
26594/26594 [==============================] - 135s 5ms/step - loss: 6.2465

Epoch 00010: loss improved from 6.24987 to 6.24646, saving model to hdf5/word-weights-improvement-10-6.2465.hdf5
LSTM_Pre_word.shape:
(3, 3063)

LSTM_Model，Seed:
" cheerfully he seems to grin How neatly spread his claws And welcome little fishes in With gently smiling jaws I'm sure those are not the right words said poor Alice and her eyes filled with tears again as she went on I must be Mabel after all and I shall have to go and live in that poky little house and have next to no toys to play with and oh ever so many lessons to learn No I've made up my mind about it if I'm Mabel I'll stay down here It'll be no use their putting their heads "
199 100

Generated Sequence:
the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the

Done.

``````

## 核心代码

``````LSTM_Model = Sequential()
print('LSTM_Model \n',LSTM_Model.summary())``````

https://yunyaniu.blog.csdn.net/article/details/111658252

Scroll to Top