
學(xué)習(xí)來源于Sirajology的視頻 Build a Chatbot
昨天寫LSTM的時候提到了聊天機器人,今天放松一下,來看看chatrobot是如何實現(xiàn)的。


前天和一個小伙伴聊,如果一個機器人知道在它通過圖靈測試后可能會被限制,那它假裝自己不能通過然后逃過一劫,從此過上自由的生活會怎樣。

Retrieval based model
以前很多聊天機器人是以 Retrieval based model 模型來進行對話的,這個模型就是程序員事先寫好一些回答,然后機器人在接收到一個問題的時候,就去搜索并選擇相關(guān)的答案。



Machine Learning Classfier
最近,大家開始使用機器學(xué)習(xí)的分類器,例如 Facebook 的 chatbot API。

你可以提前設(shè)定一些問題和答案,然后系統(tǒng)會把詞語進行分類,進一步來識別出用戶的意圖,這樣你在問兩句不一樣的話時,機器人可以識別出它們的意圖是一樣的。

Generative Model
最難的就是在沒有預(yù)先設(shè)定問答數(shù)據(jù)時就能自動生成答案的機器人,下面這篇Google的論文就是研究這樣的機器人的。

他們在兩個數(shù)據(jù)集上訓(xùn)練一個神經(jīng)網(wǎng)絡(luò)模型,一個是電影對話,一個是IT support對話記錄,這樣就有日常對話和專業(yè)領(lǐng)域知識了。
這個模型不需要寫很多代碼,但是需要很多數(shù)據(jù)。

結(jié)果是還不錯:

接下來要用 Torch 和 Lua 重建一下論文里的 Neural Network 模型。

第一步,輸入數(shù)據(jù),定義變量
-- Data
print("-- Loading dataset")
dataset = neuralconvo.DataSet(neuralconvo.CornellMovieDialogs("data/cornell_movie_dialogs"),
{
loadFirst = options.dataset, -- 定義要用多少數(shù)據(jù)
minWordFreq = options.minWordFreq -- 想要保持在詞匯表里的單詞的最小頻率
})
第二步,建模
-- Model
-- options.hiddenSize:隱藏層數(shù)
-- dataset.wordsCount: 數(shù)據(jù)集的詞數(shù)
model = neuralconvo.Seq2Seq(dataset.wordsCount, options.hiddenSize)
model.goToken = dataset.goToken
model.eosToken = dataset.eosToken
這里用到的模型是 seq2seq,它包含兩個 LSTM 遞歸神經(jīng)網(wǎng)絡(luò),第一個是 encoder 負責(zé)處理 input,第二個是 decoder 負責(zé)生成 output。

為什么要用 seq2seq?
DNN需要 inputs 和 outputs 的維度是固定的,而我們接收的是一句話,輸出的也是一句話,都是一串單詞。
所以需要一個模型可以保持一定長度的記憶。

LSTM 可以將可變長度的inputs轉(zhuǎn)化為固定維度的向量表達。所以在給了足夠多的數(shù)據(jù)后,模型可以將兩個相似的問題識別成同一個 thought vector 表達出來。在學(xué)習(xí)模型之后,不僅可以得到權(quán)重,還有 thought vectors。

第三步,加一些 hyperparameters
要用到 NLL Criterion ,NLL 就是 Negative Log Likelihood,可以改進句子的預(yù)測。
-- Training parameters
model.criterion = nn.SequencerCriterion(nn.ClassNLLCriterion()) -- 改進句子的預(yù)測
model.learningRate = options.learningRate
model.momentum = options.momentum
local decayFactor = (options.minLR - options.learningRate) / options.saturateEpoch -- 改進 learning rate
local minMeanError = nil -- 改進 learning rate
接下來就是用 Backpropagation 來訓(xùn)練模型:
-- Enabled CUDA
if options.cuda then
require 'cutorch'
require 'cunn'
model:cuda()
elseif options.opencl then
require 'cltorch'
require 'clnn'
model:cl()
end
訓(xùn)練的目標是讓error越來越小,每個例子有一個輸入句子和一個目標句子。
local err = model:train(input, target)
最后把好的model存下來。
-- Save the model if it improved.
if minMeanError == nil or errors:mean() < minMeanError then
print("\n(Saving model ...)")
torch.save("data/model.t7", model)
minMeanError = errors:mean()
end
model.learningRate = model.learningRate + decayFactor
model.learningRate = math.max(options.minLR, model.learningRate)
end
現(xiàn)在可以去 AWS 訓(xùn)練你的機器人了,投入的數(shù)據(jù)越多,聊得越開心。
其他資料:
The code for this video is here
Here's the Neural Conversational Model paper
check out the machine-generated support conversations, they're mind-blowingly good
You should train this baby in the cloud using AWS. See ML for Hackers #4 for a tutorial on how to use AWS
Some great info on LSTM architecture
Link to Facebook's Chatbot API if you're curious