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