keras 的 example 文件 lstm_text_generation.py 解析
生活随笔
收集整理的這篇文章主要介紹了
keras 的 example 文件 lstm_text_generation.py 解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該程序是學習現有的文章,然后學習預測下個字符,這樣一個字符一個字符的學會寫文章
?
先打印下char_indices
{'\n': 0, ' ': 1, '!': 2, '"': 3, "'": 4, '(': 5, ')': 6, ',': 7, '-': 8, '.': 9, '0': 10, '1': 11, '2': 12, '3': 13, '4': 14, '5': 15, '6': 16, '7': 17, '8': 18, '9': 19, ':': 20, ';': 21, '=': 22, '?': 23, '[': 24, ']': 25, '_': 26, 'a': 27, 'b': 28, 'c': 29, 'd': 30, 'e': 31, 'f': 32, 'g': 33, 'h': 34, 'i': 35, 'j': 36, 'k': 37, 'l': 38, 'm': 39, 'n': 40, 'o': 41, 'p': 42, 'q': 43, 'r': 44, 's': 45, 't': 46, 'u': 47, 'v': 48, 'w': 49, 'x': 50, 'y': 51, 'z': 52, '?': 53, '?': 54, 'é': 55, '?': 56}
?
然后構造訓練數據,輸入是?sentences,輸出是?next_chars,構造成如下結構,sentences就是把句子拆分出來,next_chars,名字就看出來了,就是下一個字符
sentences next_chars
preface\n\n\nsupposing that truth is a woma n
face\n\n\nsupposing that truth is a woman-- w
e\n\n\nsupposing that truth is a woman--wha t\nsupposing that truth is a woman--what t hpposing that truth is a woman--what then ?sing that truth is a woman--what then? i sg that truth is a woman--what then? is t hhat truth is a woman--what then? is ther etruth is a woman--what then? is there n outh is a woman--what then? is there not gis a woman--what then? is there not gro ua woman--what then? is there not ground \nwoman--what then? is there not ground\nfo ran--what then? is there not ground\nfor s u-what then? is there not ground\nfor susp eat then? is there not ground\nfor suspect ithen? is there not ground\nfor suspectingn? is there not ground\nfor suspecting th ais there not ground\nfor suspecting that athere not ground\nfor suspecting that allre not ground\nfor suspecting that all ph inot ground\nfor suspecting that all philo sground\nfor suspecting that all philosop hound\nfor suspecting that all philosopher sd\nfor suspecting that all philosophers, ior suspecting that all philosophers, in ssuspecting that all philosophers, in so fpecting that all philosophers, in so farting that all philosophers, in so far asg that all philosophers, in so far as th ehat all philosophers, in so far as they hall philosophers, in so far as they hav el philosophers, in so far as they have b ehilosophers, in so far as they have been \nosophers, in so far as they have been\ndo gphers, in so far as they have been\ndogma trs, in so far as they have been\ndogmatis tin so far as they have been\ndogmatists,so far as they have been\ndogmatists, ha vfar as they have been\ndogmatists, have fr as they have been\ndogmatists, have fai ls they have been\ndogmatists, have failedhey have been\ndogmatists, have failed tohave been\ndogmatists, have failed to un dve been\ndogmatists, have failed to under sbeen\ndogmatists, have failed to understa nn\ndogmatists, have failed to understand wogmatists, have failed to understand wom eatists, have failed to understand women- -sts, have failed to understand women--th a
啊,有一點,就是上面的sentence,直接看起來好像不一樣長,實際是一樣長的,只不過前面三行,有兩個\n,在打印的時候是兩個字符,實際上\n是一個字符,導致的看起來不整齊
然后進行one-hot編碼,這都是NLP的常規操作,然后輸入輸出數據shape為:
x.shape ?(200285, 40, 57)
y.shape ?(200285, 57)
神經網絡模型為
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 128) 95232
_________________________________________________________________
dense_1 (Dense) (None, 57) 7353
=================================================================
Total params: 102,585
Trainable params: 102,585
Non-trainable params: 0
_________________________________________________________________
?
——————————————————————
總目錄
keras的example文件解析
總結
以上是生活随笔為你收集整理的keras 的 example 文件 lstm_text_generation.py 解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: keras 的 example 文件 l
- 下一篇: keras 的 example 文件 m