如何在 TensorFlow 中使用 BERT 问答与 Python

一、介绍

Tensorflow 是一个开源的端到端平台,用于构建机器学习驱动的应用程序。它包含一组丰富的工具和库,用于各种任务,但主要侧重于神经网络的训练和推理。Tensorflow用于多种应用,包括:

  • 搜索引擎 – 用于部署深度神经网络进行搜索排名,如谷歌的RankBrain。
  • 汽车 – 构建专为自动驾驶设计的神经网络。
  • 教育 – 设计模型来过滤课堂上的有毒聊天消息。
  • Medicare – 用于构建神经网络以从患者数据中检测潜在的健康并发症。
  • 文本摘要和情感分析。

来自变压器的双向编码器表示 (BERT) 是一种自然语言模型,它使用基于转换器的机器学习技术来完成多种常见语言功能,包括:

  • 情感分析 – 情感推断,例如确定电影评论的极性。
  • 自然语言推理 – 确定假设是否在逻辑上遵循前提。
  • 命名实体识别 – 将非结构化文本中的信息提取到预定义的类别中,例如人员名称、位置和数量。
  • 问答 – 构建对话问题和回答系统,如聊天机器人。
  • 文本生成 – 生成与人类编写的文本无法区分的文本。
  • 文本联想查询 – 预测性文字建议,例如撰写电子邮件时 Gmail 的暗示性文字。
  • 文本摘要。

BERT经过了来自维基百科的25亿个单词和来自谷歌书语料库的8亿个单词的预训练。

本指南介绍了如何使用Python在TensorFlow中实现BERT的问答。

二、准备工作

  • Python 的工作知识。
  • 正确安装和配置的 python 工具链,包括 pip(Python 版本 >= 3.7)。

三、设置项目虚拟环境

为应用程序创建隔离的虚拟环境:

  1. 安装python软件包:virtualenv
    $ pip install virtualenv
    
  2. 创建项目目录:
    $ mkdir bert_QA
    
  3. 导航到新目录:
    $ cd bert_QA
    
  4. 创建虚拟环境:
    $ python3 -m venv env
    

    这将创建一个名为包含脚本的新文件夹,以控制虚拟环境,包括程序库。env

  5. 激活虚拟环境:
    $ source env/bin/activate
    

 四、安装 TensorFlow

要安装 TensorFlow,请输入以下命令:

$ pip install tensorflow

 五、Tflite 模型制造商

Tflite 模型制作器是一个库,可简化使用自定义数据集训练 Tensorflow Lite 模型的过程。它使用迁移学习来减少所需的训练数据和时间。

tflite 模型制作器库降低了在部署设备上 ML 应用程序时将 Tensorflow 神经网络模型转换为特定输入数据的复杂性。这些型号可以进行微调,以便在内存和 CPU 受限的设备(如智能手机)上运行,而不会牺牲在这些低功耗设备上运行时的精度。

本指南使用 tflite 模型制作器库来微调 BERT 模型以进行问答。

 六、安装Tflite模型制作工具

要安装 tflite 模型制作工具库,请执行以下操作:

  1. 克隆存储库:
    git clone https://github.com/tensorflow/examples
    
  2. 安装要求:
    pip install -r examples/tensorflow_examples/lite/model_maker/requirements.txt
    
  3. 安装软件包:
    pip install -e examples/tensorflow_examples/lite/model_maker/pip_package/
    

 七、构建精简模型

要创建负责问答的微调精简模型,请在工作目录中创建 afile:lite_model_gen.py

touch lite_model_gen.py

 7.1、导入库

通过将以下行添加到lite_model_gen.py文件来导入所需的库:

import tensorflow as tf

from tflite_model_maker import model_spec

from tflite_model_maker import question_answer

from tflite_model_maker.question_answer import DataLoader

从 tflit 模型制作者库中导入的类具有以下功能:

  • model_spec:用于选择表示模型的模型规范。
  • question_answer:用于数据加载和问答的模型训练。
  • DataLoader:提供用于在模型重新训练期间加载自定义数据的通用实用程序。

 7.2、选择型号规格

tflite模型制作器库支持BERT-Base和MobileBERT模型进行问答:

  • BERT-Base- 这是广泛用于NLP任务的标准BERT模型。
  • MobileBERT– 是 BERT-Base 的紧凑型版本,体积小约 4 倍,速度快近 6 倍。MobileBERT尽管比BERT-Base小,但仍能实现有竞争力的结果,并且更适合智能手机等功率受限的设备中的设备端用例。
  • MobileBERT-SQuAD- 该模型使用与MobileBERT相同的架构,但初始模型已经在斯坦福问答数据集(SQuAD)1.1上重新训练。SQuAD是一个阅读理解数据集,由众包工作者在一组维基百科文章中提出的问答对组成。SQuAD 1.1 包含 100,000+ 篇文章的 500+ 问答对。

要使用 MobileBERT-SQuAD 规范,请添加以下行:

# Model specification representing the model

spec = model_spec.get('mobilebert_qa_squad')

model_spec有一个方法 -,该方法将模型规范的名称作为参数。对于使用 BERT 的问答任务,它可以采用三个字符串中的任何一个作为参数:get

  • mobilebert_qa_squad:指定 MobileBERT-SQuAD。
  • mobilebert_qa:指定移动伯特。
  • bert_qa:指定 BERT-Base。

 7.3、获取训练数据

本指南使用 TriviaQA 数据集进行模型训练。TriviaQA 是一个用于阅读理解和问答的大规模数据集,包含超过650k个问答证据三元组。

若要加载数据集进行训练,应使用 TriviaQA 转换 python 脚本将其转换为 SQuAD1.1 格式。本指南将使用预转换的数据集进行训练和验证。

要下载转换后的数据集,请添加以下行:

# Download archived version of already converted datasets

train_data_path = tf.keras.utils.get_file(

      fname='triviaqa-web-train-8000.json',

      origin='https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-web-train-8000.json')



validation_data_path = tf.keras.utils.get_file(

      fname='triviaqa-verified-web-dev.json',

      origin='https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-verified-web-dev.json')

该函数用于从给定 URL 下载文件(如果缓存中尚不存在)。fname指定文件名,指定文件的原始 URL。这将下载数据集并返回下载文件的路径。tf.keras.utils.get_file

 7.4、加载数据集

通过添加以下行加载数据集:

# Fetch the training and validation data

train_data = DataLoader.from_squad(train_data_path, spec, is_training=True)

validation_data = DataLoader.from_squad(validation_data_path, spec, is_training=False)

DataLoader.from_squad以 SQuAD 格式加载传递的数据集,并根据给定model_spec对文本进行预处理。此方法将文件名和model_spec作为参数,可选参数包括 – 表示数据是否用于训练的布尔值。is_training

 7.5、创建模型

要创建 Tflite 模型,请执行以下操作:

# Create the model

model = question_answer.create(train_data, model_spec=spec)

类方法加载数据并训练模型以进行问答。它需要两个参数 – 训练数据和模型的规范。它还需要可选参数:question_answer.create

  • batch_size=None:用于训练的批量大小。
  • epochs=2:训练的周期数。
  • steps_per_epoch=None:在宣布一个纪元完成并开始下一个纪元之前,批次的样本。它默认为运行,直到输入数据集用尽。
  • shuffle=False:采用布尔值来确定是否应随机播放输入数据。

此方法返回问答的模型实例。

 7.6、评估模型

要使用验证数据集评估模型,请执行以下操作:

# Evaluate the model

model.evaluate(validation_data)

在模型对象上调用该方法会返回指标字典,包括分数和。evaluatef1exact_match

7.7、导出模型

要导出 tflite 模型以用于设备上的问答,请执行以下操作:

# Export the model

model.export(export_dir='.')

这会以默认格式将模型导出到当前工作目录。其他导出格式包括和。TFLITEVOCABSAVED_MODEL

7.8、模型生成代码

最终代码:lite_model_gen.py

import tensorflow as tf

from tflite_model_maker import model_spec

from tflite_model_maker import question_answer

from tflite_model_maker.question_answer import DataLoader



# Model specification representing the model

spec = model_spec.get('mobilebert_qa_squad')



# Download archived version of already converted datasets

train_data_path = tf.keras.utils.get_file(

fname='triviaqa-web-train-8000.json', origin='https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-web-train-8000.json')



validation_data_path = tf.keras.utils.get_file(

fname='triviaqa-verified-web-dev.json',

origin='https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-verified-web-dev.json')



# Fetch the training and validation data

train_data = DataLoader.from_squad(train_data_path, spec, is_training=True)

validation_data = DataLoader.from_squad(validation_data_path, spec, is_training=False)



# Create the model

model = question_answer.create(train_data, model_spec=spec)



# Evaluate the model

model.evaluate(validation_data)



# Export the model

model.export(export_dir='.')

 八、运行代码

运行代码:

$ python3 lite_model_gen.py

注意:代码运行时介于一小时到几小时之间,具体取决于 CPU 性能或是否存在 GPU。

输出如下所示:

…

….

Downloading data from https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-web-train-8000.json

32571392/32570663 [==============================] - 0s 0us/step

32579584/32570663 [==============================] - 0s 0us/step

Downloading data from https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-verified-web-dev.json

1171456/1167744 [==============================] - 0s 0us/step

1179648/1167744 [==============================] - 0s 0us/step



INFO:tensorflow:Retraining the models...

INFO:tensorflow:Retraining the models...

Epoch 1/2

1067/1067 [==============================] - 70867s 66s/step - loss: 1.1337 - start_positions_loss: 1.1310 - end_positions_loss: 1.1363

Epoch 2/2

1067/1067 [==============================] - 70983s 67s/step - loss: 0.7942 - start_positions_loss: 0.7934 - end_positions_loss: 0.7949



INFO:tensorflow:Made predictions for 200 records.

INFO:tensorflow:Made predictions for 200 records.

…

…

{'exact_match': 0.5986394557823129, 'final_f1': 0.6728435963129841}

…

…

INFO:tensorflow:TensorFlow Lite model exported successfully: ./model.tflite

成功运行后,精简模型将导出到当前项目目录。

 九、建筑问答

为了集成导出的模型以进行问答,本指南使用 TFlite 支持工具包。该工具包附带强大的库,可将TFLite模型集成到不同的平台上。

要安装:

$ pip install tflite-support

现在,创建问答脚本:

$ touch question_answer.py

9.1、导入库

导入类:BertQuestionAnswerer

from tflite_support.task.text import BertQuestionAnswerer

BertQuestionAnswerer类对文本执行问答。

9.2、创建 BertQuestionAnswerer 对象

从导出的精简模型创建BertQuestionAnswerer对象:

# Create the BertQuestionAnswerer object from a TensorFlow lite model

question_answerer = BertQuestionAnswerer.create_from_file("./model.tflite")

这将返回从模型文件创建的BertQuestionAnswerer对象。

9.3、创建问答上下文

问答需要一个上下文。这个上下文可以是提出问题的段落或句子。以下关于亚历山大大帝的文字来自维基百科。这将用作问答的上下文:

# Create context for question answering

context = "Alexander the Great was a king of the ancient Greek kingdom of Macedon. He succeeded his father Philip II to the throne in 336 BC at the age of 20, and spent most of his ruling years conducting a lengthy military campaign throughout Western Asia and Egypt. By the age of thirty, he had created one of the largest empires in history, stretching from Greece to northwestern India. He was undefeated in battle and is widely considered to be one of history's greatest and most successful military commanders. Until the age of 16, Alexander was tutored by Aristotle. In 335 BC, shortly after his assumption of kingship over Macedon, he campaigned in the Balkans and reasserted control over Thrace and Illyria before marching on the city of Thebes, which was subsequently destroyed in battle. Alexander then led the League of Corinth, and used his authority to launch the pan-Hellenic project envisaged by his father, assuming leadership over all Greeks in their conquest of Persia."

9.4、创建问题

根据上述上下文创建一个字典来保存问答对:

# Create questions

questions = {

    "Who is Alexander the Great": None,

    "Until the age of 16, who tutored Alexander": None,

}

9.5、回答问题

要回答字典中的问题:

# Answer questions

for question in questions.keys():

   answer = question_answerer.answer(context, question)

   questions[question] = answer



print(questions)

该方法有两个参数,一个上下文和一个问题。它根据上下文回答问题并返回 a.QuestionAnswererResult 是由 BertQuestionAnswerer生成的可能答案列表。answerQuestionAnswererResult

十、最终问答代码

完整的 question_answer.py 代码:

from tflite_support.task.text import BertQuestionAnswerer



# Create the BertQuestionAnswerer object from a TensorFlow lite model

question_answerer = BertQuestionAnswerer.create_from_file("./model.tflite")



# Create context for question answering

context = "Alexander the Great was a king of the ancient Greek kingdom of Macedon. He succeeded his father Philip II to the throne in 336 BC at the age of 20, and spent most of his ruling years conducting a lengthy military campaign throughout Western Asia and Egypt. By the age of thirty, he had created one of the largest empires in history, stretching from Greece to northwestern India. He was undefeated in battle and is widely considered to be one of history's greatest and most successful military commanders. Until the age of 16, Alexander was tutored by Aristotle. In 335 BC, shortly after his assumption of kingship over Macedon, he campaigned in the Balkans and reasserted control over Thrace and Illyria before marching on the city of Thebes, which was subsequently destroyed in battle. Alexander then led the League of Corinth, and used his authority to launch the pan-Hellenic project envisaged by his father, assuming leadership over all Greeks in their conquest of Persia."



# Create questions

questions = {

    "Who is Alexander the Great": None,

    "Until the age of 16, who tutored Alexander": None,

}



# Answer questions

for question in questions.keys():

   answer = question_answerer.answer(context, question)

   questions[question] = answer



print(questions)

 十一、运行代码

运行上面的代码:

$ python3 question_answer.py

这将产生以下结果:

{

  'Who is Alexander the Great': QuestionAnswererResult(answers=[

      QaAnswer(pos=Pos(start=12, end=20, logit=-1.621170163154602), text='king of the ancient Greek kingdom of Macedon.'), 

      QaAnswer(pos=Pos(start=12, end=27, logit=-2.1207242012023926), text='king of the ancient Greek kingdom of Macedon. He succeeded his father Philip II'),

      QaAnswer(pos=Pos(start=19, end=20, logit=-3.1698760986328125), text='Macedon.'), 

      QaAnswer(pos=Pos(start=26, end=27, logit=-3.3418025970458984), text='Philip II'),

      QaAnswer(pos=Pos(start=12, end=12, logit=-3.3852314949035645), text='king')]), 



  'Until the age of 16, who tutored Alexander': QuestionAnswererResult(answers=[

      QaAnswer(pos=Pos(start=121, end=121, logit=7.933090686798096), text='Aristotle.'), 

      QaAnswer(pos=Pos(start=118, end=121, logit=1.3499608039855957), text='tutored by Aristotle.'), 

      QaAnswer(pos=Pos(start=121, end=122, logit=1.0493016242980957), text='Aristotle.'), 

      QaAnswer(pos=Pos(start=110, end=121, logit=0.37497782707214355), text='Until the age of 16, Alexander was tutored by Aristotle.'), 

      QaAnswer(pos=Pos(start=118, end=119, logit=-5.260964870452881), text='tutored')])

}

返回的QuestionAnswererResult对象包含一个列表,这些列表表示所提出问题的可能答案。标记答案在上下文中的相对位置,同时表示答案文本。QaAnswerpostext

在第一个问题中,返回了五个可能的答案,其中两个是正确的,而在第二个问题中 – 5 个可能答案中有 4 个是正确的。

十二、结论

本指南介绍了如何在TensorFlow中使用BERT,方法是构建一个精简的问答模型,并使用Tflite支持库在上下文中回答问题。

赞(0)
未经允许不得转载:主机百科 » 如何在 TensorFlow 中使用 BERT 问答与 Python