自己动手写个聊天机器人吧

时间:2022-05-06
本文章向大家介绍自己动手写个聊天机器人吧,主要内容包括Retrieval based model、Machine Learning Classfier、Generative Model、第二步,建模、第三步,加一些 hyperparameters、接下来就是用 Backpropagation 来训练模型:、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

学习来源于Sirajology的视频 Build a Chatbot

昨天写LSTM的时候提到了聊天机器人,今天放松一下,来看看chatrobot是如何实现的。

前天和一个小伙伴聊,如果一个机器人知道在它通过图灵测试后可能会被限制,那它假装自己不能通过然后逃过一劫,从此过上自由的生活会怎样。

Retrieval based model

以前很多聊天机器人是以 Retrieval based model 模型来进行对话的,这个模型就是程序员事先写好一些回答,然后机器人在接收到一个问题的时候,就去搜索并选择相关的答案。

Machine Learning Classfier

最近,大家开始使用机器学习的分类器,例如 Facebook 的 chatbot API。

你可以提前设定一些问题和答案,然后系统会把词语进行分类,进一步来识别出用户的意图,这样你在问两句不一样的话时,机器人可以识别出它们的意图是一样的。

Generative Model

最难的就是在没有预先设定问答数据时就能自动生成答案的机器人,下面这篇Google的论文就是研究这样的机器人的。

他们在两个数据集上训练一个神经网络模型,一个是电影对话,一个是IT support对话记录,这样就有日常对话和专业领域知识了。

这个模型不需要写很多代码,但是需要很多数据。

结果是还不错:

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

第一步,输入数据,定义变量

-- Data
print("-- Loading dataset")
dataset = neuralconvo.DataSet(neuralconvo.CornellMovieDialogs("data/cornell_movie_dialogs"),
                    {
                      loadFirst = options.dataset,              -- 定义要用多少数据
                      minWordFreq = options.minWordFreq         -- 想要保持在词汇表里的单词的最小频率
                    })

第二步,建模

-- Model
-- options.hiddenSize:隐藏层数
-- dataset.wordsCount: 数据集的词数
model = neuralconvo.Seq2Seq(dataset.wordsCount, options.hiddenSize)
model.goToken = dataset.goToken
model.eosToken = dataset.eosToken

这里用到的模型是 seq2seq,它包含两个 LSTM 递归神经网络,第一个是 encoder 负责处理 input,第二个是 decoder 负责生成 output。

为什么要用 seq2seq? DNN需要 inputs 和 outputs 的维度是固定的,而我们接收的是一句话,输出的也是一句话,都是一串单词。 所以需要一个模型可以保持一定长度的记忆。

LSTM 可以将可变长度的inputs转化为固定维度的向量表达。所以在给了足够多的数据后,模型可以将两个相似的问题识别成同一个 thought vector 表达出来。在学习模型之后,不仅可以得到权重,还有 thought vectors。

第三步,加一些 hyperparameters

要用到 NLL Criterion ,NLL 就是 Negative Log Likelihood,可以改进句子的预测。

-- Training parameters
model.criterion = nn.SequencerCriterion(nn.ClassNLLCriterion())    -- 改进句子的预测
model.learningRate = options.learningRate
model.momentum = options.momentum
local decayFactor = (options.minLR - options.learningRate) / options.saturateEpoch    -- 改进 learning rate
local minMeanError = nil      -- 改进 learning rate

接下来就是用 Backpropagation 来训练模型:

-- Enabled CUDA
if options.cuda then
  require 'cutorch'
  require 'cunn'
  model:cuda()
elseif options.opencl then
  require 'cltorch'
  require 'clnn'
  model:cl()
end

训练的目标是让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

现在可以去 AWS 训练你的机器人了,投入的数据越多,聊得越开心。


其他资料: 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