古詩詞是中國文化殿堂的瑰寶,記得曾經在韓國作 Exchange Student 的時候,看到他們學習咱們的古詩詞,有中文的還有翻譯版的,本身發自心裏的驕傲,甚至也會在某些時候背起一些耳熟能詳的詩詞。html
本文將會經過深度學習爲咱們生成一些古詩詞,並將模型部署到 Serverless 架構上,實現基於 Serverless 的古詩詞生成 API。python
古詩詞生成其實是文本生成,或者說是生成式文本。關於基於深度學習的文本生成,最入門級的讀物包括 Andrej Karpathy 的博客。他使用例子生動講解了 Char-RNN (Character based Recurrent Neural Network) 如何用於從文本數據集裏學習,而後自動生成像模像樣的文本。git
上圖直觀展現了 Char-RNN 的原理。以要讓模型學習寫出「hello」爲例,Char-RNN 的輸入輸出層都是以字符爲單位。輸入「h」,應該輸出「e」;輸入「e」,則應該輸出後續的「l」。github
輸入層咱們能夠用只有一個元素爲1的向量來編碼不一樣的字符,例如,「h」被編碼爲「1000」、「e」被編碼爲「0100」,而「l」被編碼爲「0010」。使用 RNN 的學習目標是,可讓生成的下一個字符儘可能與訓練樣本里的目標輸出一致。在圖一的例子中,根據前兩個字符產生的狀態和第三個輸入「l」預測出的下一個字符的向量爲 <0.1, 0.5, 1.9, -1.1>,最大的一維是第三維,對應的字符則爲「0010」,正好是「l」。這就是一個正確的預測。但從第一個「h」獲得的輸出向量是第四維最大,對應的並非「e」,這樣就產生代價。express
學習的過程就是不斷下降這個代價。學習到的模型,對任何輸入字符能夠很好地不斷預測下一個字符,如此一來就能生成句子或段落。json
本文項目構建參考了 Github 已有項目:https://github.com/norybaby/poetapi
經過 Clone 代碼,而且安裝相關依賴:瀏覽器
pip3 install tensorflow==1.14 word2vec numpy
經過訓練:架構
python3 train.py
能夠看到訓練結果:less
此時會生成多個模型在 output_poem 文件夾下,咱們只須要保留最好的便可,例如個人訓練以後生成的 json 文件:
{ "best_model": "output_poem/best_model/model-20390", "best_valid_ppl": 21.441762924194336, "latest_model": "output_poem/save_model/model-20390", "params": { "batch_size": 16, "cell_type": "lstm", "dropout": 0.0, "embedding_size": 128, "hidden_size": 128, "input_dropout": 0.0, "learning_rate": 0.005, "max_grad_norm": 5.0, "num_layers": 2, "num_unrollings": 64 }, "test_ppl": 25.83984375 }
此時,我只須要保存 output_poem/best_model/model-20390
模型便可。
在項目目錄下,安裝必要依賴:
pip3 install word2vec numpy -t ./
因爲 Tensorflow 等是騰訊云云函數內置的package,因此這裏無需安裝,另外 numpy 這個 package 須要在 CentOS+Python3.6 環境下打包。也能夠經過以前製做的小工具打包:https://www.serverlesschina.com/35.html
完成以後,編寫函數入口文件:
import uuid, json from write_poem import WritePoem, start_model writer = start_model() def return_msg(error, msg): return_data = { "uuid": str(uuid.uuid1()), "error": error, "message": msg } print(return_data) return return_data def main_handler(event, context): # 類型 # 1: 自由 # 2: 押韻 # 3: 藏頭押韻 # 4: 藏字押韻 style = json.loads(event["body"])["style"] content = json.loads(event["body"]).get("content", None) if style in '34' and not content: return return_msg(True, "請輸入content參數") if style == '1': return return_msg(False, writer.free_verse()) elif style == '2': return return_msg(False, writer.rhyme_verse()) elif style == '3': return return_msg(False, writer.cangtou(content)) elif style == '4': return return_msg(False, writer.hide_words(content)) else: return return_msg(True, "請輸入正確的style參數")
同時須要準備好 Yaml 文件:
getUserIp: component: "@serverless/tencent-scf" inputs: name: autoPoem codeUri: ./ exclude: - .gitignore - .git/** - .serverless - .env handler: index.main_handler runtime: Python3.6 region: ap-beijing description: 自動古詩詞撰寫 namespace: serverless_tools memorySize: 512 timeout: 10 events: - apigw: name: serverless parameters: serviceId: service-8d3fi753 protocols: - http - https environment: release endpoints: - path: /auto/poem description: 自動古詩詞撰寫 method: POST enableCORS: true
此時,咱們就能夠經過 Serverless Framework CLI 部署項目。部署完成以後,咱們能夠經過 PostMan 測試咱們的接口:
本文經過已有的深度學習項目,在本地進行訓練,保存模型,而後將項目部署在騰訊云云函數上,經過與 API 網關的聯動,實現了一個基於深度學習的古詩詞撰寫的 API。
3 秒你能作什麼?喝一口水,看一封郵件,仍是 —— 部署一個完整的 Serverless 應用?
複製連接至 PC 瀏覽器訪問:https://serverless.cloud.tencent.com/deploy/express
3 秒極速部署,當即體驗史上最快的 Serverless HTTP 實戰開發!
傳送門:
- GitHub: github.com/serverless
- 官網:serverless.com
歡迎訪問:Serverless 中文網,您能夠在 最佳實踐 裏體驗更多關於 Serverless 應用的開發!