关闭

Python 教你三分钟用 Bert 搭建问答搜索引擎

发表于:2024-4-30 09:36

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Ckend    来源:Python实用宝典

  鼎鼎大名的 Bert 算法相信大部分同学都听说过,它是Google推出的NLP领域“王炸级”预训练模型,其在NLP任务中刷新了多项记录,并取得state of the art的成绩。
  但是有很多深度学习的新手发现BERT模型并不好搭建,上手难度很高,普通人可能要研究几天才能勉强搭建出一个模型。
  没关系,今天我们介绍的这个模块,能让你在3分钟内基于BERT算法搭建一个问答搜索引擎。它就是 bert-as-service 项目。这个开源项目,能够让你基于多GPU机器快速搭建BERT服务(支持微调模型),并且能够让多个客户端并发使用。
  1.准备
  开始之前,你要确保Python和pip已经成功安装在电脑上,如果没有,可以访问这篇文章:超详细Python安装指南 进行安装。
  (可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda:Python数据分析与挖掘好帮手—Anaconda,它内置了Python和pip.
  (可选2) 此外,推荐大家用VSCode编辑器,它有许多的优点:Python 编程的最好搭档—VSCode 详细指南。
  请选择以下任一种方式输入命令安装依赖:1. Windows 环境 打开 Cmd (开始-运行-CMD)。2. MacOS 环境 打开 Terminal (command+空格输入Terminal)。3. 如果你用的是 VSCode编辑器 或 Pycharm,可以直接使用界面下方的Terminal.
  pip install bert-serving-server # 服务端
  pip install bert-serving-client # 客户端
  请注意,服务端的版本要求:Python >= 3.5,Tensorflow >= 1.10 。
  此外还要下载预训练好的BERT模型,在 https://github.com/hanxiao/bert-as-service#install 上可以下载,如果你无法访问该网站,也可以在 https://pythondict.com/download/bert-serving-model/ 此处下载。
  也可在Python实用宝典后台回复 bert-as-service 下载这些预训练好的模型。
  下载完成后,将 zip 文件解压到某个文件夹中,例如 /tmp/uncased_L-24_H-1024_A-16/.
  2.Bert-as-service 基本使用
  安装完成后,输入以下命令启动BERT服务:
  bert-serving-start -model_dir /tmp/uncased_L-24_H-1024_A-16/ -num_worker=4
  -num_worker=4 代表这将启动一个有四个worker的服务,意味着它最多可以处理四个并发请求。超过4个其他并发请求将在负载均衡器中排队等待处理。
  下面显示了正确启动时服务器的样子:
  使用客户端获取语句的编码
  现在你可以简单地对句子进行编码,如下所示:
  from bert_serving.client import BertClient
  bc = BertClient()
  bc.encode(['First do it', 'then do it right', 'then do it better'])
  作为 BERT 的一个特性,你可以通过将它们与 |||(前后有空格)连接来获得一对句子的编码,例如
  bc.encode(['First do it ||| then do it right'])
  远程使用 BERT 服务
  你还可以在一台 (GPU) 机器上启动服务并从另一台 (CPU) 机器上调用它,如下所示:
  # on another CPU machine
  from bert_serving.client import BertClient
  bc = BertClient(ip='xx.xx.xx.xx') # ip address of the GPU machine
  bc.encode(['First do it', 'then do it right', 'then do it better'])
  3.搭建问答搜索引擎
  我们将通过 bert-as-service 从FAQ 列表中找到与用户输入的问题最相似的问题,并返回相应的答案。
  FAQ列表其实就是官方文档的readme.md, 在我提供的下载链接里也附带了。
  加载所有问题,并显示统计数据:
  prefix_q = '##### **Q:** '
  with open('README.md') as fp:
      questions = [v.replace(prefix_q, '').strip() for v in fp if v.strip() and v.startswith(prefix_q)]
      print('%d questions loaded, avg. len of %d' % (len(questions), np.mean([len(d.split()) for d in questions])))
      # 33 questions loaded, avg. len of 9
  一共有33个问题被加载,平均长度是9.
  然后使用预训练好的模型:uncased_L-12_H-768_A-12 启动一个Bert服务:
  bert-serving-start -num_worker=1 -model_dir=/data/cips/data/lab/data/model/uncased_L-12_H-768_A-12
  接下来,将我们的问题编码为向量:
  bc = BertClient(port=4000, port_out=4001)
  doc_vecs = bc.encode(questions)
  最后,我们准备好接收用户的查询,并对现有问题执行简单的“模糊”搜索。
  为此,每次有新查询到来时,我们将其编码为向量并计算其点积  doc_vecs 然后对结果进行降序排序,返回前N个类似的问题:
  while True:
      query = input('your question: ')
      query_vec = bc.encode([query])[0]
      # compute normalized dot product as score
      score = np.sum(query_vec * doc_vecs, axis=1) / np.linalg.norm(doc_vecs, axis=1)
      topk_idx = np.argsort(score)[::-1][:topk]
      for idx in topk_idx:
          print('> %s\t%s' % (score[idx], questions[idx]))
  完成!现在运行代码并输入你的查询,看看这个搜索引擎如何处理模糊匹配:
  完整代码如下,一共23行代码:
  import numpy as np
  from bert_serving.client import BertClient
  from termcolor import colored
  prefix_q = '##### **Q:** '
  topk = 5
  with open('README.md') as fp:
      questions = [v.replace(prefix_q, '').strip() for v in fp if v.strip() and v.startswith(prefix_q)]
      print('%d questions loaded, avg. len of %d' % (len(questions), np.mean([len(d.split()) for d in questions])))
  with BertClient(port=4000, port_out=4001) as bc:
      doc_vecs = bc.encode(questions)
      while True:
          query = input(colored('your question: ', 'green'))
          query_vec = bc.encode([query])[0]
          # compute normalized dot product as score
          score = np.sum(query_vec * doc_vecs, axis=1) / np.linalg.norm(doc_vecs, axis=1)
          topk_idx = np.argsort(score)[::-1][:topk]
          print('top %d questions similar to "%s"' % (topk, colored(query, 'green')))
          for idx in topk_idx:
              print('> %s\t%s' % (colored('%.1f' % score[idx], 'cyan'), colored(questions[idx], 'yellow')))
  够简单吧?当然,这是一个基于预训练的Bert模型制造的一个简单QA搜索模型。
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号