tf13: 简单聊天机器人

来源:互联网 发布:哪有学java的学校 编辑:程序博客网 时间:2024/05/22 12:49

现在很多卖货公司都使用聊天机器人充当客服人员,许多科技巨头也纷纷推出各自的聊天助手,如苹果Siri、Google Now、Amazon Alexa、微软小冰等等。前不久有一个视频比较了Google Now和Siri哪个更智能,貌似Google Now更智能。

本帖使用TensorFlow制作一个简单的聊天机器人。这个聊天机器人使用中文对话数据集进行训练(使用什么数据集训练决定了对话类型)。使用的模型为RNN(seq2seq)

相关博文:

  • 使用深度学习打造智能聊天机器人
  • 脑洞大开:基于美剧字幕的聊天语料库建设方案
  • 中文对白语料
  • https://www.tensorflow.org/versions/r0.12/tutorials/seq2seq/index.html
  • https://github.com/tflearn/tflearn/blob/master/examples/nlp/lstm_generator_shakespeare.py
code:Here。

数据集

我使用现成的影视对白数据集,跪谢作者分享数据。

下载数据集:

[python] view plain copy
  1. $ wget https://raw.githubusercontent.com/rustch3n/dgk_lost_conv/master/dgk_shooter_min.conv.zip  
  2. # 解压  
  3. $ unzip dgk_shooter_min.conv.zip  
数据预处理:

[python] view plain copy
  1. import os  
  2. import random  
  3.    
  4. conv_path = 'dgk_shooter_min.conv'  
  5.    
  6. if not os.path.exists(conv_path):  
  7.     print('数据集不存在')  
  8.     exit()  
  9.    
  10. # 数据集格式  
  11. """ 
  12. E 
  13. M 畹/华/吾/侄/ 
  14. M 你/接/到/这/封/信/的/时/候/ 
  15. M 不/知/道/大/伯/还/在/不/在/人/世/了/ 
  16. E 
  17. M 咱/们/梅/家/从/你/爷/爷/起/ 
  18. M 就/一/直/小/心/翼/翼/地/唱/戏/ 
  19. M 侍/奉/宫/廷/侍/奉/百/姓/ 
  20. M 从/来/不/曾/遭/此/大/祸/ 
  21. M 太/后/的/万/寿/节/谁/敢/不/穿/红/ 
  22. M 就/你/胆/儿/大/ 
  23. M 唉/这/我/舅/母/出/殡/ 
  24. M 我/不/敢/穿/红/啊/ 
  25. M 唉/呦/唉/呦/爷/ 
  26. M 您/打/得/好/我/该/打/ 
  27. M 就/因/为/没/穿/红/让/人/赏/咱/一/纸/枷/锁/ 
  28. M 爷/您/别/给/我/戴/这/纸/枷/锁/呀/ 
  29. E 
  30. M 您/多/打/我/几/下/不/就/得/了/吗/ 
  31. M 走/ 
  32. M 这/是/哪/一/出/啊/…/ / /这/是/ 
  33. M 撕/破/一/点/就/弄/死/你/ 
  34. M 唉/ 
  35. M 记/着/唱/戏/的/再/红/ 
  36. M 还/是/让/人/瞧/不/起/ 
  37. M 大/伯/不/想/让/你/挨/了/打/ 
  38. M 还/得/跟/人/家/说/打/得/好/ 
  39. M 大/伯/不/想/让/你/再/戴/上/那/纸/枷/锁/ 
  40. M 畹/华/开/开/门/哪/ 
  41. E 
  42. ... 
  43. """  
  44.    
  45. # 我首先使用文本编辑器sublime把dgk_shooter_min.conv文件编码转为UTF-8,一下子省了不少麻烦  
  46. convs = []  # 对话集合  
  47. with open(conv_path, encoding = "utf8") as f:  
  48.     one_conv = []        # 一次完整对话  
  49.     for line in f:  
  50.         line = line.strip('\n').replace('/''')  
  51.         if line == '':  
  52.             continue  
  53.         if line[0] == 'E':  
  54.             if one_conv:  
  55.                 convs.append(one_conv)  
  56.             one_conv = []  
  57.         elif line[0] == 'M':  
  58.             one_conv.append(line.split(' ')[1])  
  59. """ 
  60. print(convs[:3])  # 个人感觉对白数据集有点不给力啊 
  61. [ ['畹华吾侄', '你接到这封信的时候', '不知道大伯还在不在人世了'],  
  62.   ['咱们梅家从你爷爷起', '就一直小心翼翼地唱戏', '侍奉宫廷侍奉百姓', '从来不曾遭此大祸', '太后的万寿节谁敢不穿红', '就你胆儿大', '唉这我舅母出殡', '我不敢穿红啊', '唉呦唉呦爷', '您打得好我该打', '就因为没穿红让人赏咱一纸枷锁', '爷您别给我戴这纸枷锁呀'],  
  63.   ['您多打我几下不就得了吗', '走', '这是哪一出啊 ', '撕破一点就弄死你', '唉', '记着唱戏的再红', '还是让人瞧不起', '大伯不想让你挨了打', '还得跟人家说打得好', '大伯不想让你再戴上那纸枷锁', '畹华开开门哪'], ....] 
  64. """  
  65.    
  66. # 把对话分成问与答  
  67. ask = []        # 问  
  68. response = []   # 答  
  69. for conv in convs:  
  70.     if len(conv) == 1:  
  71.         continue  
  72.     if len(conv) % 2 != 0:  # 奇数对话数, 转为偶数对话  
  73.         conv = conv[:-1]  
  74.     for i in range(len(conv)):  
  75.         if i % 2 == 0:  
  76.             ask.append(conv[i])  
  77.         else:  
  78.             response.append(conv[i])  
  79.    
  80. """ 
  81. print(len(ask), len(response)) 
  82. print(ask[:3]) 
  83. print(response[:3]) 
  84. ['畹华吾侄', '咱们梅家从你爷爷起', '侍奉宫廷侍奉百姓'] 
  85. ['你接到这封信的时候', '就一直小心翼翼地唱戏', '从来不曾遭此大祸'] 
  86. """  
  87.    
  88. def convert_seq2seq_files(questions, answers, TESTSET_SIZE = 8000):  
  89.     # 创建文件  
  90.     train_enc = open('train.enc','w')  # 问  
  91.     train_dec = open('train.dec','w')  # 答  
  92.     test_enc  = open('test.enc''w')  # 问  
  93.     test_dec  = open('test.dec''w')  # 答  
  94.    
  95.     # 选择20000数据作为测试数据  
  96.     test_index = random.sample([i for i in range(len(questions))],TESTSET_SIZE)  
  97.    
  98.     for i in range(len(questions)):  
  99.         if i in test_index:  
  100.             test_enc.write(questions[i]+'\n')  
  101.             test_dec.write(answers[i]+ '\n' )  
  102.         else:  
  103.             train_enc.write(questions[i]+'\n')  
  104.             train_dec.write(answers[i]+ '\n' )  
  105.         if i % 1000 == 0:  
  106.             print(len(range(len(questions))), '处理进度:', i)  
  107.    
  108.     train_enc.close()  
  109.     train_dec.close()  
  110.     test_enc.close()  
  111.     test_dec.close()  
  112.    
  113. convert_seq2seq_files(ask, response)  
  114. # 生成的*.enc文件保存了问题  
  115. # 生成的*.dec文件保存了回答  

创建词汇表,然后把对话转为向量形式,参看练习1和7:

[python] view plain copy
  1. # 前一步生成的问答文件路径  
  2. train_encode_file = 'train.enc'  
  3. train_decode_file = 'train.dec'  
  4. test_encode_file = 'test.enc'  
  5. test_decode_file = 'test.dec'  
  6.    
  7. print('开始创建词汇表...')  
  8. # 特殊标记,用来填充标记对话  
  9. PAD = "__PAD__"  
  10. GO = "__GO__"  
  11. EOS = "__EOS__"  # 对话结束  
  12. UNK = "__UNK__"  # 标记未出现在词汇表中的字符  
  13. START_VOCABULART = [PAD, GO, EOS, UNK]  
  14. PAD_ID = 0  
  15. GO_ID = 1  
  16. EOS_ID = 2  
  17. UNK_ID = 3  
  18. # 参看tensorflow.models.rnn.translate.data_utils  
  19.    
  20. vocabulary_size = 5000  
  21. # 生成词汇表文件  
  22. def gen_vocabulary_file(input_file, output_file):  
  23.     vocabulary = {}  
  24.     with open(input_file) as f:  
  25.         counter = 0  
  26.         for line in f:  
  27.             counter += 1  
  28.             tokens = [word for word in line.strip()]  
  29.             for word in tokens:  
  30.                 if word in vocabulary:  
  31.                     vocabulary[word] += 1  
  32.                 else:  
  33.                     vocabulary[word] = 1  
  34.         vocabulary_list = START_VOCABULART + sorted(vocabulary, key=vocabulary.get, reverse=True)  
  35.         # 取前5000个常用汉字, 应该差不多够用了(额, 好多无用字符, 最好整理一下. 我就不整理了)  
  36.         if len(vocabulary_list) > 5000:  
  37.             vocabulary_list = vocabulary_list[:5000]  
  38.         print(input_file + " 词汇表大小:", len(vocabulary_list))  
  39.         with open(output_file, "w") as ff:  
  40.             for word in vocabulary_list:  
  41.                 ff.write(word + "\n")  
  42.    
  43. gen_vocabulary_file(train_encode_file, "train_encode_vocabulary")  
  44. gen_vocabulary_file(train_decode_file, "train_decode_vocabulary")  
  45.    
  46. train_encode_vocabulary_file = 'train_encode_vocabulary'  
  47. train_decode_vocabulary_file = 'train_decode_vocabulary'  
  48.    
  49. print("对话转向量...")  
  50. # 把对话字符串转为向量形式  
  51. def convert_to_vector(input_file, vocabulary_file, output_file):  
  52.     tmp_vocab = []  
  53.     with open(vocabulary_file, "r") as f:  
  54.         tmp_vocab.extend(f.readlines())  
  55.     tmp_vocab = [line.strip() for line in tmp_vocab]  
  56.     vocab = dict([(x, y) for (y, x) in enumerate(tmp_vocab)])  
  57.     #{'硕': 3142, 'v': 577, 'I': 4789, '\ue796': 4515, '拖': 1333, '疤': 2201 ...}  
  58.     output_f = open(output_file, 'w')  
  59.     with open(input_file, 'r') as f:  
  60.         for line in f:  
  61.             line_vec = []  
  62.             for words in line.strip():  
  63.                 line_vec.append(vocab.get(words, UNK_ID))  
  64.             output_f.write(" ".join([str(num) for num in line_vec]) + "\n")  
  65.     output_f.close()  
  66.    
  67. convert_to_vector(train_encode_file, train_encode_vocabulary_file, 'train_encode.vec')  
  68. convert_to_vector(train_decode_file, train_decode_vocabulary_file, 'train_decode.vec')  
  69.    
  70. convert_to_vector(test_encode_file, train_encode_vocabulary_file, 'test_encode.vec')  
  71. convert_to_vector(test_decode_file, train_decode_vocabulary_file, 'test_decode.vec')  

生成的train_encode.vec和train_decode.vec用于训练,对应的词汇表是train_encode_vocabulary和train_decode_vocabulary。

训练

需要很长时间训练,这还是小数据集,如果用百GB级的数据,没10天半个月也训练不完。

使用的模型:seq2seq_model.py。

代码:

[python] view plain copy
  1. import tensorflow as tf  # 0.12  
  2. from tensorflow.models.rnn.translate import seq2seq_model  
  3. import os  
  4. import numpy as np  
  5. import math  
  6.    
  7. PAD_ID = 0  
  8. GO_ID = 1  
  9. EOS_ID = 2  
  10. UNK_ID = 3  
  11.    
  12. train_encode_vec = 'train_encode.vec'  
  13. train_decode_vec = 'train_decode.vec'  
  14. test_encode_vec = 'test_encode.vec'  
  15. test_decode_vec = 'test_decode.vec'  
  16.    
  17. # 词汇表大小5000  
  18. vocabulary_encode_size = 5000  
  19. vocabulary_decode_size = 5000  
  20.    
  21. buckets = [(510), (1015), (2025), (4050)]  
  22. layer_size = 256  # 每层大小  
  23. num_layers = 3   # 层数  
  24. batch_size =  64  
  25.    
  26. # 读取*dencode.vec和*decode.vec数据(数据还不算太多, 一次读人到内存)  
  27. def read_data(source_path, target_path, max_size=None):  
  28.     data_set = [[] for _ in buckets]  
  29.     with tf.gfile.GFile(source_path, mode="r") as source_file:  
  30.         with tf.gfile.GFile(target_path, mode="r") as target_file:  
  31.             source, target = source_file.readline(), target_file.readline()  
  32.             counter = 0  
  33.             while source and target and (not max_size or counter < max_size):  
  34.                 counter += 1  
  35.                 source_ids = [int(x) for x in source.split()]  
  36.                 target_ids = [int(x) for x in target.split()]  
  37.                 target_ids.append(EOS_ID)  
  38.                 for bucket_id, (source_size, target_size) in enumerate(buckets):  
  39.                     if len(source_ids) < source_size and len(target_ids) < target_size:  
  40.                         data_set[bucket_id].append([source_ids, target_ids])  
  41.                         break  
  42.                 source, target = source_file.readline(), target_file.readline()  
  43.     return data_set  
  44.    
  45. model = seq2seq_model.Seq2SeqModel(source_vocab_size=vocabulary_encode_size, target_vocab_size=vocabulary_decode_size,  
  46.                                    buckets=buckets, size=layer_size, num_layers=num_layers, max_gradient_norm= 5.0,  
  47.                                    batch_size=batch_size, learning_rate=0.5, learning_rate_decay_factor=0.97, forward_only=False)  
  48.    
  49. config = tf.ConfigProto()  
  50. config.gpu_options.allocator_type = 'BFC'  # 防止 out of memory  
  51.    
  52. with tf.Session(config=config) as sess:  
  53.     # 恢复前一次训练  
  54.     ckpt = tf.train.get_checkpoint_state('.')  
  55.     if ckpt != None:  
  56.         print(ckpt.model_checkpoint_path)  
  57.         model.saver.restore(sess, ckpt.model_checkpoint_path)  
  58.     else:  
  59.         sess.run(tf.global_variables_initializer())  
  60.    
  61.     train_set = read_data(train_encode_vec, train_decode_vec)  
  62.     test_set = read_data(test_encode_vec, test_decode_vec)  
  63.    
  64.     train_bucket_sizes = [len(train_set[b]) for b in range(len(buckets))]  
  65.     train_total_size = float(sum(train_bucket_sizes))  
  66.     train_buckets_scale = [sum(train_bucket_sizes[:i + 1]) / train_total_size for i in range(len(train_bucket_sizes))]  
  67.    
  68.     loss = 0.0  
  69.     total_step = 0  
  70.     previous_losses = []  
  71.     # 一直训练,每过一段时间保存一次模型  
  72.     while True:  
  73.         random_number_01 = np.random.random_sample()  
  74.         bucket_id = min([i for i in range(len(train_buckets_scale)) if train_buckets_scale[i] > random_number_01])  
  75.    
  76.         encoder_inputs, decoder_inputs, target_weights = model.get_batch(train_set, bucket_id)  
  77.         _, step_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, False)  
  78.    
  79.         loss += step_loss / 500  
  80.         total_step += 1  
  81.    
  82.         print(total_step)  
  83.         if total_step % 500 == 0:  
  84.             print(model.global_step.eval(), model.learning_rate.eval(), loss)  
  85.    
  86.             # 如果模型没有得到提升,减小learning rate  
  87.             if len(previous_losses) > 2 and loss > max(previous_losses[-3:]):  
  88.                 sess.run(model.learning_rate_decay_op)  
  89.             previous_losses.append(loss)  
  90.             # 保存模型  
  91.             checkpoint_path = "chatbot_seq2seq.ckpt"  
  92.             model.saver.save(sess, checkpoint_path, global_step=model.global_step)  
  93.             loss = 0.0  
  94.             # 使用测试数据评估模型  
  95.             for bucket_id in range(len(buckets)):  
  96.                 if len(test_set[bucket_id]) == 0:  
  97.                     continue  
  98.                 encoder_inputs, decoder_inputs, target_weights = model.get_batch(test_set, bucket_id)  
  99.                 _, eval_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, True)  
  100.                 eval_ppx = math.exp(eval_loss) if eval_loss < 300 else float('inf')  
  101.                 print(bucket_id, eval_ppx)  

聊天机器人

使用训练好的模型:

[python] view plain copy
  1. import tensorflow as tf  # 0.12  
  2. from tensorflow.models.rnn.translate import seq2seq_model  
  3. import os  
  4. import numpy as np  
  5.    
  6. PAD_ID = 0  
  7. GO_ID = 1  
  8. EOS_ID = 2  
  9. UNK_ID = 3  
  10.    
  11. train_encode_vocabulary = 'train_encode_vocabulary'  
  12. train_decode_vocabulary = 'train_decode_vocabulary'  
  13.    
  14. def read_vocabulary(input_file):  
  15.     tmp_vocab = []  
  16.     with open(input_file, "r") as f:  
  17.         tmp_vocab.extend(f.readlines())  
  18.     tmp_vocab = [line.strip() for line in tmp_vocab]  
  19.     vocab = dict([(x, y) for (y, x) in enumerate(tmp_vocab)])  
  20.     return vocab, tmp_vocab  
  21.    
  22. vocab_en, _, = read_vocabulary(train_encode_vocabulary)  
  23. _, vocab_de, = read_vocabulary(train_decode_vocabulary)  
  24.    
  25. # 词汇表大小5000  
  26. vocabulary_encode_size = 5000  
  27. vocabulary_decode_size = 5000  
  28.    
  29. buckets = [(510), (1015), (2025), (4050)]  
  30. layer_size = 256  # 每层大小  
  31. num_layers = 3   # 层数  
  32. batch_size =  1  
  33.    
  34. model = seq2seq_model.Seq2SeqModel(source_vocab_size=vocabulary_encode_size, target_vocab_size=vocabulary_decode_size,  
  35.                                    buckets=buckets, size=layer_size, num_layers=num_layers, max_gradient_norm= 5.0,  
  36.                                    batch_size=batch_size, learning_rate=0.5, learning_rate_decay_factor=0.99, forward_only=True)  
  37. model.batch_size = 1  
  38.    
  39. with tf.Session() as sess:  
  40.     # 恢复前一次训练  
  41.     ckpt = tf.train.get_checkpoint_state('.')  
  42.     if ckpt != None:  
  43.         print(ckpt.model_checkpoint_path)  
  44.         model.saver.restore(sess, ckpt.model_checkpoint_path)  
  45.     else:  
  46.         print("没找到模型")  
  47.    
  48.     while True:  
  49.         input_string = input('me > ')  
  50.         # 退出  
  51.         if input_string == 'quit':  
  52.             exit()  
  53.    
  54.         input_string_vec = []  
  55.         for words in input_string.strip():  
  56.             input_string_vec.append(vocab_en.get(words, UNK_ID))  
  57.         bucket_id = min([b for b in range(len(buckets)) if buckets[b][0] > len(input_string_vec)])  
  58.         encoder_inputs, decoder_inputs, target_weights = model.get_batch({bucket_id: [(input_string_vec, [])]}, bucket_id)  
  59.         _, _, output_logits = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, True)  
  60.         outputs = [int(np.argmax(logit, axis=1)) for logit in output_logits]  
  61.         if EOS_ID in outputs:  
  62.             outputs = outputs[:outputs.index(EOS_ID)]  
  63.    
  64.         response = "".join([tf.compat.as_str(vocab_de[output]) for output in outputs])  
  65.         print('AI > ' + response)  
测试:

TensorFlow练习13: 制作一个简单的聊天机器人

额,好差劲。

上面的实现并没有用到任何自然语言的特性(分词、语法等等),只是单纯的使用数据强行提高它的“智商”。

后续练习:中文语音识别、文本转语音

原创粉丝点击