文字生成範例
在這個練習中,你要體驗兩個已訓練完成的文字生成模型。
第一個模型會依據影集 The Big Bang Theory 中角色 Sheldon 的語氣生成一句話;第二個模型會生成最長 400 個字元的莎士比亞風格詩句。
這兩個模型分別載入在變數 sheldon_model 與 poem_model。另外也提供兩個協助生成文字的自訂函式:generate_sheldon_phrase() 和 generate_poem()。兩者都會接收已訓練模型與一段情境字串作為參數。
本練習屬於課程
使用 Keras 建立語言模型的循環神經網路(RNN)
練習說明
- 使用預先定義的函式
generate_sheldon_phrase(),以sheldon_model與sheldon_context作為參數,並將輸出儲存在變數sheldon_phrase。 - 印出取得的句子。
- 將給定的文字存入變數
poem_context。 - 呼叫函式
generate_poem(),以poem_model與poem_context為參數產生詩,並將其印出。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Context for Sheldon phrase
sheldon_context = "I’m not insane, my mother had me tested. "
# Generate one Sheldon phrase
sheldon_phrase = ____(sheldon_model, sheldon_context)
# Print the phrase
print(____)
# Context for poem
____ = "May thy beauty forever remain"
# Print the poem
print(generate_poem(____, poem_context))