• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# BERT Question and Answer
2
3Use a TensorFlow Lite model to answer questions based on the content of a given
4passage.
5
6Note: (1) To integrate an existing model, try
7[TensorFlow Lite Task Library](https://www.tensorflow.org/lite/inference_with_metadata/task_library/bert_question_answerer).
8(2) To customize a model, try
9[TensorFlow Lite Model Maker](https://www.tensorflow.org/lite/models/modify/model_maker/question_answer).
10
11## Get started
12
13<img src="images/screenshot.gif" class="attempt-right" style="max-width: 300px">
14
15If you are new to TensorFlow Lite and are working with Android or iOS, we
16recommend exploring the following example applications that can help you get
17started.
18
19<a class="button button-primary" href="https://github.com/tensorflow/examples/tree/master/lite/examples/bert_qa/android">Android
20example</a>
21<a class="button button-primary" href="https://github.com/tensorflow/examples/tree/master/lite/examples/bert_qa/ios">iOS
22example</a>
23
24If you are using a platform other than Android/iOS, or you are already familiar
25with the
26[TensorFlow Lite APIs](https://www.tensorflow.org/api_docs/python/tf/lite), you
27can download our starter question and answer model.
28
29<a class="button button-primary" href="https://tfhub.dev/tensorflow/lite-model/mobilebert/1/metadata/1?lite-format=tflite">Download
30starter model and vocab</a>
31
32For more information about metadata and associated fields (e.g. `vocab.txt`) see
33<a href="https://www.tensorflow.org/lite/models/convert/metadata#read_the_metadata_from_models">Read
34the metadata from models</a>.
35
36## How it works
37
38The model can be used to build a system that can answer users’ questions in
39natural language. It was created using a pre-trained BERT model fine-tuned on
40SQuAD 1.1 dataset.
41
42[BERT](https://github.com/google-research/bert), or Bidirectional Encoder
43Representations from Transformers, is a method of pre-training language
44representations which obtains state-of-the-art results on a wide array of
45Natural Language Processing tasks.
46
47This app uses a compressed version of BERT, MobileBERT, that runs 4x faster and
48has 4x smaller model size.
49
50[SQuAD](https://rajpurkar.github.io/SQuAD-explorer/), or Stanford Question
51Answering Dataset, is a reading comprehension dataset consisting of articles
52from Wikipedia and a set of question-answer pairs for each article.
53
54The model takes a passage and a question as input, then returns a segment of the
55passage that most likely answers the question. It requires semi-complex
56pre-processing including tokenization and post-processing steps that are
57described in the BERT [paper](https://arxiv.org/abs/1810.04805) and implemented
58in the sample app.
59
60## Performance benchmarks
61
62Performance benchmark numbers are generated with the tool
63[described here](https://www.tensorflow.org/lite/performance/benchmarks).
64
65<table>
66  <thead>
67    <tr>
68      <th>Model Name</th>
69      <th>Model size </th>
70      <th>Device </th>
71      <th>CPU</th>
72    </tr>
73  </thead>
74  <tr>
75    <td rowspan = 3>
76      <a href="https://tfhub.dev/tensorflow/lite-model/mobilebert/1/metadata/1?lite-format=tflite">Mobile Bert</a>
77    </td>
78    <td rowspan = 3>
79      100.5 Mb
80    </td>
81    <td>Pixel 3 (Android 10) </td>
82    <td>123ms*</td>
83  </tr>
84   <tr>
85     <td>Pixel 4 (Android 10) </td>
86    <td>74ms*</td>
87  </tr>
88   <tr>
89     <td>iPhone XS (iOS 12.4.1) </td>
90    <td>257ms** </td>
91  </tr>
92</table>
93
94\* 4 threads used.
95
96\*\* 2 threads used on iPhone for the best performance result.
97
98## Example output
99
100### Passage (Input)
101
102> Google LLC is an American multinational technology company that specializes in
103> Internet-related services and products, which include online advertising
104> technologies, search engine, cloud computing, software, and hardware. It is
105> considered one of the Big Four technology companies, alongside Amazon, Apple,
106> and Facebook.
107>
108> Google was founded in September 1998 by Larry Page and Sergey Brin while they
109> were Ph.D. students at Stanford University in California. Together they own
110> about 14 percent of its shares and control 56 percent of the stockholder
111> voting power through supervoting stock. They incorporated Google as a
112> California privately held company on September 4, 1998, in California. Google
113> was then reincorporated in Delaware on October 22, 2002. An initial public
114> offering (IPO) took place on August 19, 2004, and Google moved to its
115> headquarters in Mountain View, California, nicknamed the Googleplex. In August
116> 2015, Google announced plans to reorganize its various interests as a
117> conglomerate called Alphabet Inc. Google is Alphabet's leading subsidiary and
118> will continue to be the umbrella company for Alphabet's Internet interests.
119> Sundar Pichai was appointed CEO of Google, replacing Larry Page who became the
120> CEO of Alphabet.
121
122### Question (Input)
123
124> Who is the CEO of Google?
125
126### Answer (Output)
127
128> Sundar Pichai
129
130## Read more about BERT
131
132*   Academic paper: [BERT: Pre-training of Deep Bidirectional Transformers for
133    Language Understanding](https://arxiv.org/abs/1810.04805)
134*   [Open-source implementation of BERT](https://github.com/google-research/bert)
135