莆田联客易外贸网站建设推广,深互动平台网站,合肥网络推广优惠设想科技,帝国程序如何改网站标题情感聊天机器人通常属于开放领域#xff0c;用户可以与机器人进行各种话题的互动。例如#xff0c;微软小冰和早期的AnswerBus就是这种类型的聊天机器人。基于检索的开放领域聊天机器人需要大量的语料数据#xff0c;其开发流程与基于任务型的聊天机器人相似#xff0c;而基…情感聊天机器人通常属于开放领域用户可以与机器人进行各种话题的互动。例如微软小冰和早期的AnswerBus就是这种类型的聊天机器人。基于检索的开放领域聊天机器人需要大量的语料数据其开发流程与基于任务型的聊天机器人相似而基于深度学习的生成类型聊天机器人则具有处理开发领域的先天优势。其中以Seq2Seq模型为基础的闲聊机器人已经在机器翻译领域取得了成功的应用。
Seq2Seq模型是NLP中的一个经典模型最初由Google开发并用于机器翻译。它基于RNN网络模型构建能够支持且不限于的应用包括语言翻译、人机对话、内容生成等。Seq2Seq就如字面意思输入一个序列输出另一个序列。这种结构最重要的地方在于输入序列和输出序列的长度是可变的。Seq2Seq属于Encoder-Decoder的大范畴主要是一个由编码器(encoder)和一个解码器(decoder)组成的网络。编码器将输入项转换为包含其特征的相应隐藏向量解码器反转该过程将向量转换为输出项解码器每次都会使用前一个输出作为其输入。不断重复此过程直到遇到结束字符。
1.基于Seq2Seq的聊天机器人开发流程
我们将基于TensorFlow深度学习框架介绍以Seq2Seq为基础的聊天机器人的开发流程。
1.语料准备
首先是语料准备先准备基于开放域聊天语料进行模型训练。在我们的聊天语料中奇数行是问题偶数行对应的回答。
1 聊点么好呢?2 那我们随便聊聊吧3 你是什么人4 我是智能客服5 有人在吗6 小宝一直会在这里诚心为您服务
基于生成方式的开放领域聊天机器人需要充足的聊天语料聊天语料需要覆盖大部分的话题才能保证回答的多样性和语句的通顺。然后我们通过对所有的聊天语料进行预处理进行字典统计。
python
def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size, tokenizerNone, normalize_digitsTrue): if not gfile.Exists(vocabulary_path): print(Creating vocabulary %s from data %s % (vocabulary_path, data_path)) vocab {} with gfile.GFile(data_path, moderb) as f: counter 0 for line in f: counter 1 if counter % 100000 0: print(processing line %d % counter) line tf.compat.as_bytes(line) tokens tokenizer(line) if tokenizer else basic_tokenizer(line) for win tokens: word _DIGIT_RE.sub(b0, w) if normalize_digits else w if word in vocab: vocab[word] 1 else: vocab[word] 1 vocab_list _START_VOCAB sorted(vocab, keyvocab.get, reverseTrue) if len(vocab_list) max_vocabulary_size: vocab_list vocab_list[:max_vocabulary_size] with gfile.GFile(vocabulary_path, modewb) as vocab_file: for win vocab_list: vocab_file.write(w b\n)
根据统计的词频和字典我们为聊天语料建立Token Id比如“聊点什么好呢”这句话根据每个词在词组中的位置“聊”0“点”1“什么”2“好”3“呢”4可以表征为01234。
python
def data_to_token_ids(data_path, target_path, vocabulary_path, tokenizerNone, normalize_digitsTrue): 将数据文件进行分词并转换为token-ids使用给定的词汇文件。此函数逐行加载来自data_path的数据文件调用上述sentence_to_token_ids并将结果保存在target_path中。有关token-ids格式的详细信息请参阅sentence_to_token_ids的注释。 Args: data_path (str): 数据文件的路径格式为每行一句。 target_path (str): 将创建的文件token-ids的路径。 vocabulary_path (str): 词汇文件的路径。 tokenizer: 用于对每个句子进行分词的函数如果为None将使用basic_tokenizer。 normalize_digits (bool): 如果为True则将所有数字替换为O。 if not gfile.Exists(target_path): print(正在对位于 {} 的数据进行分词.format(data_path)) vocab initialize_vocabulary(vocabulary_path) with gfile.GFile(data_path, moderb) as data_file: with gfile.GFile(target_path, modew) as tokens_file: counter 0 for line in data_file: try: line line.decode(utf8, ignore) except Exception as e: print(e, line) continue counter 1 if counter % 100000 0: print(正在对第 {} 行进行分词.format(counter)) token_ids sentence_to_token_ids(tf.compat.as_bytes(line), vocab, tokenizer, normalize_digits) tokens_file.write( .join([str(tok) for tok in token_ids]) \n)1.2定义Encoder和Decoder 根据Seq2Seq的结构需要首先定义Cell选择GRU或者LSTM的Cell并确定Size。然后利用Tensorflow中tf_Seq2Seq.embedding_attention_Seq2Seq这个函数来构架Encoder和Decoder模型在训练模式下Decoder的输入是真实的Target序列。
def single_cel1(): return tf.contrib.rnn.GRUCell(size) if use_lstm else tf.contrib.rnn.BasicLSTMCell(size) def single_cell(): return tf.contrib.rnn.BasicLSTMCell(size) cell single_cel1() if num_layers 1 else single_cell()
cell tf.contrib.rnn.MultiRNNCell([single_cell() for _ in range(num_layers)]) # The seq2seg function: we use embedding for the input and attention.
def seq2seq_f(encoder_inputs, decoder_inputs, feed_previous): return tf_seq2seq.embedding_attention_seq2seq( encoder_inputs, decoder_inputs, cell, num_encoder_symbolssource_vocab_size, num_decoder_symbolstarget_vocab_size, embedding_sizesize, output_projectionoutput_projection, feed_previousfeed_previous, dtypedtype)
# Training outputs and losses, if forward_only:
self.outputs, self.losses, self.encoder_state tf_seq2seq.model_with_buckets( self.encoder_inputs, self.decoder_inputs, targets, self.target_weights, buckets, lambda x, y: seq2seq_f(x, y, True), softmax_loss_functionsoftmax_loss_function
) # If we use output projection, we need to project outputs for decoding.
if output_projection is not None: for b in xrange(len(buckets)): self.outputs[b] [ tf.matmul(output, output_projection[0]) output_projection[1] for output in self.outputs[b] ]
else: self.outputs, self.losses, self.encoder_state tf_seq2seq.model_with_buckets( self.encoder_inputs, self.decoder_inputs, targets, self.target_weights, buckets, lambda x, y: seq2seq_f(x, y, False), softmax_loss_functionsoftmax_loss_function )
1.3模型训练和评估模块 对于训练阶段首先定义Encoder和Decoder的网络结构12.3.2节然后对输入进行预处理12.3.1节最后通过Get_Batch将数据分成多个Batch并利用Session进行训练。此外每次Epoch都要通过对模型生成语句的困惑度进行计算来评估生成回答语句是否通顺。
python
def det_train(args): print(Preparing dialog data in to, args.model_name, args.data_dir) setup_workpath(workspace*args.workspace) train_data, dev_data, _ data_utils.prepare_dialog_data(args.data_dir, args.vocab_size) if args.reinforce_learn: args.batch_size # is decode one sentence at a time gpu_options tf.GPUOptions(per_process_gpu_memory_fraction*args.gpu_usage) with tf.Session(configtf.ConfigProto(gpu_optionsgpu_options)) as sess: # Create model, print(Creating id layers of hd units.) model seq2seq_model_utils.create_model(sess, args.forward_only-False) # Read data into buckets and compute their sizes, print(Reading development and training data (limit: %d), % args.max_train_data_size) dev_set data_utils.read_data(dev_data, args.buckets*args.rev_model) train_set data_utils.read_data(train_data, args.buckets, args.max_train_data_size, args.rev_model) #Tev mode train_bucket_sizes [len(train_set[b]) for b in range(len(args.buckets))] train_total_size float(sum(train_bucket_sizes)) train_buckets_scale [sum(train_bucket_sizes[:i 1]) / train_total_size for i in range(len(train_bucket_sizes))] # This is the training loop step_time, loss 0.0, 0.0 # current step and loss so far previous_losses [] # to keep track of the losses in every epoch # Load vocabularies vocab_path os.path.join(args.data_dir, rocabid.%d % args.vocab_size) vocab, rev_vocab data_utils.initialize_vocabulary(vocab_path) while True: random_number np.random.random() # random number between 0 and 1 bucket_id min([i for i in range(len(train_buckets_scale)) if train_buckets_scale[i] random_number]) # find the bucket id based on the random number # Get a batch and make a step start_time time.time() # record the start time of this batch encoder_inputs, decoder_inputs, target_weights model.get_batch(train_set, bucket_id) # get a batch from the selected bucket id if args.reinforce_learn: step_loss model.step_rf(args, sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, rev_vocab) # make a step using the reinforcement learning loss function else: step_loss model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_idbucket_id, forward_onlyFalse) # make a step using the default loss function # update the loss and current step after each batch/step finishs (in the end of this loop) loss step_loss / (time.time() - start1.4模型预测和Beam Search模块 在预测模块对应生成对话我们需要利用Beam Search来寻找最优解。通过对Beam Size的控制可以保证输出语句的多样性。此外我们也可以加入强化学习对于不同的机器人回答进行及时的人工反馈通过Reinforcement Learning不断优化模型。
python
Get output logits for the sentence
beams, now_beams, results [(1.0, 0.0, ieos: 0.0, dec inp: decoder_inputs, prob: 1.0, prob_ts: 1.0, prob_t: 4.0))]. []. [ Adjusted probability all_prob_ts model_step(encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) if args.antilm else None all_prob_t model_step(dummy_encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) Normal seg2seg if debug: print( .join([dict_lookup(rev_vocab, w) for w in cand[dec_inp]])) if cand[eos]: results [(prob, 0, cand)] continue Adjusted probability all_prob_ts model_step(encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) if args.antilm else None all_prob_t model_step(dummy_encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) Adjusted probability all_prob_ts model_step(encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) if args.antilm else None all_prob_t model_step(dummy_encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) Adjusted probability all_prob_ts model_step(encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) if args.antilm else None all_prob_t model_step(dummy_encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id)
]
all_prob_ts model_step(encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) if args.antilm else None
all_prob_t model_step(dummy_encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id)
all_prob_ts model_step(encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) if args.antilm else None
all_prob_t model_step(dummy_encoder_inputs, cand[dec_inp], dptr, target_weights, bucket_id) all_prob all_prob_ts - args.antilm * all_prob_t # args.n_bonus * dptr random() * 1e-50
all_prob all_prob_ts - args.antilm * all_prob_t
if args.n_bonus ! 0: all_prob args.n_bonus * dptr Suppress copy-cat (respond the same as input)
if dptr len(input_token_ids): all_prob[input_token_ids[dptr]] all_prob[input_token_ids[dptr]] * 0.01 if return_raw: return all_prob, all_prob_ts, all_prob_t # beam search for c in np.argsort(all_prob)[::-1][:args.beam_size]:
new_cand
gos dec_inp (c - data_utils.EOS_ID), [(np.array([c]) if i -- (dptr1) else k)
for i, k in enumerate(cand[dec_inp])]
prob_ts cand[prob_ts *all_prob_ts[c]
prob prob cand[prob _ cand[prob ] * all_prob t[c]
new_cand (new_cand[prob], random(). new_cand) # stuff a randon to prevent comparing new_cand
if len (new_beams) args.beam_size:
heapq. heappush(new_beams, new cand)
elif (new cando[0] new _beams[0][0]):
heapq. heapreplace(new _beams, new _cand)
except Exception as e:
print([Error], e)
print( ----[new _beams]-- )
print(-ines _cand]\n, new _cand) -\n. new _beams)
results new _cands # flush last cands post-process results res _cands
for prob, _ in sorted(results, reverseTrue):
cand[dec _inp]l- res _cands. append(cand) join([dict _lookup(rev _vocab. w) for w in cand[dec _inp]l]) retugn res _cands[:args. beam _size]
往期精彩文章
基础课22——云服务SaaS、Pass、laas、AIaas-CSDN博客文章浏览阅读47次。云服务是一种基于互联网的计算模式通过云计算技术将计算、存储、网络等资源以服务的形式提供给用户用户可以通过网络按需使用这些资源无需购买、安装和维护硬件设备。云服务具有灵活扩展、按需使用、随时随地访问等优势可以降低用户成本提高资源利用效率。随着云计算技术的不断发展云服务的应用范围也将越来越广泛。https://blog.csdn.net/2202_75469062/article/details/134212001?spm1001.2014.3001.5501
基础课20——智能客服系统的使用维护-CSDN博客文章浏览阅读72次。智能客服系统在上线后仍然需要定期的维护和更新。这是因为智能客服系统是一个复杂的软件系统涉及到多个组件和功能需要不断优化和改进以满足用户需求和保持市场竞争力。https://blog.csdn.net/2202_75469062/article/details/134211359?spm1001.2014.3001.5501 文章转载自: http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.llmhq.cn.gov.cn.llmhq.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.zyslyq.cn.gov.cn.zyslyq.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.qxmnf.cn.gov.cn.qxmnf.cn http://www.morning.gxtbn.cn.gov.cn.gxtbn.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn