rasa對(duì)話系統(tǒng)踩坑記(六)

說是不更新的,覺得這個(gè)文章也只是自己的一個(gè)流水賬式的踩坑記錄,應(yīng)該也沒什么人關(guān)注。但最近好幾個(gè)人給我提issure,發(fā)email,才知道原來現(xiàn)在國(guó)內(nèi)也不少公司在關(guān)注并使用rasa(話說幾個(gè)月前我用rasa的時(shí)候好像沒那么多人關(guān)注的)。所以繼續(xù)更新rasa-core相關(guān)的踩坑文章吧,而且最近rasa-core版本確實(shí)也變動(dòng)了不少。

rasa_chatbot_cn這個(gè)demo中,開始使用的是rasa-core==0.11.4的版本,但因?yàn)樽罱膸讉€(gè)功能我升級(jí)到了0.12.3。這篇先說說,compare policy的實(shí)現(xiàn)。

之前的項(xiàng)目一直使用的是KerasPolicy,就算是自定義的policy也是繼承自KerasPolicy。而KerasPolicy的模型很簡(jiǎn)單,只是單一的LSTM+Dense+softmax。導(dǎo)致的問題是我們需要不斷地完善自己的story,把各種情況下的story進(jìn)行補(bǔ)充,花費(fèi)了大量的人工。

然后后面就在嘗試使用EmbeddingPolicy,因?yàn)樗C合了很多方法。


embeddint-dialogue-policy.png

EmbeddingPolicy包含以下的步驟:

  • apply dense layers to create embeddings for user intents, entities and system actions including previous actions and slots(稠密嵌入,包括用戶意圖、實(shí)體和系統(tǒng)行為:以前的動(dòng)作和槽)
  • use the embeddings of previous user inputs as a user memory and embeddings of previous system actions as a system memory.(使用以前的用戶輸入作為用戶memory和以前的系統(tǒng)行為作為系統(tǒng)memory)
  • concatenate user input, previous system action and slots embeddings for current time into an input vertor to rnn(合并用戶輸入,以前系統(tǒng)行為和槽作為當(dāng)前時(shí)間用戶的輸入向量給rnn模型)
  • using user and previous system action embeddings from the input vector, calculate attention probabilities over the user and system memories(使用輸入向量中用戶輸入和以前的系統(tǒng)行為嵌入,來計(jì)算用戶和系統(tǒng)的注意力向量)
  • sum the user embedding and user attention vector and feed it and the embeddings of the slots as an input to an LSTM cell(用戶詞嵌入和用戶注意力向量相加再和槽向量一起作為L(zhǎng)STM的輸入)
  • apply a dense layer to the output of the LSTM to get a raw recurrent embedding of a dialogue(應(yīng)用LSTM的輸出來獲得一個(gè)對(duì)話的原始循環(huán)嵌入)
  • sum this raw recurrent embedding of a dialogue with system attention vector to create dialogue level embedding, this step allows the algorithm to repeat previous system action by copying its embedding vector directly to the current time output(將對(duì)話的原始循環(huán)嵌入和系統(tǒng)注意力向量相加,來創(chuàng)建對(duì)話層的嵌入。這一步允許算法通過直接拷貝它的向量到當(dāng)前的輸出來重復(fù)之前的系統(tǒng)行為)
  • weight previous LSTM states with system attention probabilities to get the previous action embedding, the policy is likely payed attention to(加權(quán)以前的LSTM狀態(tài)和系統(tǒng)注意力來獲取以前的行為嵌入,policy最有可能需要注意的)
  • if the similarity between this previous action embedding and current time dialogue embedding is high, overwrite current LSTM state with the one from the time when this action happened(如果以前的行為嵌入和當(dāng)前的對(duì)話嵌入相似度很高,overwrite當(dāng)前的LSTM狀態(tài))
  • for each LSTM time step, calculate the similarity between the dialogue embedding and embedded system actions(對(duì)于LSTM的每一步,計(jì)算對(duì)話嵌入和系統(tǒng)行為嵌入的相似度)

所以EmbeddingPolicy效果上來說會(huì)比較好,但是它有個(gè)問題是耗時(shí),而且尤其是官網(wǎng)的源碼,它并沒有使用GPU、沒有充分利用CPU資源。這個(gè)問題我進(jìn)行了改善,后續(xù)會(huì)分享。因?yàn)楸容^耗時(shí)耗資源,所以需要在小數(shù)量級(jí)上對(duì)policy進(jìn)行比較。

下圖是我在rasa_chatbot_cn上比較了KerasPolicy和EmbeddingPolicy的結(jié)果:

policy-compare.png

圖中很明顯可以看出policy的優(yōu)劣。至于如何使用,首先需要建立要進(jìn)行對(duì)比的policy.yml文件,這里分別是embed_policy.yml和keras_policy.yml文件。然后就是兩個(gè)命令,一個(gè)是compare policy,一個(gè)是evaluate policy:

python -m rasa_core.train compare -c keras_policy.yml embed_policy.yml \
    -d mobile_domain.yml -s data/mobile_edit_story.md -o comparison_models/ --runs 3 --percentages \
    0 25 50 70
python -m rasa_core.evaluate compare -s data/mobile_edit_story.md --core comparison_models/ -o comparison_results/

具體的參數(shù)不做過多解釋,官網(wǎng)有詳細(xì)說明。原創(chuàng)文章,轉(zhuǎn)載請(qǐng)說明出處

Recommand

liveportrait
novelling

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。