Created
December 2, 2024 00:20
-
-
Save davidmezzetti/235be648308f2f151d5224fc709c2da8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Comparison between Apache Tika and Docling\n", | |
"\n", | |
"[Apache Tika](https://github.com/apache/tika) has been around since the late 2000s. It's written in Java and has been used by countless enterprise organizations.\n", | |
"\n", | |
"[Docling](https://github.com/DS4SD/docling) is a new open-source text extraction library that gained popularity in late 2024.\n", | |
"\n", | |
"This notebook compares the speed and accuracy between Apache Tika and Docling in Python.\n", | |
"\n", | |
"Is newer always better? Let's take a look and see." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Install and Prepare\n", | |
"\n", | |
"The first step is installing the libraries.\n", | |
"\n", | |
"```bash\n", | |
"pip install docling tika\n", | |
"\n", | |
"mkdir -p /tmp/txtai\n", | |
"wget -N https://github.com/neuml/txtai/releases/download/v6.2.0/tests.tar.gz -P /tmp\n", | |
"tar -xvzf /tmp/tests.tar.gz -C /tmp\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Create the test code\n", | |
"\n", | |
"The following code runs text extraction for Tika and Docling. It also measures the elapsed runtime." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from docling.document_converter import DocumentConverter\n", | |
"\n", | |
"from tika import parser\n", | |
"\n", | |
"import time\n", | |
"\n", | |
"def run(fn, path):\n", | |
" start = time.time()\n", | |
" result = fn(path)\n", | |
" print(time.time() - start)\n", | |
" return result\n", | |
"\n", | |
"def tika(path):\n", | |
" parsed = parser.from_file(path, xmlContent=True)\n", | |
" return parsed[\"content\"]\n", | |
"\n", | |
"def docling(path):\n", | |
" return converter.convert(path).document.export_to_html()\n", | |
"\n", | |
"converter = DocumentConverter()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Speed\n", | |
"\n", | |
"First, let's compare the runtime performance between Apache Tika and Docling for a 1 page PDF file." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.007306098937988281\n" | |
] | |
} | |
], | |
"source": [ | |
"_ = run(tika, \"/tmp/txtai/article.pdf\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "19d725e7baee4bf781c8ea8ea27b6134", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Fetching 9 files: 0%| | 0/9 [00:00<?, ?it/s]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3.4919795989990234\n" | |
] | |
} | |
], | |
"source": [ | |
"_ = run(docling, \"/tmp/txtai/article.pdf\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"`0.007s` for Tika vs `3.49s` for Docling. Docling takes longer as it uses vision machine learning models to parse PDFs. Apache Tika simply extracts the text elements from the PDF.\n", | |
"\n", | |
"Next, we'll compare a DOCX file." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.02512383460998535\n" | |
] | |
} | |
], | |
"source": [ | |
"_ = run(tika, \"/tmp/txtai/document.docx\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.014896392822265625\n" | |
] | |
} | |
], | |
"source": [ | |
"_ = run(docling, \"/tmp/txtai/document.docx\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This time it's `0.03s` for Tika vs `0.01s` for Docling. DOCX is a format that is easier to work with programatically. Vision models aren't required. The content can be extracted with standard rules-based logic." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Accuracy\n", | |
"\n", | |
"Let's review the extracted output for DOCX, starting with Tika." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.027918338775634766\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<blockquote><html xmlns=\"http://www.w3.org/1999/xhtml\">\n", | |
"<head>\n", | |
"<meta name=\"cp:revision\" content=\"11\" />\n", | |
"<meta name=\"extended-properties:AppVersion\" content=\"15.0000\" />\n", | |
"<meta name=\"dc:language\" content=\"en-US\" />\n", | |
"<meta name=\"meta:paragraph-count\" content=\"50\" />\n", | |
"<meta name=\"meta:word-count\" content=\"237\" />\n", | |
"<meta name=\"extended-properties:Application\" content=\"LibreOffice/7.4.7.2$Linux_X86_64 LibreOffice_project/40$Build-2\" />\n", | |
"<meta name=\"xmpTPg:NPages\" content=\"2\" />\n", | |
"<meta name=\"resourceName\" content=\"b'document.docx'\" />\n", | |
"<meta name=\"dcterms:created\" content=\"2023-11-28T09:47:29Z\" />\n", | |
"<meta name=\"dcterms:modified\" content=\"2023-11-28T15:00:41Z\" />\n", | |
"<meta name=\"meta:character-count\" content=\"1405\" />\n", | |
"<meta name=\"extended-properties:Template\" content=\"\" />\n", | |
"<meta name=\"meta:character-count-with-spaces\" content=\"1589\" />\n", | |
"<meta name=\"X-TIKA:Parsed-By\" content=\"org.apache.tika.parser.DefaultParser\" />\n", | |
"<meta name=\"X-TIKA:Parsed-By\" content=\"org.apache.tika.parser.microsoft.ooxml.OOXMLParser\" />\n", | |
"<meta name=\"extended-properties:DocSecurityString\" content=\"None\" />\n", | |
"<meta name=\"extended-properties:TotalTime\" content=\"15\" />\n", | |
"<meta name=\"Content-Length\" content=\"7650\" />\n", | |
"<meta name=\"meta:page-count\" content=\"2\" />\n", | |
"<meta name=\"Content-Type\" content=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document\" />\n", | |
"<title></title>\n", | |
"</head>\n", | |
"<body><h1>txtai – the all-in-one embeddings database</h1>\n", | |
"<p class=\"body_Text\">txtai is an all-in-one embeddings database for semantic search, LLM orchestration and language model workflows.</p>\n", | |
"<p />\n", | |
"<p class=\"body_Text\">Summary of txtai features:</p>\n", | |
"<p class=\"body_Text\">· <i>Vector search</i> with SQL, object storage, topic modeling</p>\n", | |
"<p class=\"body_Text\">· Create <i>embeddings</i> for text, documents, audio, images and video</p>\n", | |
"<p class=\"body_Text\">· <i>Pipelines</i> powered by language models that run LLM prompts</p>\n", | |
"<p class=\"body_Text\">· <i>Workflows</i> to join pipelines together and aggregate business logic</p>\n", | |
"<p class=\"body_Text\">· Build with <i>Python</i> or <i>YAML</i>. API bindings available for JavaScript, Java, Rust and Go.</p>\n", | |
"<p class=\"body_Text\">· <i>Run local or scale out with container orchestration</i></p>\n", | |
"<h2 />\n", | |
"<h2>Examples</h2>\n", | |
"<p class=\"body_Text\">List of example notebooks.</p>\n", | |
"<table><tbody><tr>\t<td><p class=\"table_Heading\">Notebook</p>\n", | |
"</td>\t<td><p class=\"table_Heading\">Description</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Introducing txtai </p>\n", | |
"</td>\t<td><p>Overview of the functionality provided by txtai</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Similarity search with images</p>\n", | |
"</td>\t<td><p>Embed images and text into the same space for search</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Build a QA database</p>\n", | |
"</td>\t<td><p>Question matching with semantic search</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Semantic Graphs</p>\n", | |
"</td>\t<td><p>Explore topics, data connectivity and run network analysis</p>\n", | |
"</td></tr>\n", | |
"</tbody></table>\n", | |
"<h2>\n", | |
"Install</h2>\n", | |
"<p class=\"body_Text\">The easiest way to install is via pip and PyPI</p>\n", | |
"<p class=\"preformatted_Text\">pip install txtai</p>\n", | |
"<p class=\"body_Text\">Python 3.8+ is supported. Using a Python virtual environment is <b>recommended</b>.</p>\n", | |
"<p class=\"body_Text\">See the detailed install instructions for more information covering optional dependencies, environment specific prerequisites, installing from source, conda support and how to run with containers.</p>\n", | |
"<p class=\"body_Text\" />\n", | |
"<p class=\"body_Text\" />\n", | |
"<p class=\"body_Text\" />\n", | |
"<p class=\"body_Text\" />\n", | |
"<h2>Model guide</h2>\n", | |
"<p class=\"body_Text\">The following shows a list of suggested models.</p>\n", | |
"<table><tbody><tr>\t<td><p class=\"table_Heading\">Component</p>\n", | |
"</td>\t<td><p class=\"table_Heading\">Model(s)</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Embeddings</p>\n", | |
"</td>\t<td><p>all-MiniLM-L6-v2</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p />\n", | |
"</td>\t<td><p>E5-base-v2</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Image Captions</p>\n", | |
"</td>\t<td><p>BLIP</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Labels - Zero Shot</p>\n", | |
"</td>\t<td><p>BART-Large-MNLI</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Labels - Fixed</p>\n", | |
"</td>\t<td><p>Fine-tune with training pipeline</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Large Language Model (LLM)</p>\n", | |
"</td>\t<td><p>Flan T5 XL</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p />\n", | |
"</td>\t<td><p>Mistral 7B OpenOrca</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Summarization</p>\n", | |
"</td>\t<td><p>DistilBART</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Text-to-Speech</p>\n", | |
"</td>\t<td><p>ESPnet JETS</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Transcription</p>\n", | |
"</td>\t<td><p>Whisper</p>\n", | |
"</td></tr>\n", | |
"<tr>\t<td><p>Translation</p>\n", | |
"</td>\t<td><p>OPUS Model Series</p>\n", | |
"</td></tr>\n", | |
"</tbody></table>\n", | |
"<p class=\"body_Text\" />\n", | |
"</body></html></blockquote>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"from IPython.display import display, HTML\n", | |
"\n", | |
"def show(output):\n", | |
" display(HTML(f\"<blockquote>{output}</blockquote>\"))\n", | |
"\n", | |
"show(run(tika, \"/tmp/txtai/document.docx\"))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Tika preserves all section headers, tables and other text formatting.\n", | |
"\n", | |
"Now let's see what Docling does." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.013564348220825195\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<blockquote><!DOCTYPE html>\n", | |
"<html lang=\"en\">\n", | |
"<head>\n", | |
" <meta charset=\"UTF-8\">\n", | |
" <style>\n", | |
" table {\n", | |
" border-collapse: separate;\n", | |
" /* Maintain separate borders */\n", | |
" border-spacing: 5px; /*\n", | |
" Space between cells */\n", | |
" width: 50%;\n", | |
" }\n", | |
" th, td {\n", | |
" border: 1px solid black;\n", | |
" /* Add lines etween cells */\n", | |
" padding: 8px; }\n", | |
" </style>\n", | |
" </head>\n", | |
"<ul>\n", | |
"<li>txtai – the all-in-one embeddings database</li>\n", | |
"</ul>\n", | |
"<p>txtai is an all-in-one embeddings database for semantic search, LLM orchestration and language model workflows.</p>\n", | |
"<p></p>\n", | |
"<p>Summary of txtai features:</p>\n", | |
"<ul>\n", | |
"<li>Vector search with SQL, object storage, topic modeling</li>\n", | |
"<li>Create embeddings for text, documents, audio, images and video</li>\n", | |
"<li>Pipelines powered by language models that run LLM prompts</li>\n", | |
"<li>Workflows to join pipelines together and aggregate business logic</li>\n", | |
"<li>Build with Python or YAML. API bindings available for JavaScript, Java, Rust and Go.</li>\n", | |
"<li>Run local or scale out with container orchestration</li>\n", | |
"<li>Examples</li>\n", | |
"</ul>\n", | |
"<p>List of example notebooks.</p>\n", | |
"<table><tbody><tr><td>Notebook</td><td>Description</td></tr><tr><td>Introducing txtai</td><td>Overview of the functionality provided by txtai</td></tr><tr><td>Similarity search with images</td><td>Embed images and text into the same space for search</td></tr><tr><td>Build a QA database</td><td>Question matching with semantic search</td></tr><tr><td>Semantic Graphs</td><td>Explore topics, data connectivity and run network analysis</td></tr></tbody></table>\n", | |
"<h3>Install</h3>\n", | |
"<p>The easiest way to install is via pip and PyPI</p>\n", | |
"<p>pip install txtai</p>\n", | |
"<p>Python 3.8+ is supported. Using a Python virtual environment is recommended.</p>\n", | |
"<p>See the detailed install instructions for more information covering optional dependencies, environment specific prerequisites, installing from source, conda support and how to run with containers.</p>\n", | |
"<p></p>\n", | |
"<p></p>\n", | |
"<p></p>\n", | |
"<p></p>\n", | |
"<ul>\n", | |
"<li>Model guide</li>\n", | |
"</ul>\n", | |
"<p>The following shows a list of suggested models.</p>\n", | |
"<table><tbody><tr><td>Component</td><td>Model(s)</td></tr><tr><td>Embeddings</td><td>all-MiniLM-L6-v2</td></tr><tr><td></td><td>E5-base-v2</td></tr><tr><td>Image Captions</td><td>BLIP</td></tr><tr><td>Labels - Zero Shot</td><td>BART-Large-MNLI</td></tr><tr><td>Labels - Fixed</td><td>Fine-tune with training pipeline</td></tr><tr><td>Large Language Model (LLM)</td><td>Flan T5 XL</td></tr><tr><td></td><td>Mistral 7B OpenOrca</td></tr><tr><td>Summarization</td><td>DistilBART</td></tr><tr><td>Text-to-Speech</td><td>ESPnet JETS</td></tr><tr><td>Transcription</td><td>Whisper</td></tr><tr><td>Translation</td><td>OPUS Model Series</td></tr></tbody></table>\n", | |
"<p></p>\n", | |
"</html></blockquote>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"show(run(docling, \"/tmp/txtai/document.docx\"))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Very similar output from Docling. Just a couple minor regressions though. Note how the `main header` is a bullet along with the `model guide` header.\n", | |
"\n", | |
"Now, let's see how PDFs look." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"2024-12-01 19:15:21,585 [MainThread ] [INFO ] Retrieving https://arxiv.org/pdf/1810.04805 to /tmp/pdf-1810.04805.\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.2387542724609375\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<blockquote><html xmlns=\"http://www.w3.org/1999/xhtml\">\n", | |
"<head>\n", | |
"<meta name=\"pdf:PDFVersion\" content=\"1.5\" />\n", | |
"<meta name=\"xmp:CreatorTool\" content=\"LaTeX with hyperref package\" />\n", | |
"<meta name=\"pdf:hasXFA\" content=\"false\" />\n", | |
"<meta name=\"access_permission:modify_annotations\" content=\"true\" />\n", | |
"<meta name=\"access_permission:can_print_degraded\" content=\"true\" />\n", | |
"<meta name=\"dcterms:created\" content=\"2019-05-28T00:07:51Z\" />\n", | |
"<meta name=\"dcterms:modified\" content=\"2019-05-28T00:07:51Z\" />\n", | |
"<meta name=\"dc:format\" content=\"application/pdf; version=1.5\" />\n", | |
"<meta name=\"pdf:docinfo:creator_tool\" content=\"LaTeX with hyperref package\" />\n", | |
"<meta name=\"access_permission:fill_in_form\" content=\"true\" />\n", | |
"<meta name=\"pdf:docinfo:modified\" content=\"2019-05-28T00:07:51Z\" />\n", | |
"<meta name=\"pdf:hasCollection\" content=\"false\" />\n", | |
"<meta name=\"pdf:encrypted\" content=\"false\" />\n", | |
"<meta name=\"pdf:docinfo:custom:PTEX.Fullbanner\" content=\"This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016) kpathsea version 6.2.2\" />\n", | |
"<meta name=\"Content-Length\" content=\"775166\" />\n", | |
"<meta name=\"pdf:hasMarkedContent\" content=\"false\" />\n", | |
"<meta name=\"Content-Type\" content=\"application/pdf\" />\n", | |
"<meta name=\"PTEX.Fullbanner\" content=\"This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016) kpathsea version 6.2.2\" />\n", | |
"<meta name=\"pdf:producer\" content=\"pdfTeX-1.40.17\" />\n", | |
"<meta name=\"access_permission:extract_for_accessibility\" content=\"true\" />\n", | |
"<meta name=\"access_permission:assemble_document\" content=\"true\" />\n", | |
"<meta name=\"xmpTPg:NPages\" content=\"16\" />\n", | |
"<meta name=\"resourceName\" content=\"b'pdf-1810.04805'\" />\n", | |
"<meta name=\"pdf:hasXMP\" content=\"false\" />\n", | |
"<meta name=\"access_permission:extract_content\" content=\"true\" />\n", | |
"<meta name=\"access_permission:can_print\" content=\"true\" />\n", | |
"<meta name=\"pdf:docinfo:trapped\" content=\"False\" />\n", | |
"<meta name=\"X-TIKA:Parsed-By\" content=\"org.apache.tika.parser.DefaultParser\" />\n", | |
"<meta name=\"X-TIKA:Parsed-By\" content=\"org.apache.tika.parser.pdf.PDFParser\" />\n", | |
"<meta name=\"access_permission:can_modify\" content=\"true\" />\n", | |
"<meta name=\"pdf:docinfo:producer\" content=\"pdfTeX-1.40.17\" />\n", | |
"<meta name=\"pdf:docinfo:created\" content=\"2019-05-28T00:07:51Z\" />\n", | |
"<title></title>\n", | |
"</head>\n", | |
"<body><div class=\"page\"><p />\n", | |
"<p>BERT: Pre-training of Deep Bidirectional Transformers for\n", | |
"Language Understanding\n", | |
"</p>\n", | |
"<p>Jacob Devlin Ming-Wei Chang Kenton Lee Kristina Toutanova\n", | |
"Google AI Language\n", | |
"</p>\n", | |
"<p>{jacobdevlin,mingweichang,kentonl,kristout}@google.com\n", | |
"</p>\n", | |
"<p>Abstract\n", | |
"</p>\n", | |
"<p>We introduce a new language representa-\n", | |
"tion model called BERT, which stands for\n", | |
"Bidirectional Encoder Representations from\n", | |
"Transformers. Unlike recent language repre-\n", | |
"sentation models (Peters et al., 2018a; Rad-\n", | |
"ford et al., 2018), BERT is designed to pre-\n", | |
"train deep bidirectional representations from\n", | |
"unlabeled text by jointly conditioning on both\n", | |
"left and right context in all layers. As a re-\n", | |
"sult, the pre-trained BERT model can be fine-\n", | |
"tuned with just one additional output layer\n", | |
"to create state-of-the-art models for a wide\n", | |
"range of tasks, such as question answering and\n", | |
"language inference, without substantial task-\n", | |
"specific architecture modifications.\n", | |
"</p>\n", | |
"<p>BERT is conceptually simple and empirically\n", | |
"powerful. It obtains new state-of-the-art re-\n", | |
"sults on eleven natural language processing\n", | |
"tasks, including pushing the GLUE score to\n", | |
"80.5% (7.7% point absolute improvement),\n", | |
"MultiNLI accuracy to 86.7% (4.6% absolute\n", | |
"improvement), SQuAD v1.1 question answer-\n", | |
"ing Test F1 to 93.2 (1.5 point absolute im-\n", | |
"provement) and SQuAD v2.0 Test F1 to 83.1\n", | |
"(5.1 point absolute improvement).\n", | |
"</p>\n", | |
"<p>1 Introduction\n", | |
"</p>\n", | |
"<p>Language model pre-training has been shown to\n", | |
"be effective for improving many natural language\n", | |
"processing tasks (Dai and Le, 2015; Peters et al.,\n", | |
"2018a; Radford et al., 2018; Howard and Ruder,\n", | |
"2018). These include sentence-level tasks such as\n", | |
"natural language inference (Bowman et al., 2015;\n", | |
"Williams et al., 2018) and paraphrasing (Dolan\n", | |
"and Brockett, 2005), which aim to predict the re-\n", | |
"lationships between sentences by analyzing them\n", | |
"holistically, as well as token-level tasks such as\n", | |
"named entity recognition and question answering,\n", | |
"where models are required to produce fine-grained\n", | |
"output at the token level (Tjong Kim Sang and\n", | |
"De Meulder, 2003; Rajpurkar et al., 2016).\n", | |
"</p>\n", | |
"<p>There are two existing strategies for apply-\n", | |
"ing pre-trained language representations to down-\n", | |
"stream tasks: feature-based and fine-tuning. The\n", | |
"feature-based approach, such as ELMo (Peters\n", | |
"et al., 2018a), uses task-specific architectures that\n", | |
"include the pre-trained representations as addi-\n", | |
"tional features. The fine-tuning approach, such as\n", | |
"the Generative Pre-trained Transformer (OpenAI\n", | |
"GPT) (Radford et al., 2018), introduces minimal\n", | |
"task-specific parameters, and is trained on the\n", | |
"downstream tasks by simply fine-tuning all pre-\n", | |
"trained parameters. The two approaches share the\n", | |
"same objective function during pre-training, where\n", | |
"they use unidirectional language models to learn\n", | |
"general language representations.\n", | |
"</p>\n", | |
"<p>We argue that current techniques restrict the\n", | |
"power of the pre-trained representations, espe-\n", | |
"cially for the fine-tuning approaches. The ma-\n", | |
"jor limitation is that standard language models are\n", | |
"unidirectional, and this limits the choice of archi-\n", | |
"tectures that can be used during pre-training. For\n", | |
"example, in OpenAI GPT, the authors use a left-to-\n", | |
"right architecture, where every token can only at-\n", | |
"tend to previous tokens in the self-attention layers\n", | |
"of the Transformer (Vaswani et al., 2017). Such re-\n", | |
"strictions are sub-optimal for sentence-level tasks,\n", | |
"and could be very harmful when applying fine-\n", | |
"tuning based approaches to token-level tasks such\n", | |
"as question answering, where it is crucial to incor-\n", | |
"porate context from both directions.\n", | |
"</p>\n", | |
"<p>In this paper, we improve the fine-tuning based\n", | |
"approaches by proposing BERT: Bidirectional\n", | |
"Encoder Representations from Transformers.\n", | |
"BERT alleviates the previously mentioned unidi-\n", | |
"rectionality constraint by using a “masked lan-\n", | |
"guage model” (MLM) pre-training objective, in-\n", | |
"spired by the Cloze task (Taylor, 1953). The\n", | |
"masked language model randomly masks some of\n", | |
"the tokens from the input, and the objective is to\n", | |
"predict the original vocabulary id of the masked\n", | |
"</p>\n", | |
"<p>ar\n", | |
"X\n", | |
"</p>\n", | |
"<p>iv\n", | |
":1\n", | |
"</p>\n", | |
"<p>81\n", | |
"0.\n", | |
"</p>\n", | |
"<p>04\n", | |
"80\n", | |
"</p>\n", | |
"<p>5v\n", | |
"2 \n", | |
"</p>\n", | |
"<p> [\n", | |
"cs\n", | |
"</p>\n", | |
"<p>.C\n", | |
"L\n", | |
"</p>\n", | |
"<p>] \n", | |
" 2\n", | |
"</p>\n", | |
"<p>4 \n", | |
"M\n", | |
"</p>\n", | |
"<p>ay\n", | |
" 2\n", | |
"</p>\n", | |
"<p>01\n", | |
"9</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>word based only on its context. Unlike left-to-\n", | |
"right language model pre-training, the MLM ob-\n", | |
"jective enables the representation to fuse the left\n", | |
"and the right context, which allows us to pre-\n", | |
"train a deep bidirectional Transformer. In addi-\n", | |
"tion to the masked language model, we also use\n", | |
"a “next sentence prediction” task that jointly pre-\n", | |
"trains text-pair representations. The contributions\n", | |
"of our paper are as follows:\n", | |
"</p>\n", | |
"<p>• We demonstrate the importance of bidirectional\n", | |
"pre-training for language representations. Un-\n", | |
"like Radford et al. (2018), which uses unidirec-\n", | |
"tional language models for pre-training, BERT\n", | |
"uses masked language models to enable pre-\n", | |
"trained deep bidirectional representations. This\n", | |
"is also in contrast to Peters et al. (2018a), which\n", | |
"uses a shallow concatenation of independently\n", | |
"trained left-to-right and right-to-left LMs.\n", | |
"</p>\n", | |
"<p>• We show that pre-trained representations reduce\n", | |
"the need for many heavily-engineered task-\n", | |
"specific architectures. BERT is the first fine-\n", | |
"tuning based representation model that achieves\n", | |
"state-of-the-art performance on a large suite\n", | |
"of sentence-level and token-level tasks, outper-\n", | |
"forming many task-specific architectures.\n", | |
"</p>\n", | |
"<p>• BERT advances the state of the art for eleven\n", | |
"NLP tasks. The code and pre-trained mod-\n", | |
"els are available at https://github.com/\n", | |
"google-research/bert.\n", | |
"</p>\n", | |
"<p>2 Related Work\n", | |
"</p>\n", | |
"<p>There is a long history of pre-training general lan-\n", | |
"guage representations, and we briefly review the\n", | |
"most widely-used approaches in this section.\n", | |
"</p>\n", | |
"<p>2.1 Unsupervised Feature-based Approaches\n", | |
"Learning widely applicable representations of\n", | |
"words has been an active area of research for\n", | |
"decades, including non-neural (Brown et al., 1992;\n", | |
"Ando and Zhang, 2005; Blitzer et al., 2006) and\n", | |
"neural (Mikolov et al., 2013; Pennington et al.,\n", | |
"2014) methods. Pre-trained word embeddings\n", | |
"are an integral part of modern NLP systems, of-\n", | |
"fering significant improvements over embeddings\n", | |
"learned from scratch (Turian et al., 2010). To pre-\n", | |
"train word embedding vectors, left-to-right lan-\n", | |
"guage modeling objectives have been used (Mnih\n", | |
"and Hinton, 2009), as well as objectives to dis-\n", | |
"criminate correct from incorrect words in left and\n", | |
"right context (Mikolov et al., 2013).\n", | |
"</p>\n", | |
"<p>These approaches have been generalized to\n", | |
"coarser granularities, such as sentence embed-\n", | |
"dings (Kiros et al., 2015; Logeswaran and Lee,\n", | |
"2018) or paragraph embeddings (Le and Mikolov,\n", | |
"2014). To train sentence representations, prior\n", | |
"work has used objectives to rank candidate next\n", | |
"sentences (Jernite et al., 2017; Logeswaran and\n", | |
"Lee, 2018), left-to-right generation of next sen-\n", | |
"tence words given a representation of the previous\n", | |
"sentence (Kiros et al., 2015), or denoising auto-\n", | |
"encoder derived objectives (Hill et al., 2016).\n", | |
"</p>\n", | |
"<p>ELMo and its predecessor (Peters et al., 2017,\n", | |
"2018a) generalize traditional word embedding re-\n", | |
"search along a different dimension. They extract\n", | |
"context-sensitive features from a left-to-right and a\n", | |
"right-to-left language model. The contextual rep-\n", | |
"resentation of each token is the concatenation of\n", | |
"the left-to-right and right-to-left representations.\n", | |
"When integrating contextual word embeddings\n", | |
"with existing task-specific architectures, ELMo\n", | |
"advances the state of the art for several major NLP\n", | |
"benchmarks (Peters et al., 2018a) including ques-\n", | |
"tion answering (Rajpurkar et al., 2016), sentiment\n", | |
"analysis (Socher et al., 2013), and named entity\n", | |
"recognition (Tjong Kim Sang and De Meulder,\n", | |
"2003). Melamud et al. (2016) proposed learning\n", | |
"contextual representations through a task to pre-\n", | |
"dict a single word from both left and right context\n", | |
"using LSTMs. Similar to ELMo, their model is\n", | |
"feature-based and not deeply bidirectional. Fedus\n", | |
"et al. (2018) shows that the cloze task can be used\n", | |
"to improve the robustness of text generation mod-\n", | |
"els.\n", | |
"</p>\n", | |
"<p>2.2 Unsupervised Fine-tuning Approaches\n", | |
"</p>\n", | |
"<p>As with the feature-based approaches, the first\n", | |
"works in this direction only pre-trained word em-\n", | |
"bedding parameters from unlabeled text (Col-\n", | |
"lobert and Weston, 2008).\n", | |
"</p>\n", | |
"<p>More recently, sentence or document encoders\n", | |
"which produce contextual token representations\n", | |
"have been pre-trained from unlabeled text and\n", | |
"fine-tuned for a supervised downstream task (Dai\n", | |
"and Le, 2015; Howard and Ruder, 2018; Radford\n", | |
"et al., 2018). The advantage of these approaches\n", | |
"is that few parameters need to be learned from\n", | |
"scratch. At least partly due to this advantage,\n", | |
"OpenAI GPT (Radford et al., 2018) achieved pre-\n", | |
"viously state-of-the-art results on many sentence-\n", | |
"level tasks from the GLUE benchmark (Wang\n", | |
"et al., 2018a). Left-to-right language model-</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://github.com/google-research/bert\">https://github.com/google-research/bert</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://github.com/google-research/bert\">https://github.com/google-research/bert</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>BERT BERT\n", | |
"</p>\n", | |
"<p>E[CLS] E1 E[SEP]... EN E1’ ... EM’\n", | |
"</p>\n", | |
"<p>C T1 T[SEP]... TN T1’ ... TM’\n", | |
"</p>\n", | |
"<p>[CLS] Tok 1 [SEP]... Tok N Tok 1 ... TokM\n", | |
"</p>\n", | |
"<p>Question Paragraph\n", | |
"</p>\n", | |
"<p>Start/End Span\n", | |
"</p>\n", | |
"<p>BERT\n", | |
"</p>\n", | |
"<p>E[CLS] E1 E[SEP]... EN E1’ ... EM’\n", | |
"</p>\n", | |
"<p>C T1 T[SEP]... TN T1’ ... TM’\n", | |
"</p>\n", | |
"<p>[CLS] Tok 1 [SEP]... Tok N Tok 1 ... TokM\n", | |
"</p>\n", | |
"<p>Masked Sentence A Masked Sentence B\n", | |
"</p>\n", | |
"<p>Pre-training Fine-Tuning\n", | |
"</p>\n", | |
"<p>NSP Mask LM Mask LM\n", | |
"</p>\n", | |
"<p>Unlabeled Sentence A and B Pair \n", | |
"</p>\n", | |
"<p>SQuAD\n", | |
"</p>\n", | |
"<p>Question Answer Pair\n", | |
"</p>\n", | |
"<p>NERMNLI\n", | |
"</p>\n", | |
"<p>Figure 1: Overall pre-training and fine-tuning procedures for BERT. Apart from output layers, the same architec-\n", | |
"tures are used in both pre-training and fine-tuning. The same pre-trained model parameters are used to initialize\n", | |
"models for different down-stream tasks. During fine-tuning, all parameters are fine-tuned. [CLS] is a special\n", | |
"symbol added in front of every input example, and [SEP] is a special separator token (e.g. separating ques-\n", | |
"tions/answers).\n", | |
"</p>\n", | |
"<p>ing and auto-encoder objectives have been used\n", | |
"for pre-training such models (Howard and Ruder,\n", | |
"2018; Radford et al., 2018; Dai and Le, 2015).\n", | |
"</p>\n", | |
"<p>2.3 Transfer Learning from Supervised Data\n", | |
"</p>\n", | |
"<p>There has also been work showing effective trans-\n", | |
"fer from supervised tasks with large datasets, such\n", | |
"as natural language inference (Conneau et al.,\n", | |
"2017) and machine translation (McCann et al.,\n", | |
"2017). Computer vision research has also demon-\n", | |
"strated the importance of transfer learning from\n", | |
"large pre-trained models, where an effective recipe\n", | |
"is to fine-tune models pre-trained with Ima-\n", | |
"geNet (Deng et al., 2009; Yosinski et al., 2014).\n", | |
"</p>\n", | |
"<p>3 BERT\n", | |
"</p>\n", | |
"<p>We introduce BERT and its detailed implementa-\n", | |
"tion in this section. There are two steps in our\n", | |
"framework: pre-training and fine-tuning. Dur-\n", | |
"ing pre-training, the model is trained on unlabeled\n", | |
"data over different pre-training tasks. For fine-\n", | |
"tuning, the BERT model is first initialized with\n", | |
"the pre-trained parameters, and all of the param-\n", | |
"eters are fine-tuned using labeled data from the\n", | |
"downstream tasks. Each downstream task has sep-\n", | |
"arate fine-tuned models, even though they are ini-\n", | |
"tialized with the same pre-trained parameters. The\n", | |
"question-answering example in Figure 1 will serve\n", | |
"as a running example for this section.\n", | |
"</p>\n", | |
"<p>A distinctive feature of BERT is its unified ar-\n", | |
"chitecture across different tasks. There is mini-\n", | |
"</p>\n", | |
"<p>mal difference between the pre-trained architec-\n", | |
"ture and the final downstream architecture.\n", | |
"</p>\n", | |
"<p>Model Architecture BERT’s model architec-\n", | |
"ture is a multi-layer bidirectional Transformer en-\n", | |
"coder based on the original implementation de-\n", | |
"scribed in Vaswani et al. (2017) and released in\n", | |
"the tensor2tensor library.1 Because the use\n", | |
"of Transformers has become common and our im-\n", | |
"plementation is almost identical to the original,\n", | |
"we will omit an exhaustive background descrip-\n", | |
"tion of the model architecture and refer readers to\n", | |
"Vaswani et al. (2017) as well as excellent guides\n", | |
"such as “The Annotated Transformer.”2\n", | |
"</p>\n", | |
"<p>In this work, we denote the number of layers\n", | |
"(i.e., Transformer blocks) as L, the hidden size as\n", | |
"H , and the number of self-attention heads as A.3\n", | |
"</p>\n", | |
"<p>We primarily report results on two model sizes:\n", | |
"BERTBASE (L=12, H=768, A=12, Total Param-\n", | |
"eters=110M) and BERTLARGE (L=24, H=1024,\n", | |
"A=16, Total Parameters=340M).\n", | |
"</p>\n", | |
"<p>BERTBASE was chosen to have the same model\n", | |
"size as OpenAI GPT for comparison purposes.\n", | |
"Critically, however, the BERT Transformer uses\n", | |
"bidirectional self-attention, while the GPT Trans-\n", | |
"former uses constrained self-attention where every\n", | |
"token can only attend to context to its left.4\n", | |
"</p>\n", | |
"<p>1https://github.com/tensorflow/tensor2tensor\n", | |
"2http://nlp.seas.harvard.edu/2018/04/03/attention.html\n", | |
"3In all cases we set the feed-forward/filter size to be 4H ,\n", | |
"</p>\n", | |
"<p>i.e., 3072 for the H = 768 and 4096 for the H = 1024.\n", | |
"4We note that in the literature the bidirectional Trans-</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>Input/Output Representations To make BERT\n", | |
"handle a variety of down-stream tasks, our input\n", | |
"representation is able to unambiguously represent\n", | |
"both a single sentence and a pair of sentences\n", | |
"(e.g., 〈Question, Answer 〉) in one token sequence.\n", | |
"Throughout this work, a “sentence” can be an arbi-\n", | |
"trary span of contiguous text, rather than an actual\n", | |
"linguistic sentence. A “sequence” refers to the in-\n", | |
"put token sequence to BERT, which may be a sin-\n", | |
"gle sentence or two sentences packed together.\n", | |
"</p>\n", | |
"<p>We use WordPiece embeddings (Wu et al.,\n", | |
"2016) with a 30,000 token vocabulary. The first\n", | |
"token of every sequence is always a special clas-\n", | |
"sification token ([CLS]). The final hidden state\n", | |
"corresponding to this token is used as the ag-\n", | |
"gregate sequence representation for classification\n", | |
"tasks. Sentence pairs are packed together into a\n", | |
"single sequence. We differentiate the sentences in\n", | |
"two ways. First, we separate them with a special\n", | |
"token ([SEP]). Second, we add a learned embed-\n", | |
"ding to every token indicating whether it belongs\n", | |
"to sentence A or sentence B. As shown in Figure 1,\n", | |
"we denote input embedding as E, the final hidden\n", | |
"vector of the special [CLS] token as C ∈ RH ,\n", | |
"and the final hidden vector for the ith input token\n", | |
"as Ti ∈ RH .\n", | |
"</p>\n", | |
"<p>For a given token, its input representation is\n", | |
"constructed by summing the corresponding token,\n", | |
"segment, and position embeddings. A visualiza-\n", | |
"tion of this construction can be seen in Figure 2.\n", | |
"</p>\n", | |
"<p>3.1 Pre-training BERT\n", | |
"</p>\n", | |
"<p>Unlike Peters et al. (2018a) and Radford et al.\n", | |
"(2018), we do not use traditional left-to-right or\n", | |
"right-to-left language models to pre-train BERT.\n", | |
"Instead, we pre-train BERT using two unsuper-\n", | |
"vised tasks, described in this section. This step\n", | |
"is presented in the left part of Figure 1.\n", | |
"</p>\n", | |
"<p>Task #1: Masked LM Intuitively, it is reason-\n", | |
"able to believe that a deep bidirectional model is\n", | |
"strictly more powerful than either a left-to-right\n", | |
"model or the shallow concatenation of a left-to-\n", | |
"right and a right-to-left model. Unfortunately,\n", | |
"standard conditional language models can only be\n", | |
"trained left-to-right or right-to-left, since bidirec-\n", | |
"tional conditioning would allow each word to in-\n", | |
"directly “see itself”, and the model could trivially\n", | |
"predict the target word in a multi-layered context.\n", | |
"</p>\n", | |
"<p>former is often referred to as a “Transformer encoder” while\n", | |
"the left-context-only version is referred to as a “Transformer\n", | |
"decoder” since it can be used for text generation.\n", | |
"</p>\n", | |
"<p>In order to train a deep bidirectional representa-\n", | |
"tion, we simply mask some percentage of the input\n", | |
"tokens at random, and then predict those masked\n", | |
"tokens. We refer to this procedure as a “masked\n", | |
"LM” (MLM), although it is often referred to as a\n", | |
"Cloze task in the literature (Taylor, 1953). In this\n", | |
"case, the final hidden vectors corresponding to the\n", | |
"mask tokens are fed into an output softmax over\n", | |
"the vocabulary, as in a standard LM. In all of our\n", | |
"experiments, we mask 15% of all WordPiece to-\n", | |
"kens in each sequence at random. In contrast to\n", | |
"denoising auto-encoders (Vincent et al., 2008), we\n", | |
"only predict the masked words rather than recon-\n", | |
"structing the entire input.\n", | |
"</p>\n", | |
"<p>Although this allows us to obtain a bidirec-\n", | |
"tional pre-trained model, a downside is that we\n", | |
"are creating a mismatch between pre-training and\n", | |
"fine-tuning, since the [MASK] token does not ap-\n", | |
"pear during fine-tuning. To mitigate this, we do\n", | |
"not always replace “masked” words with the ac-\n", | |
"tual [MASK] token. The training data generator\n", | |
"chooses 15% of the token positions at random for\n", | |
"prediction. If the i-th token is chosen, we replace\n", | |
"the i-th token with (1) the [MASK] token 80% of\n", | |
"the time (2) a random token 10% of the time (3)\n", | |
"the unchanged i-th token 10% of the time. Then,\n", | |
"Ti will be used to predict the original token with\n", | |
"cross entropy loss. We compare variations of this\n", | |
"procedure in Appendix C.2.\n", | |
"</p>\n", | |
"<p>Task #2: Next Sentence Prediction (NSP)\n", | |
"Many important downstream tasks such as Ques-\n", | |
"tion Answering (QA) and Natural Language Infer-\n", | |
"ence (NLI) are based on understanding the rela-\n", | |
"tionship between two sentences, which is not di-\n", | |
"rectly captured by language modeling. In order\n", | |
"to train a model that understands sentence rela-\n", | |
"tionships, we pre-train for a binarized next sen-\n", | |
"tence prediction task that can be trivially gener-\n", | |
"ated from any monolingual corpus. Specifically,\n", | |
"when choosing the sentences A and B for each pre-\n", | |
"training example, 50% of the time B is the actual\n", | |
"next sentence that follows A (labeled as IsNext),\n", | |
"and 50% of the time it is a random sentence from\n", | |
"the corpus (labeled as NotNext). As we show\n", | |
"in Figure 1, C is used for next sentence predic-\n", | |
"tion (NSP).5 Despite its simplicity, we demon-\n", | |
"strate in Section 5.1 that pre-training towards this\n", | |
"task is very beneficial to both QA and NLI. 6\n", | |
"</p>\n", | |
"<p>5The final model achieves 97%-98% accuracy on NSP.\n", | |
"6The vector C is not a meaningful sentence representation\n", | |
"</p>\n", | |
"<p>without fine-tuning, since it was trained with NSP.</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>[CLS] he likes play ##ing [SEP]my dog is cute [SEP]Input\n", | |
"</p>\n", | |
"<p>E[CLS] Ehe Elikes Eplay E##ing E[SEP]Emy Edog Eis Ecute E[SEP]\n", | |
"Token\n", | |
"Embeddings\n", | |
"</p>\n", | |
"<p>EA EB EB EB EB EBEA EA EA EA EA\n", | |
"Segment\n", | |
"Embeddings\n", | |
"</p>\n", | |
"<p>E0 E6 E7 E8 E9 E10E1 E2 E3 E4 E5\n", | |
"Position\n", | |
"Embeddings\n", | |
"</p>\n", | |
"<p>Figure 2: BERT input representation. The input embeddings are the sum of the token embeddings, the segmenta-\n", | |
"tion embeddings and the position embeddings.\n", | |
"</p>\n", | |
"<p>The NSP task is closely related to representation-\n", | |
"learning objectives used in Jernite et al. (2017) and\n", | |
"Logeswaran and Lee (2018). However, in prior\n", | |
"work, only sentence embeddings are transferred to\n", | |
"down-stream tasks, where BERT transfers all pa-\n", | |
"rameters to initialize end-task model parameters.\n", | |
"</p>\n", | |
"<p>Pre-training data The pre-training procedure\n", | |
"largely follows the existing literature on language\n", | |
"model pre-training. For the pre-training corpus we\n", | |
"use the BooksCorpus (800M words) (Zhu et al.,\n", | |
"2015) and English Wikipedia (2,500M words).\n", | |
"For Wikipedia we extract only the text passages\n", | |
"and ignore lists, tables, and headers. It is criti-\n", | |
"cal to use a document-level corpus rather than a\n", | |
"shuffled sentence-level corpus such as the Billion\n", | |
"Word Benchmark (Chelba et al., 2013) in order to\n", | |
"extract long contiguous sequences.\n", | |
"</p>\n", | |
"<p>3.2 Fine-tuning BERT\n", | |
"</p>\n", | |
"<p>Fine-tuning is straightforward since the self-\n", | |
"attention mechanism in the Transformer al-\n", | |
"lows BERT to model many downstream tasks—\n", | |
"whether they involve single text or text pairs—by\n", | |
"swapping out the appropriate inputs and outputs.\n", | |
"For applications involving text pairs, a common\n", | |
"pattern is to independently encode text pairs be-\n", | |
"fore applying bidirectional cross attention, such\n", | |
"as Parikh et al. (2016); Seo et al. (2017). BERT\n", | |
"instead uses the self-attention mechanism to unify\n", | |
"these two stages, as encoding a concatenated text\n", | |
"pair with self-attention effectively includes bidi-\n", | |
"rectional cross attention between two sentences.\n", | |
"</p>\n", | |
"<p>For each task, we simply plug in the task-\n", | |
"specific inputs and outputs into BERT and fine-\n", | |
"tune all the parameters end-to-end. At the in-\n", | |
"put, sentence A and sentence B from pre-training\n", | |
"are analogous to (1) sentence pairs in paraphras-\n", | |
"ing, (2) hypothesis-premise pairs in entailment, (3)\n", | |
"question-passage pairs in question answering, and\n", | |
"</p>\n", | |
"<p>(4) a degenerate text-∅ pair in text classification\n", | |
"or sequence tagging. At the output, the token rep-\n", | |
"resentations are fed into an output layer for token-\n", | |
"level tasks, such as sequence tagging or question\n", | |
"answering, and the [CLS] representation is fed\n", | |
"into an output layer for classification, such as en-\n", | |
"tailment or sentiment analysis.\n", | |
"</p>\n", | |
"<p>Compared to pre-training, fine-tuning is rela-\n", | |
"tively inexpensive. All of the results in the pa-\n", | |
"per can be replicated in at most 1 hour on a sin-\n", | |
"gle Cloud TPU, or a few hours on a GPU, starting\n", | |
"from the exact same pre-trained model.7 We de-\n", | |
"scribe the task-specific details in the correspond-\n", | |
"ing subsections of Section 4. More details can be\n", | |
"found in Appendix A.5.\n", | |
"</p>\n", | |
"<p>4 Experiments\n", | |
"</p>\n", | |
"<p>In this section, we present BERT fine-tuning re-\n", | |
"sults on 11 NLP tasks.\n", | |
"</p>\n", | |
"<p>4.1 GLUE\n", | |
"The General Language Understanding Evaluation\n", | |
"(GLUE) benchmark (Wang et al., 2018a) is a col-\n", | |
"lection of diverse natural language understanding\n", | |
"tasks. Detailed descriptions of GLUE datasets are\n", | |
"included in Appendix B.1.\n", | |
"</p>\n", | |
"<p>To fine-tune on GLUE, we represent the input\n", | |
"sequence (for single sentence or sentence pairs)\n", | |
"as described in Section 3, and use the final hid-\n", | |
"den vector C ∈ RH corresponding to the first\n", | |
"input token ([CLS]) as the aggregate representa-\n", | |
"tion. The only new parameters introduced during\n", | |
"fine-tuning are classification layer weights W ∈\n", | |
"RK×H , whereK is the number of labels. We com-\n", | |
"pute a standard classification loss with C and W ,\n", | |
"i.e., log(softmax(CW T )).\n", | |
"</p>\n", | |
"<p>7For example, the BERT SQuAD model can be trained in\n", | |
"around 30 minutes on a single Cloud TPU to achieve a Dev\n", | |
"F1 score of 91.0%.\n", | |
"</p>\n", | |
"<p>8See (10) in https://gluebenchmark.com/faq.</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://gluebenchmark.com/faq\">https://gluebenchmark.com/faq</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>System MNLI-(m/mm) QQP QNLI SST-2 CoLA STS-B MRPC RTE Average\n", | |
"392k 363k 108k 67k 8.5k 5.7k 3.5k 2.5k -\n", | |
"</p>\n", | |
"<p>Pre-OpenAI SOTA 80.6/80.1 66.1 82.3 93.2 35.0 81.0 86.0 61.7 74.0\n", | |
"BiLSTM+ELMo+Attn 76.4/76.1 64.8 79.8 90.4 36.0 73.3 84.9 56.8 71.0\n", | |
"OpenAI GPT 82.1/81.4 70.3 87.4 91.3 45.4 80.0 82.3 56.0 75.1\n", | |
"BERTBASE 84.6/83.4 71.2 90.5 93.5 52.1 85.8 88.9 66.4 79.6\n", | |
"BERTLARGE 86.7/85.9 72.1 92.7 94.9 60.5 86.5 89.3 70.1 82.1\n", | |
"</p>\n", | |
"<p>Table 1: GLUE Test results, scored by the evaluation server (https://gluebenchmark.com/leaderboard).\n", | |
"The number below each task denotes the number of training examples. The “Average” column is slightly different\n", | |
"than the official GLUE score, since we exclude the problematic WNLI set.8 BERT and OpenAI GPT are single-\n", | |
"model, single task. F1 scores are reported for QQP and MRPC, Spearman correlations are reported for STS-B, and\n", | |
"accuracy scores are reported for the other tasks. We exclude entries that use BERT as one of their components.\n", | |
"</p>\n", | |
"<p>We use a batch size of 32 and fine-tune for 3\n", | |
"epochs over the data for all GLUE tasks. For each\n", | |
"task, we selected the best fine-tuning learning rate\n", | |
"(among 5e-5, 4e-5, 3e-5, and 2e-5) on the Dev set.\n", | |
"Additionally, for BERTLARGE we found that fine-\n", | |
"tuning was sometimes unstable on small datasets,\n", | |
"so we ran several random restarts and selected the\n", | |
"best model on the Dev set. With random restarts,\n", | |
"we use the same pre-trained checkpoint but per-\n", | |
"form different fine-tuning data shuffling and clas-\n", | |
"sifier layer initialization.9\n", | |
"</p>\n", | |
"<p>Results are presented in Table 1. Both\n", | |
"BERTBASE and BERTLARGE outperform all sys-\n", | |
"tems on all tasks by a substantial margin, obtaining\n", | |
"4.5% and 7.0% respective average accuracy im-\n", | |
"provement over the prior state of the art. Note that\n", | |
"BERTBASE and OpenAI GPT are nearly identical\n", | |
"in terms of model architecture apart from the at-\n", | |
"tention masking. For the largest and most widely\n", | |
"reported GLUE task, MNLI, BERT obtains a 4.6%\n", | |
"absolute accuracy improvement. On the official\n", | |
"GLUE leaderboard10, BERTLARGE obtains a score\n", | |
"of 80.5, compared to OpenAI GPT, which obtains\n", | |
"72.8 as of the date of writing.\n", | |
"</p>\n", | |
"<p>We find that BERTLARGE significantly outper-\n", | |
"forms BERTBASE across all tasks, especially those\n", | |
"with very little training data. The effect of model\n", | |
"size is explored more thoroughly in Section 5.2.\n", | |
"</p>\n", | |
"<p>4.2 SQuAD v1.1\n", | |
"</p>\n", | |
"<p>The Stanford Question Answering Dataset\n", | |
"(SQuAD v1.1) is a collection of 100k crowd-\n", | |
"sourced question/answer pairs (Rajpurkar et al.,\n", | |
"2016). Given a question and a passage from\n", | |
"</p>\n", | |
"<p>9The GLUE data set distribution does not include the Test\n", | |
"labels, and we only made a single GLUE evaluation server\n", | |
"submission for each of BERTBASE and BERTLARGE.\n", | |
"</p>\n", | |
"<p>10https://gluebenchmark.com/leaderboard\n", | |
"</p>\n", | |
"<p>Wikipedia containing the answer, the task is to\n", | |
"predict the answer text span in the passage.\n", | |
"</p>\n", | |
"<p>As shown in Figure 1, in the question answer-\n", | |
"ing task, we represent the input question and pas-\n", | |
"sage as a single packed sequence, with the ques-\n", | |
"tion using the A embedding and the passage using\n", | |
"the B embedding. We only introduce a start vec-\n", | |
"tor S ∈ RH and an end vector E ∈ RH during\n", | |
"fine-tuning. The probability of word i being the\n", | |
"start of the answer span is computed as a dot prod-\n", | |
"uct between Ti and S followed by a softmax over\n", | |
"all of the words in the paragraph: Pi =\n", | |
"</p>\n", | |
"<p>eS·Ti∑\n", | |
"j e\n", | |
"</p>\n", | |
"<p>S·Tj .\n", | |
"</p>\n", | |
"<p>The analogous formula is used for the end of the\n", | |
"answer span. The score of a candidate span from\n", | |
"position i to position j is defined as S·Ti + E·Tj ,\n", | |
"and the maximum scoring span where j ≥ i is\n", | |
"used as a prediction. The training objective is the\n", | |
"sum of the log-likelihoods of the correct start and\n", | |
"end positions. We fine-tune for 3 epochs with a\n", | |
"learning rate of 5e-5 and a batch size of 32.\n", | |
"</p>\n", | |
"<p>Table 2 shows top leaderboard entries as well\n", | |
"as results from top published systems (Seo et al.,\n", | |
"2017; Clark and Gardner, 2018; Peters et al.,\n", | |
"2018a; Hu et al., 2018). The top results from the\n", | |
"SQuAD leaderboard do not have up-to-date public\n", | |
"system descriptions available,11 and are allowed to\n", | |
"use any public data when training their systems.\n", | |
"We therefore use modest data augmentation in\n", | |
"our system by first fine-tuning on TriviaQA (Joshi\n", | |
"et al., 2017) befor fine-tuning on SQuAD.\n", | |
"</p>\n", | |
"<p>Our best performing system outperforms the top\n", | |
"leaderboard system by +1.5 F1 in ensembling and\n", | |
"+1.3 F1 as a single system. In fact, our single\n", | |
"BERT model outperforms the top ensemble sys-\n", | |
"tem in terms of F1 score. Without TriviaQA fine-\n", | |
"</p>\n", | |
"<p>11QANet is described in Yu et al. (2018), but the system\n", | |
"has improved substantially after publication.</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://gluebenchmark.com/leaderboard\">https://gluebenchmark.com/leaderboard</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>System Dev Test\n", | |
"EM F1 EM F1\n", | |
"</p>\n", | |
"<p>Top Leaderboard Systems (Dec 10th, 2018)\n", | |
"Human - - 82.3 91.2\n", | |
"#1 Ensemble - nlnet - - 86.0 91.7\n", | |
"#2 Ensemble - QANet - - 84.5 90.5\n", | |
"</p>\n", | |
"<p>Published\n", | |
"BiDAF+ELMo (Single) - 85.6 - 85.8\n", | |
"R.M. Reader (Ensemble) 81.2 87.9 82.3 88.5\n", | |
"</p>\n", | |
"<p>Ours\n", | |
"BERTBASE (Single) 80.8 88.5 - -\n", | |
"BERTLARGE (Single) 84.1 90.9 - -\n", | |
"BERTLARGE (Ensemble) 85.8 91.8 - -\n", | |
"BERTLARGE (Sgl.+TriviaQA) 84.2 91.1 85.1 91.8\n", | |
"BERTLARGE (Ens.+TriviaQA) 86.2 92.2 87.4 93.2\n", | |
"</p>\n", | |
"<p>Table 2: SQuAD 1.1 results. The BERT ensemble\n", | |
"is 7x systems which use different pre-training check-\n", | |
"points and fine-tuning seeds.\n", | |
"</p>\n", | |
"<p>System Dev Test\n", | |
"EM F1 EM F1\n", | |
"</p>\n", | |
"<p>Top Leaderboard Systems (Dec 10th, 2018)\n", | |
"Human 86.3 89.0 86.9 89.5\n", | |
"#1 Single - MIR-MRC (F-Net) - - 74.8 78.0\n", | |
"#2 Single - nlnet - - 74.2 77.1\n", | |
"</p>\n", | |
"<p>Published\n", | |
"unet (Ensemble) - - 71.4 74.9\n", | |
"SLQA+ (Single) - 71.4 74.4\n", | |
"</p>\n", | |
"<p>Ours\n", | |
"BERTLARGE (Single) 78.7 81.9 80.0 83.1\n", | |
"</p>\n", | |
"<p>Table 3: SQuAD 2.0 results. We exclude entries that\n", | |
"use BERT as one of their components.\n", | |
"</p>\n", | |
"<p>tuning data, we only lose 0.1-0.4 F1, still outper-\n", | |
"forming all existing systems by a wide margin.12\n", | |
"</p>\n", | |
"<p>4.3 SQuAD v2.0\n", | |
"</p>\n", | |
"<p>The SQuAD 2.0 task extends the SQuAD 1.1\n", | |
"problem definition by allowing for the possibility\n", | |
"that no short answer exists in the provided para-\n", | |
"graph, making the problem more realistic.\n", | |
"</p>\n", | |
"<p>We use a simple approach to extend the SQuAD\n", | |
"v1.1 BERT model for this task. We treat ques-\n", | |
"tions that do not have an answer as having an an-\n", | |
"swer span with start and end at the [CLS] to-\n", | |
"ken. The probability space for the start and end\n", | |
"answer span positions is extended to include the\n", | |
"position of the [CLS] token. For prediction, we\n", | |
"compare the score of the no-answer span: snull =\n", | |
"S·C + E·C to the score of the best non-null span\n", | |
"</p>\n", | |
"<p>12The TriviaQA data we used consists of paragraphs from\n", | |
"TriviaQA-Wiki formed of the first 400 tokens in documents,\n", | |
"that contain at least one of the provided possible answers.\n", | |
"</p>\n", | |
"<p>System Dev Test\n", | |
"</p>\n", | |
"<p>ESIM+GloVe 51.9 52.7\n", | |
"ESIM+ELMo 59.1 59.2\n", | |
"OpenAI GPT - 78.0\n", | |
"</p>\n", | |
"<p>BERTBASE 81.6 -\n", | |
"BERTLARGE 86.6 86.3\n", | |
"</p>\n", | |
"<p>Human (expert)† - 85.0\n", | |
"Human (5 annotations)† - 88.0\n", | |
"</p>\n", | |
"<p>Table 4: SWAG Dev and Test accuracies. †Human per-\n", | |
"formance is measured with 100 samples, as reported in\n", | |
"the SWAG paper.\n", | |
"</p>\n", | |
"<p>ˆsi,j = maxj≥iS·Ti + E·Tj . We predict a non-null\n", | |
"answer when ˆsi,j > snull + τ , where the thresh-\n", | |
"old τ is selected on the dev set to maximize F1.\n", | |
"We did not use TriviaQA data for this model. We\n", | |
"fine-tuned for 2 epochs with a learning rate of 5e-5\n", | |
"and a batch size of 48.\n", | |
"</p>\n", | |
"<p>The results compared to prior leaderboard en-\n", | |
"tries and top published work (Sun et al., 2018;\n", | |
"Wang et al., 2018b) are shown in Table 3, exclud-\n", | |
"ing systems that use BERT as one of their com-\n", | |
"ponents. We observe a +5.1 F1 improvement over\n", | |
"the previous best system.\n", | |
"</p>\n", | |
"<p>4.4 SWAG\n", | |
"</p>\n", | |
"<p>The Situations With Adversarial Generations\n", | |
"(SWAG) dataset contains 113k sentence-pair com-\n", | |
"pletion examples that evaluate grounded common-\n", | |
"sense inference (Zellers et al., 2018). Given a sen-\n", | |
"tence, the task is to choose the most plausible con-\n", | |
"tinuation among four choices.\n", | |
"</p>\n", | |
"<p>When fine-tuning on the SWAG dataset, we\n", | |
"construct four input sequences, each containing\n", | |
"the concatenation of the given sentence (sentence\n", | |
"A) and a possible continuation (sentence B). The\n", | |
"only task-specific parameters introduced is a vec-\n", | |
"tor whose dot product with the [CLS] token rep-\n", | |
"resentation C denotes a score for each choice\n", | |
"which is normalized with a softmax layer.\n", | |
"</p>\n", | |
"<p>We fine-tune the model for 3 epochs with a\n", | |
"learning rate of 2e-5 and a batch size of 16. Re-\n", | |
"sults are presented in Table 4. BERTLARGE out-\n", | |
"performs the authors’ baseline ESIM+ELMo sys-\n", | |
"tem by +27.1% and OpenAI GPT by 8.3%.\n", | |
"</p>\n", | |
"<p>5 Ablation Studies\n", | |
"</p>\n", | |
"<p>In this section, we perform ablation experiments\n", | |
"over a number of facets of BERT in order to better\n", | |
"understand their relative importance. Additional</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>Dev Set\n", | |
"Tasks MNLI-m QNLI MRPC SST-2 SQuAD\n", | |
"</p>\n", | |
"<p>(Acc) (Acc) (Acc) (Acc) (F1)\n", | |
"</p>\n", | |
"<p>BERTBASE 84.4 88.4 86.7 92.7 88.5\n", | |
"No NSP 83.9 84.9 86.5 92.6 87.9\n", | |
"LTR & No NSP 82.1 84.3 77.5 92.1 77.8\n", | |
"</p>\n", | |
"<p>+ BiLSTM 82.1 84.1 75.7 91.6 84.9\n", | |
"</p>\n", | |
"<p>Table 5: Ablation over the pre-training tasks using the\n", | |
"BERTBASE architecture. “No NSP” is trained without\n", | |
"the next sentence prediction task. “LTR & No NSP” is\n", | |
"trained as a left-to-right LM without the next sentence\n", | |
"prediction, like OpenAI GPT. “+ BiLSTM” adds a ran-\n", | |
"domly initialized BiLSTM on top of the “LTR + No\n", | |
"NSP” model during fine-tuning.\n", | |
"</p>\n", | |
"<p>ablation studies can be found in Appendix C.\n", | |
"</p>\n", | |
"<p>5.1 Effect of Pre-training Tasks\n", | |
"</p>\n", | |
"<p>We demonstrate the importance of the deep bidi-\n", | |
"rectionality of BERT by evaluating two pre-\n", | |
"training objectives using exactly the same pre-\n", | |
"training data, fine-tuning scheme, and hyperpa-\n", | |
"rameters as BERTBASE:\n", | |
"</p>\n", | |
"<p>No NSP: A bidirectional model which is trained\n", | |
"using the “masked LM” (MLM) but without the\n", | |
"“next sentence prediction” (NSP) task.\n", | |
"LTR & No NSP: A left-context-only model which\n", | |
"is trained using a standard Left-to-Right (LTR)\n", | |
"LM, rather than an MLM. The left-only constraint\n", | |
"was also applied at fine-tuning, because removing\n", | |
"it introduced a pre-train/fine-tune mismatch that\n", | |
"degraded downstream performance. Additionally,\n", | |
"this model was pre-trained without the NSP task.\n", | |
"This is directly comparable to OpenAI GPT, but\n", | |
"using our larger training dataset, our input repre-\n", | |
"sentation, and our fine-tuning scheme.\n", | |
"</p>\n", | |
"<p>We first examine the impact brought by the NSP\n", | |
"task. In Table 5, we show that removing NSP\n", | |
"hurts performance significantly on QNLI, MNLI,\n", | |
"and SQuAD 1.1. Next, we evaluate the impact\n", | |
"of training bidirectional representations by com-\n", | |
"paring “No NSP” to “LTR & No NSP”. The LTR\n", | |
"model performs worse than the MLM model on all\n", | |
"tasks, with large drops on MRPC and SQuAD.\n", | |
"</p>\n", | |
"<p>For SQuAD it is intuitively clear that a LTR\n", | |
"model will perform poorly at token predictions,\n", | |
"since the token-level hidden states have no right-\n", | |
"side context. In order to make a good faith at-\n", | |
"tempt at strengthening the LTR system, we added\n", | |
"a randomly initialized BiLSTM on top. This does\n", | |
"significantly improve results on SQuAD, but the\n", | |
"</p>\n", | |
"<p>results are still far worse than those of the pre-\n", | |
"trained bidirectional models. The BiLSTM hurts\n", | |
"performance on the GLUE tasks.\n", | |
"</p>\n", | |
"<p>We recognize that it would also be possible to\n", | |
"train separate LTR and RTL models and represent\n", | |
"each token as the concatenation of the two mod-\n", | |
"els, as ELMo does. However: (a) this is twice as\n", | |
"expensive as a single bidirectional model; (b) this\n", | |
"is non-intuitive for tasks like QA, since the RTL\n", | |
"model would not be able to condition the answer\n", | |
"on the question; (c) this it is strictly less powerful\n", | |
"than a deep bidirectional model, since it can use\n", | |
"both left and right context at every layer.\n", | |
"</p>\n", | |
"<p>5.2 Effect of Model Size\n", | |
"</p>\n", | |
"<p>In this section, we explore the effect of model size\n", | |
"on fine-tuning task accuracy. We trained a number\n", | |
"of BERT models with a differing number of layers,\n", | |
"hidden units, and attention heads, while otherwise\n", | |
"using the same hyperparameters and training pro-\n", | |
"cedure as described previously.\n", | |
"</p>\n", | |
"<p>Results on selected GLUE tasks are shown in\n", | |
"Table 6. In this table, we report the average Dev\n", | |
"Set accuracy from 5 random restarts of fine-tuning.\n", | |
"We can see that larger models lead to a strict ac-\n", | |
"curacy improvement across all four datasets, even\n", | |
"for MRPC which only has 3,600 labeled train-\n", | |
"ing examples, and is substantially different from\n", | |
"the pre-training tasks. It is also perhaps surpris-\n", | |
"ing that we are able to achieve such significant\n", | |
"improvements on top of models which are al-\n", | |
"ready quite large relative to the existing literature.\n", | |
"For example, the largest Transformer explored in\n", | |
"Vaswani et al. (2017) is (L=6, H=1024, A=16)\n", | |
"with 100M parameters for the encoder, and the\n", | |
"largest Transformer we have found in the literature\n", | |
"is (L=64, H=512, A=2) with 235M parameters\n", | |
"(Al-Rfou et al., 2018). By contrast, BERTBASE\n", | |
"contains 110M parameters and BERTLARGE con-\n", | |
"tains 340M parameters.\n", | |
"</p>\n", | |
"<p>It has long been known that increasing the\n", | |
"model size will lead to continual improvements\n", | |
"on large-scale tasks such as machine translation\n", | |
"and language modeling, which is demonstrated\n", | |
"by the LM perplexity of held-out training data\n", | |
"shown in Table 6. However, we believe that\n", | |
"this is the first work to demonstrate convinc-\n", | |
"ingly that scaling to extreme model sizes also\n", | |
"leads to large improvements on very small scale\n", | |
"tasks, provided that the model has been suffi-\n", | |
"ciently pre-trained. Peters et al. (2018b) presented</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>mixed results on the downstream task impact of\n", | |
"increasing the pre-trained bi-LM size from two\n", | |
"to four layers and Melamud et al. (2016) men-\n", | |
"tioned in passing that increasing hidden dimen-\n", | |
"sion size from 200 to 600 helped, but increasing\n", | |
"further to 1,000 did not bring further improve-\n", | |
"ments. Both of these prior works used a feature-\n", | |
"based approach — we hypothesize that when the\n", | |
"model is fine-tuned directly on the downstream\n", | |
"tasks and uses only a very small number of ran-\n", | |
"domly initialized additional parameters, the task-\n", | |
"specific models can benefit from the larger, more\n", | |
"expressive pre-trained representations even when\n", | |
"downstream task data is very small.\n", | |
"</p>\n", | |
"<p>5.3 Feature-based Approach with BERT\n", | |
"</p>\n", | |
"<p>All of the BERT results presented so far have used\n", | |
"the fine-tuning approach, where a simple classifi-\n", | |
"cation layer is added to the pre-trained model, and\n", | |
"all parameters are jointly fine-tuned on a down-\n", | |
"stream task. However, the feature-based approach,\n", | |
"where fixed features are extracted from the pre-\n", | |
"trained model, has certain advantages. First, not\n", | |
"all tasks can be easily represented by a Trans-\n", | |
"former encoder architecture, and therefore require\n", | |
"a task-specific model architecture to be added.\n", | |
"Second, there are major computational benefits\n", | |
"to pre-compute an expensive representation of the\n", | |
"training data once and then run many experiments\n", | |
"with cheaper models on top of this representation.\n", | |
"</p>\n", | |
"<p>In this section, we compare the two approaches\n", | |
"by applying BERT to the CoNLL-2003 Named\n", | |
"Entity Recognition (NER) task (Tjong Kim Sang\n", | |
"and De Meulder, 2003). In the input to BERT, we\n", | |
"use a case-preserving WordPiece model, and we\n", | |
"include the maximal document context provided\n", | |
"by the data. Following standard practice, we for-\n", | |
"mulate this as a tagging task but do not use a CRF\n", | |
"</p>\n", | |
"<p>Hyperparams Dev Set Accuracy\n", | |
"</p>\n", | |
"<p>#L #H #A LM (ppl) MNLI-m MRPC SST-2\n", | |
"</p>\n", | |
"<p>3 768 12 5.84 77.9 79.8 88.4\n", | |
"6 768 3 5.24 80.6 82.2 90.7\n", | |
"6 768 12 4.68 81.9 84.8 91.3\n", | |
"</p>\n", | |
"<p>12 768 12 3.99 84.4 86.7 92.9\n", | |
"12 1024 16 3.54 85.7 86.9 93.3\n", | |
"24 1024 16 3.23 86.6 87.8 93.7\n", | |
"</p>\n", | |
"<p>Table 6: Ablation over BERT model size. #L = the\n", | |
"number of layers; #H = hidden size; #A = number of at-\n", | |
"tention heads. “LM (ppl)” is the masked LM perplexity\n", | |
"of held-out training data.\n", | |
"</p>\n", | |
"<p>System Dev F1 Test F1\n", | |
"</p>\n", | |
"<p>ELMo (Peters et al., 2018a) 95.7 92.2\n", | |
"CVT (Clark et al., 2018) - 92.6\n", | |
"CSE (Akbik et al., 2018) - 93.1\n", | |
"</p>\n", | |
"<p>Fine-tuning approach\n", | |
"BERTLARGE 96.6 92.8\n", | |
"BERTBASE 96.4 92.4\n", | |
"</p>\n", | |
"<p>Feature-based approach (BERTBASE)\n", | |
"Embeddings 91.0 -\n", | |
"Second-to-Last Hidden 95.6 -\n", | |
"Last Hidden 94.9 -\n", | |
"Weighted Sum Last Four Hidden 95.9 -\n", | |
"Concat Last Four Hidden 96.1 -\n", | |
"Weighted Sum All 12 Layers 95.5 -\n", | |
"</p>\n", | |
"<p>Table 7: CoNLL-2003 Named Entity Recognition re-\n", | |
"sults. Hyperparameters were selected using the Dev\n", | |
"set. The reported Dev and Test scores are averaged over\n", | |
"5 random restarts using those hyperparameters.\n", | |
"</p>\n", | |
"<p>layer in the output. We use the representation of\n", | |
"the first sub-token as the input to the token-level\n", | |
"classifier over the NER label set.\n", | |
"</p>\n", | |
"<p>To ablate the fine-tuning approach, we apply the\n", | |
"feature-based approach by extracting the activa-\n", | |
"tions from one or more layers without fine-tuning\n", | |
"any parameters of BERT. These contextual em-\n", | |
"beddings are used as input to a randomly initial-\n", | |
"ized two-layer 768-dimensional BiLSTM before\n", | |
"the classification layer.\n", | |
"</p>\n", | |
"<p>Results are presented in Table 7. BERTLARGE\n", | |
"performs competitively with state-of-the-art meth-\n", | |
"ods. The best performing method concatenates the\n", | |
"token representations from the top four hidden lay-\n", | |
"ers of the pre-trained Transformer, which is only\n", | |
"0.3 F1 behind fine-tuning the entire model. This\n", | |
"demonstrates that BERT is effective for both fine-\n", | |
"tuning and feature-based approaches.\n", | |
"</p>\n", | |
"<p>6 Conclusion\n", | |
"</p>\n", | |
"<p>Recent empirical improvements due to transfer\n", | |
"learning with language models have demonstrated\n", | |
"that rich, unsupervised pre-training is an integral\n", | |
"part of many language understanding systems. In\n", | |
"particular, these results enable even low-resource\n", | |
"tasks to benefit from deep unidirectional architec-\n", | |
"tures. Our major contribution is further general-\n", | |
"izing these findings to deep bidirectional architec-\n", | |
"tures, allowing the same pre-trained model to suc-\n", | |
"cessfully tackle a broad set of NLP tasks.</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>References\n", | |
"</p>\n", | |
"<p>Alan Akbik, Duncan Blythe, and Roland Vollgraf.\n", | |
"2018. Contextual string embeddings for sequence\n", | |
"labeling. In Proceedings of the 27th International\n", | |
"Conference on Computational Linguistics, pages\n", | |
"1638–1649.\n", | |
"</p>\n", | |
"<p>Rami Al-Rfou, Dokook Choe, Noah Constant, Mandy\n", | |
"Guo, and Llion Jones. 2018. Character-level lan-\n", | |
"guage modeling with deeper self-attention. arXiv\n", | |
"preprint arXiv:1808.04444.\n", | |
"</p>\n", | |
"<p>Rie Kubota Ando and Tong Zhang. 2005. A framework\n", | |
"for learning predictive structures from multiple tasks\n", | |
"and unlabeled data. Journal of Machine Learning\n", | |
"Research, 6(Nov):1817–1853.\n", | |
"</p>\n", | |
"<p>Luisa Bentivogli, Bernardo Magnini, Ido Dagan,\n", | |
"Hoa Trang Dang, and Danilo Giampiccolo. 2009.\n", | |
"The fifth PASCAL recognizing textual entailment\n", | |
"challenge. In TAC. NIST.\n", | |
"</p>\n", | |
"<p>John Blitzer, Ryan McDonald, and Fernando Pereira.\n", | |
"2006. Domain adaptation with structural correspon-\n", | |
"dence learning. In Proceedings of the 2006 confer-\n", | |
"ence on empirical methods in natural language pro-\n", | |
"cessing, pages 120–128. Association for Computa-\n", | |
"tional Linguistics.\n", | |
"</p>\n", | |
"<p>Samuel R. Bowman, Gabor Angeli, Christopher Potts,\n", | |
"and Christopher D. Manning. 2015. A large anno-\n", | |
"tated corpus for learning natural language inference.\n", | |
"In EMNLP. Association for Computational Linguis-\n", | |
"tics.\n", | |
"</p>\n", | |
"<p>Peter F Brown, Peter V Desouza, Robert L Mercer,\n", | |
"Vincent J Della Pietra, and Jenifer C Lai. 1992.\n", | |
"Class-based n-gram models of natural language.\n", | |
"Computational linguistics, 18(4):467–479.\n", | |
"</p>\n", | |
"<p>Daniel Cer, Mona Diab, Eneko Agirre, Inigo Lopez-\n", | |
"Gazpio, and Lucia Specia. 2017. Semeval-2017\n", | |
"task 1: Semantic textual similarity multilingual and\n", | |
"crosslingual focused evaluation. In Proceedings\n", | |
"of the 11th International Workshop on Semantic\n", | |
"Evaluation (SemEval-2017), pages 1–14, Vancou-\n", | |
"ver, Canada. Association for Computational Lin-\n", | |
"guistics.\n", | |
"</p>\n", | |
"<p>Ciprian Chelba, Tomas Mikolov, Mike Schuster, Qi Ge,\n", | |
"Thorsten Brants, Phillipp Koehn, and Tony Robin-\n", | |
"son. 2013. One billion word benchmark for measur-\n", | |
"ing progress in statistical language modeling. arXiv\n", | |
"preprint arXiv:1312.3005.\n", | |
"</p>\n", | |
"<p>Z. Chen, H. Zhang, X. Zhang, and L. Zhao. 2018.\n", | |
"Quora question pairs.\n", | |
"</p>\n", | |
"<p>Christopher Clark and Matt Gardner. 2018. Simple\n", | |
"and effective multi-paragraph reading comprehen-\n", | |
"sion. In ACL.\n", | |
"</p>\n", | |
"<p>Kevin Clark, Minh-Thang Luong, Christopher D Man-\n", | |
"ning, and Quoc Le. 2018. Semi-supervised se-\n", | |
"quence modeling with cross-view training. In Pro-\n", | |
"ceedings of the 2018 Conference on Empirical Meth-\n", | |
"ods in Natural Language Processing, pages 1914–\n", | |
"1925.\n", | |
"</p>\n", | |
"<p>Ronan Collobert and Jason Weston. 2008. A unified\n", | |
"architecture for natural language processing: Deep\n", | |
"neural networks with multitask learning. In Pro-\n", | |
"ceedings of the 25th international conference on\n", | |
"Machine learning, pages 160–167. ACM.\n", | |
"</p>\n", | |
"<p>Alexis Conneau, Douwe Kiela, Holger Schwenk, Loı̈c\n", | |
"Barrault, and Antoine Bordes. 2017. Supervised\n", | |
"learning of universal sentence representations from\n", | |
"natural language inference data. In Proceedings of\n", | |
"the 2017 Conference on Empirical Methods in Nat-\n", | |
"ural Language Processing, pages 670–680, Copen-\n", | |
"hagen, Denmark. Association for Computational\n", | |
"Linguistics.\n", | |
"</p>\n", | |
"<p>Andrew M Dai and Quoc V Le. 2015. Semi-supervised\n", | |
"sequence learning. In Advances in neural informa-\n", | |
"tion processing systems, pages 3079–3087.\n", | |
"</p>\n", | |
"<p>J. Deng, W. Dong, R. Socher, L.-J. Li, K. Li, and L. Fei-\n", | |
"Fei. 2009. ImageNet: A Large-Scale Hierarchical\n", | |
"Image Database. In CVPR09.\n", | |
"</p>\n", | |
"<p>William B Dolan and Chris Brockett. 2005. Automati-\n", | |
"cally constructing a corpus of sentential paraphrases.\n", | |
"In Proceedings of the Third International Workshop\n", | |
"on Paraphrasing (IWP2005).\n", | |
"</p>\n", | |
"<p>William Fedus, Ian Goodfellow, and Andrew M Dai.\n", | |
"2018. Maskgan: Better text generation via filling in\n", | |
"the . arXiv preprint arXiv:1801.07736.\n", | |
"</p>\n", | |
"<p>Dan Hendrycks and Kevin Gimpel. 2016. Bridging\n", | |
"nonlinearities and stochastic regularizers with gaus-\n", | |
"sian error linear units. CoRR, abs/1606.08415.\n", | |
"</p>\n", | |
"<p>Felix Hill, Kyunghyun Cho, and Anna Korhonen. 2016.\n", | |
"Learning distributed representations of sentences\n", | |
"from unlabelled data. In Proceedings of the 2016\n", | |
"Conference of the North American Chapter of the\n", | |
"Association for Computational Linguistics: Human\n", | |
"Language Technologies. Association for Computa-\n", | |
"tional Linguistics.\n", | |
"</p>\n", | |
"<p>Jeremy Howard and Sebastian Ruder. 2018. Universal\n", | |
"language model fine-tuning for text classification. In\n", | |
"ACL. Association for Computational Linguistics.\n", | |
"</p>\n", | |
"<p>Minghao Hu, Yuxing Peng, Zhen Huang, Xipeng Qiu,\n", | |
"Furu Wei, and Ming Zhou. 2018. Reinforced\n", | |
"mnemonic reader for machine reading comprehen-\n", | |
"sion. In IJCAI.\n", | |
"</p>\n", | |
"<p>Yacine Jernite, Samuel R. Bowman, and David Son-\n", | |
"tag. 2017. Discourse-based objectives for fast un-\n", | |
"supervised sentence representation learning. CoRR,\n", | |
"abs/1705.00557.</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://doi.org/10.18653/v1/S17-2001\">https://doi.org/10.18653/v1/S17-2001</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://doi.org/10.18653/v1/S17-2001\">https://doi.org/10.18653/v1/S17-2001</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://doi.org/10.18653/v1/S17-2001\">https://doi.org/10.18653/v1/S17-2001</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://data.quora.com/First-Quora-Dataset-Release-Question-Pairs\">https://data.quora.com/First-Quora-Dataset-Release-Question-Pairs</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://www.aclweb.org/anthology/D17-1070\">https://www.aclweb.org/anthology/D17-1070</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://www.aclweb.org/anthology/D17-1070\">https://www.aclweb.org/anthology/D17-1070</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://www.aclweb.org/anthology/D17-1070\">https://www.aclweb.org/anthology/D17-1070</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1606.08415\">http://arxiv.org/abs/1606.08415</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1606.08415\">http://arxiv.org/abs/1606.08415</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1606.08415\">http://arxiv.org/abs/1606.08415</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1801.06146\">http://arxiv.org/abs/1801.06146</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1801.06146\">http://arxiv.org/abs/1801.06146</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1705.00557\">http://arxiv.org/abs/1705.00557</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://arxiv.org/abs/1705.00557\">http://arxiv.org/abs/1705.00557</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>Mandar Joshi, Eunsol Choi, Daniel S Weld, and Luke\n", | |
"Zettlemoyer. 2017. Triviaqa: A large scale distantly\n", | |
"supervised challenge dataset for reading comprehen-\n", | |
"sion. In ACL.\n", | |
"</p>\n", | |
"<p>Ryan Kiros, Yukun Zhu, Ruslan R Salakhutdinov,\n", | |
"Richard Zemel, Raquel Urtasun, Antonio Torralba,\n", | |
"and Sanja Fidler. 2015. Skip-thought vectors. In\n", | |
"Advances in neural information processing systems,\n", | |
"pages 3294–3302.\n", | |
"</p>\n", | |
"<p>Quoc Le and Tomas Mikolov. 2014. Distributed rep-\n", | |
"resentations of sentences and documents. In Inter-\n", | |
"national Conference on Machine Learning, pages\n", | |
"1188–1196.\n", | |
"</p>\n", | |
"<p>Hector J Levesque, Ernest Davis, and Leora Morgen-\n", | |
"stern. 2011. The winograd schema challenge. In\n", | |
"Aaai spring symposium: Logical formalizations of\n", | |
"commonsense reasoning, volume 46, page 47.\n", | |
"</p>\n", | |
"<p>Lajanugen Logeswaran and Honglak Lee. 2018. An\n", | |
"efficient framework for learning sentence represen-\n", | |
"tations. In International Conference on Learning\n", | |
"Representations.\n", | |
"</p>\n", | |
"<p>Bryan McCann, James Bradbury, Caiming Xiong, and\n", | |
"Richard Socher. 2017. Learned in translation: Con-\n", | |
"textualized word vectors. In NIPS.\n", | |
"</p>\n", | |
"<p>Oren Melamud, Jacob Goldberger, and Ido Dagan.\n", | |
"2016. context2vec: Learning generic context em-\n", | |
"bedding with bidirectional LSTM. In CoNLL.\n", | |
"</p>\n", | |
"<p>Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg S Cor-\n", | |
"rado, and Jeff Dean. 2013. Distributed representa-\n", | |
"tions of words and phrases and their compositional-\n", | |
"ity. In Advances in Neural Information Processing\n", | |
"Systems 26, pages 3111–3119. Curran Associates,\n", | |
"Inc.\n", | |
"</p>\n", | |
"<p>Andriy Mnih and Geoffrey E Hinton. 2009. A scal-\n", | |
"able hierarchical distributed language model. In\n", | |
"D. Koller, D. Schuurmans, Y. Bengio, and L. Bot-\n", | |
"tou, editors, Advances in Neural Information Pro-\n", | |
"cessing Systems 21, pages 1081–1088. Curran As-\n", | |
"sociates, Inc.\n", | |
"</p>\n", | |
"<p>Ankur P Parikh, Oscar Täckström, Dipanjan Das, and\n", | |
"Jakob Uszkoreit. 2016. A decomposable attention\n", | |
"model for natural language inference. In EMNLP.\n", | |
"</p>\n", | |
"<p>Jeffrey Pennington, Richard Socher, and Christo-\n", | |
"pher D. Manning. 2014. Glove: Global vectors for\n", | |
"word representation. In Empirical Methods in Nat-\n", | |
"ural Language Processing (EMNLP), pages 1532–\n", | |
"1543.\n", | |
"</p>\n", | |
"<p>Matthew Peters, Waleed Ammar, Chandra Bhagavat-\n", | |
"ula, and Russell Power. 2017. Semi-supervised se-\n", | |
"quence tagging with bidirectional language models.\n", | |
"In ACL.\n", | |
"</p>\n", | |
"<p>Matthew Peters, Mark Neumann, Mohit Iyyer, Matt\n", | |
"Gardner, Christopher Clark, Kenton Lee, and Luke\n", | |
"Zettlemoyer. 2018a. Deep contextualized word rep-\n", | |
"resentations. In NAACL.\n", | |
"</p>\n", | |
"<p>Matthew Peters, Mark Neumann, Luke Zettlemoyer,\n", | |
"and Wen-tau Yih. 2018b. Dissecting contextual\n", | |
"word embeddings: Architecture and representation.\n", | |
"In Proceedings of the 2018 Conference on Empiri-\n", | |
"cal Methods in Natural Language Processing, pages\n", | |
"1499–1509.\n", | |
"</p>\n", | |
"<p>Alec Radford, Karthik Narasimhan, Tim Salimans, and\n", | |
"Ilya Sutskever. 2018. Improving language under-\n", | |
"standing with unsupervised learning. Technical re-\n", | |
"port, OpenAI.\n", | |
"</p>\n", | |
"<p>Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, and\n", | |
"Percy Liang. 2016. Squad: 100,000+ questions for\n", | |
"machine comprehension of text. In Proceedings of\n", | |
"the 2016 Conference on Empirical Methods in Nat-\n", | |
"ural Language Processing, pages 2383–2392.\n", | |
"</p>\n", | |
"<p>Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, and\n", | |
"Hannaneh Hajishirzi. 2017. Bidirectional attention\n", | |
"flow for machine comprehension. In ICLR.\n", | |
"</p>\n", | |
"<p>Richard Socher, Alex Perelygin, Jean Wu, Jason\n", | |
"Chuang, Christopher D Manning, Andrew Ng, and\n", | |
"Christopher Potts. 2013. Recursive deep models\n", | |
"for semantic compositionality over a sentiment tree-\n", | |
"bank. In Proceedings of the 2013 conference on\n", | |
"empirical methods in natural language processing,\n", | |
"pages 1631–1642.\n", | |
"</p>\n", | |
"<p>Fu Sun, Linyang Li, Xipeng Qiu, and Yang Liu.\n", | |
"2018. U-net: Machine reading comprehension\n", | |
"with unanswerable questions. arXiv preprint\n", | |
"arXiv:1810.06638.\n", | |
"</p>\n", | |
"<p>Wilson L Taylor. 1953. Cloze procedure: A new\n", | |
"tool for measuring readability. Journalism Bulletin,\n", | |
"30(4):415–433.\n", | |
"</p>\n", | |
"<p>Erik F Tjong Kim Sang and Fien De Meulder.\n", | |
"2003. Introduction to the conll-2003 shared task:\n", | |
"Language-independent named entity recognition. In\n", | |
"CoNLL.\n", | |
"</p>\n", | |
"<p>Joseph Turian, Lev Ratinov, and Yoshua Bengio. 2010.\n", | |
"Word representations: A simple and general method\n", | |
"for semi-supervised learning. In Proceedings of the\n", | |
"48th Annual Meeting of the Association for Compu-\n", | |
"tational Linguistics, ACL ’10, pages 384–394.\n", | |
"</p>\n", | |
"<p>Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob\n", | |
"Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz\n", | |
"Kaiser, and Illia Polosukhin. 2017. Attention is all\n", | |
"you need. In Advances in Neural Information Pro-\n", | |
"cessing Systems, pages 6000–6010.\n", | |
"</p>\n", | |
"<p>Pascal Vincent, Hugo Larochelle, Yoshua Bengio, and\n", | |
"Pierre-Antoine Manzagol. 2008. Extracting and\n", | |
"composing robust features with denoising autoen-\n", | |
"coders. In Proceedings of the 25th international\n", | |
"conference on Machine learning, pages 1096–1103.\n", | |
"ACM.\n", | |
"</p>\n", | |
"<p>Alex Wang, Amanpreet Singh, Julian Michael, Fe-\n", | |
"lix Hill, Omer Levy, and Samuel Bowman. 2018a.\n", | |
"Glue: A multi-task benchmark and analysis platform</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://openreview.net/forum?id=rJvJXZb0W\">https://openreview.net/forum?id=rJvJXZb0W</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://openreview.net/forum?id=rJvJXZb0W\">https://openreview.net/forum?id=rJvJXZb0W</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://openreview.net/forum?id=rJvJXZb0W\">https://openreview.net/forum?id=rJvJXZb0W</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://papers.nips.cc/paper/3583-a-scalable-hierarchical-distributed-language-model.pdf\">http://papers.nips.cc/paper/3583-a-scalable-hierarchical-distributed-language-model.pdf</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://papers.nips.cc/paper/3583-a-scalable-hierarchical-distributed-language-model.pdf\">http://papers.nips.cc/paper/3583-a-scalable-hierarchical-distributed-language-model.pdf</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://www.aclweb.org/anthology/D14-1162\">http://www.aclweb.org/anthology/D14-1162</a></div>\n", | |
"<div class=\"annotation\"><a href=\"http://www.aclweb.org/anthology/D14-1162\">http://www.aclweb.org/anthology/D14-1162</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>for natural language understanding. In Proceedings\n", | |
"of the 2018 EMNLP Workshop BlackboxNLP: An-\n", | |
"alyzing and Interpreting Neural Networks for NLP,\n", | |
"pages 353–355.\n", | |
"</p>\n", | |
"<p>Wei Wang, Ming Yan, and Chen Wu. 2018b. Multi-\n", | |
"granularity hierarchical attention fusion networks\n", | |
"for reading comprehension and question answering.\n", | |
"In Proceedings of the 56th Annual Meeting of the As-\n", | |
"sociation for Computational Linguistics (Volume 1:\n", | |
"Long Papers). Association for Computational Lin-\n", | |
"guistics.\n", | |
"</p>\n", | |
"<p>Alex Warstadt, Amanpreet Singh, and Samuel R Bow-\n", | |
"man. 2018. Neural network acceptability judg-\n", | |
"ments. arXiv preprint arXiv:1805.12471.\n", | |
"</p>\n", | |
"<p>Adina Williams, Nikita Nangia, and Samuel R Bow-\n", | |
"man. 2018. A broad-coverage challenge corpus\n", | |
"for sentence understanding through inference. In\n", | |
"NAACL.\n", | |
"</p>\n", | |
"<p>Yonghui Wu, Mike Schuster, Zhifeng Chen, Quoc V\n", | |
"Le, Mohammad Norouzi, Wolfgang Macherey,\n", | |
"Maxim Krikun, Yuan Cao, Qin Gao, Klaus\n", | |
"Macherey, et al. 2016. Google’s neural ma-\n", | |
"chine translation system: Bridging the gap between\n", | |
"human and machine translation. arXiv preprint\n", | |
"arXiv:1609.08144.\n", | |
"</p>\n", | |
"<p>Jason Yosinski, Jeff Clune, Yoshua Bengio, and Hod\n", | |
"Lipson. 2014. How transferable are features in deep\n", | |
"neural networks? In Advances in neural information\n", | |
"processing systems, pages 3320–3328.\n", | |
"</p>\n", | |
"<p>Adams Wei Yu, David Dohan, Minh-Thang Luong, Rui\n", | |
"Zhao, Kai Chen, Mohammad Norouzi, and Quoc V\n", | |
"Le. 2018. QANet: Combining local convolution\n", | |
"with global self-attention for reading comprehen-\n", | |
"sion. In ICLR.\n", | |
"</p>\n", | |
"<p>Rowan Zellers, Yonatan Bisk, Roy Schwartz, and Yejin\n", | |
"Choi. 2018. Swag: A large-scale adversarial dataset\n", | |
"for grounded commonsense inference. In Proceed-\n", | |
"ings of the 2018 Conference on Empirical Methods\n", | |
"in Natural Language Processing (EMNLP).\n", | |
"</p>\n", | |
"<p>Yukun Zhu, Ryan Kiros, Rich Zemel, Ruslan Salakhut-\n", | |
"dinov, Raquel Urtasun, Antonio Torralba, and Sanja\n", | |
"Fidler. 2015. Aligning books and movies: Towards\n", | |
"story-like visual explanations by watching movies\n", | |
"and reading books. In Proceedings of the IEEE\n", | |
"international conference on computer vision, pages\n", | |
"19–27.\n", | |
"</p>\n", | |
"<p>Appendix for “BERT: Pre-training of\n", | |
"Deep Bidirectional Transformers for\n", | |
"</p>\n", | |
"<p>Language Understanding”\n", | |
"</p>\n", | |
"<p>We organize the appendix into three sections:\n", | |
"</p>\n", | |
"<p>• Additional implementation details for BERT\n", | |
"are presented in Appendix A;\n", | |
"</p>\n", | |
"<p>• Additional details for our experiments are\n", | |
"presented in Appendix B; and\n", | |
"</p>\n", | |
"<p>• Additional ablation studies are presented in\n", | |
"Appendix C.\n", | |
"</p>\n", | |
"<p>We present additional ablation studies for\n", | |
"BERT including:\n", | |
"</p>\n", | |
"<p>– Effect of Number of Training Steps; and\n", | |
"– Ablation for Different Masking Proce-\n", | |
"</p>\n", | |
"<p>dures.\n", | |
"</p>\n", | |
"<p>A Additional Details for BERT\n", | |
"</p>\n", | |
"<p>A.1 Illustration of the Pre-training Tasks\n", | |
"</p>\n", | |
"<p>We provide examples of the pre-training tasks in\n", | |
"the following.\n", | |
"</p>\n", | |
"<p>Masked LM and the Masking Procedure As-\n", | |
"suming the unlabeled sentence is my dog is\n", | |
"</p>\n", | |
"<p>hairy, and during the random masking procedure\n", | |
"we chose the 4-th token (which corresponding to\n", | |
"hairy), our masking procedure can be further il-\n", | |
"lustrated by\n", | |
"</p>\n", | |
"<p>• 80% of the time: Replace the word with the\n", | |
"[MASK] token, e.g., my dog is hairy →\n", | |
"my dog is [MASK]\n", | |
"</p>\n", | |
"<p>• 10% of the time: Replace the word with a\n", | |
"random word, e.g., my dog is hairy → my\n", | |
"</p>\n", | |
"<p>dog is apple\n", | |
"</p>\n", | |
"<p>• 10% of the time: Keep the word un-\n", | |
"changed, e.g., my dog is hairy → my dog\n", | |
"</p>\n", | |
"<p>is hairy. The purpose of this is to bias the\n", | |
"representation towards the actual observed\n", | |
"word.\n", | |
"</p>\n", | |
"<p>The advantage of this procedure is that the\n", | |
"Transformer encoder does not know which words\n", | |
"it will be asked to predict or which have been re-\n", | |
"placed by random words, so it is forced to keep\n", | |
"a distributional contextual representation of ev-\n", | |
"ery input token. Additionally, because random\n", | |
"replacement only occurs for 1.5% of all tokens\n", | |
"(i.e., 10% of 15%), this does not seem to harm\n", | |
"the model’s language understanding capability. In\n", | |
"Section C.2, we evaluate the impact this proce-\n", | |
"dure.\n", | |
"</p>\n", | |
"<p>Compared to standard langauge model training,\n", | |
"the masked LM only make predictions on 15% of\n", | |
"tokens in each batch, which suggests that more\n", | |
"pre-training steps may be required for the model</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>BERT (Ours)\n", | |
"</p>\n", | |
"<p>Trm Trm Trm\n", | |
"</p>\n", | |
"<p>Trm Trm Trm\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>Trm Trm Trm\n", | |
"</p>\n", | |
"<p>Trm Trm Trm\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>OpenAI GPT\n", | |
"</p>\n", | |
"<p>Lstm\n", | |
"</p>\n", | |
"<p>ELMo\n", | |
"</p>\n", | |
"<p>Lstm Lstm\n", | |
"</p>\n", | |
"<p>Lstm Lstm Lstm\n", | |
"</p>\n", | |
"<p>Lstm Lstm Lstm\n", | |
"</p>\n", | |
"<p>Lstm Lstm Lstm\n", | |
"</p>\n", | |
"<p> T1 T2 TN...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p> E1 E2 EN...\n", | |
"</p>\n", | |
"<p> T1 T2 TN...\n", | |
"</p>\n", | |
"<p> E1 E2 EN...\n", | |
"</p>\n", | |
"<p> T1 T2 TN...\n", | |
"</p>\n", | |
"<p> E1 E2 EN...\n", | |
"</p>\n", | |
"<p>Figure 3: Differences in pre-training model architectures. BERT uses a bidirectional Transformer. OpenAI GPT\n", | |
"uses a left-to-right Transformer. ELMo uses the concatenation of independently trained left-to-right and right-to-\n", | |
"left LSTMs to generate features for downstream tasks. Among the three, only BERT representations are jointly\n", | |
"conditioned on both left and right context in all layers. In addition to the architecture differences, BERT and\n", | |
"OpenAI GPT are fine-tuning approaches, while ELMo is a feature-based approach.\n", | |
"</p>\n", | |
"<p>to converge. In Section C.1 we demonstrate that\n", | |
"MLM does converge marginally slower than a left-\n", | |
"to-right model (which predicts every token), but\n", | |
"the empirical improvements of the MLM model\n", | |
"far outweigh the increased training cost.\n", | |
"</p>\n", | |
"<p>Next Sentence Prediction The next sentence\n", | |
"prediction task can be illustrated in the following\n", | |
"examples.\n", | |
"</p>\n", | |
"<p>Input = [CLS] the man went to [MASK] store [SEP]\n", | |
"</p>\n", | |
"<p>he bought a gallon [MASK] milk [SEP]\n", | |
"</p>\n", | |
"<p>Label = IsNext\n", | |
"</p>\n", | |
"<p>Input = [CLS] the man [MASK] to the store [SEP]\n", | |
"</p>\n", | |
"<p>penguin [MASK] are flight ##less birds [SEP]\n", | |
"</p>\n", | |
"<p>Label = NotNext\n", | |
"</p>\n", | |
"<p>A.2 Pre-training Procedure\n", | |
"</p>\n", | |
"<p>To generate each training input sequence, we sam-\n", | |
"ple two spans of text from the corpus, which we\n", | |
"refer to as “sentences” even though they are typ-\n", | |
"ically much longer than single sentences (but can\n", | |
"be shorter also). The first sentence receives the A\n", | |
"embedding and the second receives the B embed-\n", | |
"ding. 50% of the time B is the actual next sentence\n", | |
"that follows A and 50% of the time it is a random\n", | |
"sentence, which is done for the “next sentence pre-\n", | |
"diction” task. They are sampled such that the com-\n", | |
"bined length is ≤ 512 tokens. The LM masking is\n", | |
"applied after WordPiece tokenization with a uni-\n", | |
"form masking rate of 15%, and no special consid-\n", | |
"eration given to partial word pieces.\n", | |
"</p>\n", | |
"<p>We train with batch size of 256 sequences (256\n", | |
"sequences * 512 tokens = 128,000 tokens/batch)\n", | |
"for 1,000,000 steps, which is approximately 40\n", | |
"</p>\n", | |
"<p>epochs over the 3.3 billion word corpus. We\n", | |
"use Adam with learning rate of 1e-4, β1 = 0.9,\n", | |
"β2 = 0.999, L2 weight decay of 0.01, learning\n", | |
"rate warmup over the first 10,000 steps, and linear\n", | |
"decay of the learning rate. We use a dropout prob-\n", | |
"ability of 0.1 on all layers. We use a gelu acti-\n", | |
"vation (Hendrycks and Gimpel, 2016) rather than\n", | |
"the standard relu, following OpenAI GPT. The\n", | |
"training loss is the sum of the mean masked LM\n", | |
"likelihood and the mean next sentence prediction\n", | |
"likelihood.\n", | |
"</p>\n", | |
"<p>Training of BERTBASE was performed on 4\n", | |
"Cloud TPUs in Pod configuration (16 TPU chips\n", | |
"total).13 Training of BERTLARGE was performed\n", | |
"on 16 Cloud TPUs (64 TPU chips total). Each pre-\n", | |
"training took 4 days to complete.\n", | |
"</p>\n", | |
"<p>Longer sequences are disproportionately expen-\n", | |
"sive because attention is quadratic to the sequence\n", | |
"length. To speed up pretraing in our experiments,\n", | |
"we pre-train the model with sequence length of\n", | |
"128 for 90% of the steps. Then, we train the rest\n", | |
"10% of the steps of sequence of 512 to learn the\n", | |
"positional embeddings.\n", | |
"</p>\n", | |
"<p>A.3 Fine-tuning Procedure\n", | |
"</p>\n", | |
"<p>For fine-tuning, most model hyperparameters are\n", | |
"the same as in pre-training, with the exception of\n", | |
"the batch size, learning rate, and number of train-\n", | |
"ing epochs. The dropout probability was always\n", | |
"kept at 0.1. The optimal hyperparameter values\n", | |
"are task-specific, but we found the following range\n", | |
"of possible values to work well across all tasks:\n", | |
"</p>\n", | |
"<p>• Batch size: 16, 32\n", | |
"</p>\n", | |
"<p>13https://cloudplatform.googleblog.com/2018/06/Cloud-\n", | |
"TPU-now-offers-preemptible-pricing-and-global-\n", | |
"availability.html</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>• Learning rate (Adam): 5e-5, 3e-5, 2e-5\n", | |
"• Number of epochs: 2, 3, 4\n", | |
"</p>\n", | |
"<p>We also observed that large data sets (e.g.,\n", | |
"100k+ labeled training examples) were far less\n", | |
"sensitive to hyperparameter choice than small data\n", | |
"sets. Fine-tuning is typically very fast, so it is rea-\n", | |
"sonable to simply run an exhaustive search over\n", | |
"the above parameters and choose the model that\n", | |
"performs best on the development set.\n", | |
"</p>\n", | |
"<p>A.4 Comparison of BERT, ELMo ,and\n", | |
"OpenAI GPT\n", | |
"</p>\n", | |
"<p>Here we studies the differences in recent popular\n", | |
"representation learning models including ELMo,\n", | |
"OpenAI GPT and BERT. The comparisons be-\n", | |
"tween the model architectures are shown visually\n", | |
"in Figure 3. Note that in addition to the architec-\n", | |
"ture differences, BERT and OpenAI GPT are fine-\n", | |
"tuning approaches, while ELMo is a feature-based\n", | |
"approach.\n", | |
"</p>\n", | |
"<p>The most comparable existing pre-training\n", | |
"method to BERT is OpenAI GPT, which trains a\n", | |
"left-to-right Transformer LM on a large text cor-\n", | |
"pus. In fact, many of the design decisions in BERT\n", | |
"were intentionally made to make it as close to\n", | |
"GPT as possible so that the two methods could be\n", | |
"minimally compared. The core argument of this\n", | |
"work is that the bi-directionality and the two pre-\n", | |
"training tasks presented in Section 3.1 account for\n", | |
"the majority of the empirical improvements, but\n", | |
"we do note that there are several other differences\n", | |
"between how BERT and GPT were trained:\n", | |
"</p>\n", | |
"<p>• GPT is trained on the BooksCorpus (800M\n", | |
"words); BERT is trained on the BooksCor-\n", | |
"pus (800M words) and Wikipedia (2,500M\n", | |
"words).\n", | |
"</p>\n", | |
"<p>• GPT uses a sentence separator ([SEP]) and\n", | |
"classifier token ([CLS]) which are only in-\n", | |
"troduced at fine-tuning time; BERT learns\n", | |
"[SEP], [CLS] and sentence A/B embed-\n", | |
"dings during pre-training.\n", | |
"</p>\n", | |
"<p>• GPT was trained for 1M steps with a batch\n", | |
"size of 32,000 words; BERT was trained for\n", | |
"1M steps with a batch size of 128,000 words.\n", | |
"</p>\n", | |
"<p>• GPT used the same learning rate of 5e-5 for\n", | |
"all fine-tuning experiments; BERT chooses a\n", | |
"task-specific fine-tuning learning rate which\n", | |
"performs the best on the development set.\n", | |
"</p>\n", | |
"<p>To isolate the effect of these differences, we per-\n", | |
"form ablation experiments in Section 5.1 which\n", | |
"demonstrate that the majority of the improvements\n", | |
"are in fact coming from the two pre-training tasks\n", | |
"and the bidirectionality they enable.\n", | |
"</p>\n", | |
"<p>A.5 Illustrations of Fine-tuning on Different\n", | |
"Tasks\n", | |
"</p>\n", | |
"<p>The illustration of fine-tuning BERT on different\n", | |
"tasks can be seen in Figure 4. Our task-specific\n", | |
"models are formed by incorporating BERT with\n", | |
"one additional output layer, so a minimal num-\n", | |
"ber of parameters need to be learned from scratch.\n", | |
"Among the tasks, (a) and (b) are sequence-level\n", | |
"tasks while (c) and (d) are token-level tasks. In\n", | |
"the figure, E represents the input embedding, Ti\n", | |
"represents the contextual representation of token i,\n", | |
"[CLS] is the special symbol for classification out-\n", | |
"put, and [SEP] is the special symbol to separate\n", | |
"non-consecutive token sequences.\n", | |
"</p>\n", | |
"<p>B Detailed Experimental Setup\n", | |
"</p>\n", | |
"<p>B.1 Detailed Descriptions for the GLUE\n", | |
"Benchmark Experiments.\n", | |
"</p>\n", | |
"<p>Our GLUE results in Table1 are obtained\n", | |
"from https://gluebenchmark.com/\n", | |
"leaderboard and https://blog.\n", | |
"openai.com/language-unsupervised.\n", | |
"The GLUE benchmark includes the following\n", | |
"datasets, the descriptions of which were originally\n", | |
"summarized in Wang et al. (2018a):\n", | |
"</p>\n", | |
"<p>MNLI Multi-Genre Natural Language Inference\n", | |
"is a large-scale, crowdsourced entailment classifi-\n", | |
"cation task (Williams et al., 2018). Given a pair of\n", | |
"sentences, the goal is to predict whether the sec-\n", | |
"ond sentence is an entailment, contradiction, or\n", | |
"neutral with respect to the first one.\n", | |
"</p>\n", | |
"<p>QQP Quora Question Pairs is a binary classifi-\n", | |
"cation task where the goal is to determine if two\n", | |
"questions asked on Quora are semantically equiv-\n", | |
"alent (Chen et al., 2018).\n", | |
"</p>\n", | |
"<p>QNLI Question Natural Language Inference is\n", | |
"a version of the Stanford Question Answering\n", | |
"Dataset (Rajpurkar et al., 2016) which has been\n", | |
"converted to a binary classification task (Wang\n", | |
"et al., 2018a). The positive examples are (ques-\n", | |
"tion, sentence) pairs which do contain the correct\n", | |
"answer, and the negative examples are (question,\n", | |
"sentence) from the same paragraph which do not\n", | |
"contain the answer.</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://gluebenchmark.com/leaderboard\">https://gluebenchmark.com/leaderboard</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://gluebenchmark.com/leaderboard\">https://gluebenchmark.com/leaderboard</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://blog.openai.com/language-unsupervised\">https://blog.openai.com/language-unsupervised</a></div>\n", | |
"<div class=\"annotation\"><a href=\"https://blog.openai.com/language-unsupervised\">https://blog.openai.com/language-unsupervised</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>BERT\n", | |
"</p>\n", | |
"<p>E[CLS] E1 E[SEP]... EN E1’ ... EM’\n", | |
"</p>\n", | |
"<p>C T1 T[SEP]... TN T1’ ... TM’\n", | |
"</p>\n", | |
"<p>[CLS]\n", | |
"Tok \n", | |
"</p>\n", | |
"<p>1 [SEP]... Tok \n", | |
"N\n", | |
"</p>\n", | |
"<p>Tok \n", | |
"1 ... Tok\n", | |
"</p>\n", | |
"<p>M\n", | |
"</p>\n", | |
"<p>Question Paragraph\n", | |
"</p>\n", | |
"<p>BERT\n", | |
"</p>\n", | |
"<p>E[CLS] E1 E2\n", | |
" EN\n", | |
"</p>\n", | |
"<p>C T1 T2\n", | |
" TN\n", | |
"</p>\n", | |
"<p>Single Sentence \n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>BERT\n", | |
"</p>\n", | |
"<p>Tok 1 Tok 2 Tok N...[CLS]\n", | |
"</p>\n", | |
"<p>E[CLS] E1 E2\n", | |
" EN\n", | |
"</p>\n", | |
"<p>C T1 T2\n", | |
" TN\n", | |
"</p>\n", | |
"<p>Single Sentence \n", | |
"</p>\n", | |
"<p>B-PERO O\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>...E[CLS] E1 E[SEP]\n", | |
"</p>\n", | |
"<p>Class \n", | |
"Label\n", | |
"</p>\n", | |
"<p>... EN E1’ ... EM’\n", | |
"</p>\n", | |
"<p>C T1 T[SEP]... TN T1’ ... TM’\n", | |
"</p>\n", | |
"<p>Start/End Span\n", | |
"</p>\n", | |
"<p>Class \n", | |
"Label\n", | |
"</p>\n", | |
"<p>BERT\n", | |
"</p>\n", | |
"<p>Tok 1 Tok 2 Tok N...[CLS] Tok 1[CLS][CLS]\n", | |
"Tok \n", | |
"</p>\n", | |
"<p>1 [SEP]... Tok \n", | |
"N\n", | |
"</p>\n", | |
"<p>Tok \n", | |
"1 ... Tok\n", | |
"</p>\n", | |
"<p>M\n", | |
"</p>\n", | |
"<p>Sentence 1\n", | |
"</p>\n", | |
"<p>...\n", | |
"</p>\n", | |
"<p>Sentence 2\n", | |
"</p>\n", | |
"<p>Figure 4: Illustrations of Fine-tuning BERT on Different Tasks.\n", | |
"</p>\n", | |
"<p>SST-2 The Stanford Sentiment Treebank is a\n", | |
"binary single-sentence classification task consist-\n", | |
"ing of sentences extracted from movie reviews\n", | |
"with human annotations of their sentiment (Socher\n", | |
"et al., 2013).\n", | |
"</p>\n", | |
"<p>CoLA The Corpus of Linguistic Acceptability is\n", | |
"a binary single-sentence classification task, where\n", | |
"the goal is to predict whether an English sentence\n", | |
"is linguistically “acceptable” or not (Warstadt\n", | |
"et al., 2018).\n", | |
"</p>\n", | |
"<p>STS-B The Semantic Textual Similarity Bench-\n", | |
"mark is a collection of sentence pairs drawn from\n", | |
"news headlines and other sources (Cer et al.,\n", | |
"2017). They were annotated with a score from 1\n", | |
"to 5 denoting how similar the two sentences are in\n", | |
"terms of semantic meaning.\n", | |
"</p>\n", | |
"<p>MRPC Microsoft Research Paraphrase Corpus\n", | |
"consists of sentence pairs automatically extracted\n", | |
"from online news sources, with human annotations\n", | |
"</p>\n", | |
"<p>for whether the sentences in the pair are semanti-\n", | |
"cally equivalent (Dolan and Brockett, 2005).\n", | |
"</p>\n", | |
"<p>RTE Recognizing Textual Entailment is a bi-\n", | |
"nary entailment task similar to MNLI, but with\n", | |
"much less training data (Bentivogli et al., 2009).14\n", | |
"</p>\n", | |
"<p>WNLI Winograd NLI is a small natural lan-\n", | |
"guage inference dataset (Levesque et al., 2011).\n", | |
"The GLUE webpage notes that there are issues\n", | |
"with the construction of this dataset, 15 and every\n", | |
"trained system that’s been submitted to GLUE has\n", | |
"performed worse than the 65.1 baseline accuracy\n", | |
"of predicting the majority class. We therefore ex-\n", | |
"clude this set to be fair to OpenAI GPT. For our\n", | |
"GLUE submission, we always predicted the ma-\n", | |
"</p>\n", | |
"<p>14Note that we only report single-task fine-tuning results\n", | |
"in this paper. A multitask fine-tuning approach could poten-\n", | |
"tially push the performance even further. For example, we\n", | |
"did observe substantial improvements on RTE from multi-\n", | |
"task training with MNLI.\n", | |
"</p>\n", | |
"<p>15https://gluebenchmark.com/faq</p>\n", | |
"<p />\n", | |
"<div class=\"annotation\"><a href=\"https://gluebenchmark.com/faq\">https://gluebenchmark.com/faq</a></div>\n", | |
"</div>\n", | |
"<div class=\"page\"><p />\n", | |
"<p>jority class.\n", | |
"</p>\n", | |
"<p>C Additional Ablation Studies\n", | |
"</p>\n", | |
"<p>C.1 Effect of Number of Training Steps\n", | |
"</p>\n", | |
"<p>Figure 5 presents MNLI Dev accuracy after fine-\n", | |
"tuning from a checkpoint that has been pre-trained\n", | |
"for k steps. This allows us to answer the following\n", | |
"questions:\n", | |
"</p>\n", | |
"<p>1. Question: Does BERT really need such\n", | |
"a large amount of pre-training (128,000\n", | |
"words/batch * 1,000,000 steps) to achieve\n", | |
"high fine-tuning accuracy?\n", | |
"Answer: Yes, BERTBASE achieves almost\n", | |
"1.0% additional accuracy on MNLI when\n", | |
"trained on 1M steps compared to 500k steps.\n", | |
"</p>\n", | |
"<p>2. Question: Does MLM pre-training converge\n", | |
"slower than LTR pre-training, since only 15%\n", | |
"of words are predicted in each batch rather\n", | |
"than every word?\n", | |
"Answer: The MLM model does converge\n", | |
"slightly slower than the LTR model. How-\n", | |
"ever, in terms of absolute accuracy the MLM\n", | |
"model begins to outperform the LTR model\n", | |
"almost immediately.\n", | |
"</p>\n", | |
"<p>C.2 Ablation for Different Masking\n", | |
"Procedures\n", | |
"</p>\n", | |
"<p>In Section 3.1, we mention that BERT uses a\n", | |
"mixed strategy for masking the target tokens when\n", | |
"pre-training with the masked language model\n", | |
"(MLM) objective. The following is an ablation\n", | |
"study to evaluate the effect of different masking\n", | |
"strategies.\n", | |
"</p>\n", | |
"<p>200 400 600 800 1,000\n", | |
"</p>\n", | |
"<p>76\n", | |
"</p>\n", | |
"<p>78\n", | |
"</p>\n", | |
"<p>80\n", | |
"</p>\n", | |
"<p>82\n", | |
"</p>\n", | |
"<p>84\n", | |
"</p>\n", | |
"<p>Pre-training Steps (Thousands)\n", | |
"</p>\n", | |
"<p>M\n", | |
"N\n", | |
"</p>\n", | |
"<p>L\n", | |
"ID\n", | |
"</p>\n", | |
"<p>ev\n", | |
"A\n", | |
"</p>\n", | |
"<p>cc\n", | |
"ur\n", | |
"</p>\n", | |
"<p>ac\n", | |
"y\n", | |
"</p>\n", | |
"<p>BERTBASE (Masked LM)\n", | |
"BERTBASE (Left-to-Right)\n", | |
"</p>\n", | |
"<p>Figure 5: Ablation over number of training steps. This\n", | |
"shows the MNLI accuracy after fine-tuning, starting\n", | |
"from model parameters that have been pre-trained for\n", | |
"k steps. The x-axis is the value of k.\n", | |
"</p>\n", | |
"<p>Note that the purpose of the masking strategies\n", | |
"is to reduce the mismatch between pre-training\n", | |
"and fine-tuning, as the [MASK] symbol never ap-\n", | |
"pears during the fine-tuning stage. We report the\n", | |
"Dev results for both MNLI and NER. For NER,\n", | |
"we report both fine-tuning and feature-based ap-\n", | |
"proaches, as we expect the mismatch will be am-\n", | |
"plified for the feature-based approach as the model\n", | |
"will not have the chance to adjust the representa-\n", | |
"tions.\n", | |
"</p>\n", | |
"<p>Masking Rates Dev Set Results\n", | |
"</p>\n", | |
"<p>MASK SAME RND MNLI NER\n", | |
"Fine-tune Fine-tune Feature-based\n", | |
"</p>\n", | |
"<p>80% 10% 10% 84.2 95.4 94.9\n", | |
"100% 0% 0% 84.3 94.9 94.0\n", | |
"</p>\n", | |
"<p>80% 0% 20% 84.1 95.2 94.6\n", | |
"80% 20% 0% 84.4 95.2 94.7\n", | |
"</p>\n", | |
"<p>0% 20% 80% 83.7 94.8 94.6\n", | |
"0% 0% 100% 83.6 94.9 94.6\n", | |
"</p>\n", | |
"<p>Table 8: Ablation over different masking strategies.\n", | |
"</p>\n", | |
"<p>The results are presented in Table 8. In the table,\n", | |
"MASK means that we replace the target token with\n", | |
"the [MASK] symbol for MLM; SAME means that\n", | |
"we keep the target token as is; RND means that\n", | |
"we replace the target token with another random\n", | |
"token.\n", | |
"</p>\n", | |
"<p>The numbers in the left part of the table repre-\n", | |
"sent the probabilities of the specific strategies used\n", | |
"during MLM pre-training (BERT uses 80%, 10%,\n", | |
"10%). The right part of the paper represents the\n", | |
"Dev set results. For the feature-based approach,\n", | |
"we concatenate the last 4 layers of BERT as the\n", | |
"features, which was shown to be the best approach\n", | |
"in Section 5.3.\n", | |
"</p>\n", | |
"<p>From the table it can be seen that fine-tuning is\n", | |
"surprisingly robust to different masking strategies.\n", | |
"However, as expected, using only the MASK strat-\n", | |
"egy was problematic when applying the feature-\n", | |
"based approach to NER. Interestingly, using only\n", | |
"the RND strategy performs much worse than our\n", | |
"strategy as well.</p>\n", | |
"<p />\n", | |
"</div>\n", | |
"</body></html></blockquote>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"show(run(tika, \"https://arxiv.org/pdf/1810.04805\"))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The BERT paper took `0.24s` to parse. But note how no formatting is applied. One thing that novices learn when parsing PDFs is that the format is closer to an image than a Word document. The format is designed for viewing and printing, not parsing content out of.\n", | |
"\n", | |
"Next, we'll show where vision models shine - PDFs." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"14.562819242477417\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<blockquote><!DOCTYPE html>\n", | |
"<html lang=\"en\">\n", | |
"<head>\n", | |
" <meta charset=\"UTF-8\">\n", | |
" <style>\n", | |
" table {\n", | |
" border-collapse: separate;\n", | |
" /* Maintain separate borders */\n", | |
" border-spacing: 5px; /*\n", | |
" Space between cells */\n", | |
" width: 50%;\n", | |
" }\n", | |
" th, td {\n", | |
" border: 1px solid black;\n", | |
" /* Add lines etween cells */\n", | |
" padding: 8px; }\n", | |
" </style>\n", | |
" </head>\n", | |
"<h2>BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</h2>\n", | |
"<h2>Jacob Devlin Ming-Wei Chang Kenton Lee Kristina Toutanova Google AI Language</h2>\n", | |
"<p>{ jacobdevlin,mingweichang,kentonl,kristout } @google.com</p>\n", | |
"<h2>Abstract</h2>\n", | |
"<p>We introduce a new language representation model called BERT , which stands for B idirectional E ncoder R epresentations from T ransformers. Unlike recent language representation models (Peters et al., 2018a; Radford et al., 2018), BERT is designed to pretrain deep bidirectional representations from unlabeled text by jointly conditioning on both left and right context in all layers. As a result, the pre-trained BERT model can be finetuned with just one additional output layer to create state-of-the-art models for a wide range of tasks, such as question answering and language inference, without substantial taskspecific architecture modifications.</p>\n", | |
"<p>BERT is conceptually simple and empirically powerful. It obtains new state-of-the-art results on eleven natural language processing tasks, including pushing the GLUE score to 80.5% (7.7% point absolute improvement), MultiNLI accuracy to 86.7% (4.6% absolute improvement), SQuAD v1.1 question answering Test F1 to 93.2 (1.5 point absolute improvement) and SQuAD v2.0 Test F1 to 83.1 (5.1 point absolute improvement).</p>\n", | |
"<h2>1 Introduction</h2>\n", | |
"<p>Language model pre-training has been shown to be effective for improving many natural language processing tasks (Dai and Le, 2015; Peters et al., 2018a; Radford et al., 2018; Howard and Ruder, 2018). These include sentence-level tasks such as natural language inference (Bowman et al., 2015; Williams et al., 2018) and paraphrasing (Dolan and Brockett, 2005), which aim to predict the relationships between sentences by analyzing them holistically, as well as token-level tasks such as named entity recognition and question answering, where models are required to produce fine-grained output at the token level (Tjong Kim Sang and De Meulder, 2003; Rajpurkar et al., 2016).</p>\n", | |
"<p>There are two existing strategies for applying pre-trained language representations to downstream tasks: feature-based and fine-tuning . The feature-based approach, such as ELMo (Peters et al., 2018a), uses task-specific architectures that include the pre-trained representations as additional features. The fine-tuning approach, such as the Generative Pre-trained Transformer (OpenAI GPT) (Radford et al., 2018), introduces minimal task-specific parameters, and is trained on the downstream tasks by simply fine-tuning all pretrained parameters. The two approaches share the same objective function during pre-training, where they use unidirectional language models to learn general language representations.</p>\n", | |
"<p>We argue that current techniques restrict the power of the pre-trained representations, especially for the fine-tuning approaches. The major limitation is that standard language models are unidirectional, and this limits the choice of architectures that can be used during pre-training. For example, in OpenAI GPT, the authors use a left-toright architecture, where every token can only attend to previous tokens in the self-attention layers of the Transformer (Vaswani et al., 2017). Such restrictions are sub-optimal for sentence-level tasks, and could be very harmful when applying finetuning based approaches to token-level tasks such as question answering, where it is crucial to incorporate context from both directions.</p>\n", | |
"<p>In this paper, we improve the fine-tuning based approaches by proposing BERT: B idirectional E ncoder R epresentations from T ransformers. BERT alleviates the previously mentioned unidirectionality constraint by using a \"masked language model\" (MLM) pre-training objective, inspired by the Cloze task (Taylor, 1953). The masked language model randomly masks some of the tokens from the input, and the objective is to predict the original vocabulary id of the masked</p>\n", | |
"<p>word based only on its context. Unlike left-toright language model pre-training, the MLM objective enables the representation to fuse the left and the right context, which allows us to pretrain a deep bidirectional Transformer. In addition to the masked language model, we also use a \"next sentence prediction\" task that jointly pretrains text-pair representations. The contributions of our paper are as follows:</p>\n", | |
"<ul>\n", | |
"<li>· We demonstrate the importance of bidirectional pre-training for language representations. Unlike Radford et al. (2018), which uses unidirectional language models for pre-training, BERT uses masked language models to enable pretrained deep bidirectional representations. This is also in contrast to Peters et al. (2018a), which uses a shallow concatenation of independently trained left-to-right and right-to-left LMs.</li>\n", | |
"<li>· We show that pre-trained representations reduce the need for many heavily-engineered taskspecific architectures. BERT is the first finetuning based representation model that achieves state-of-the-art performance on a large suite of sentence-level and token-level tasks, outperforming many task-specific architectures.</li>\n", | |
"<li>· BERT advances the state of the art for eleven NLP tasks. The code and pre-trained models are available at https://github.com/ google-research/bert .</li>\n", | |
"</ul>\n", | |
"<h2>2 Related Work</h2>\n", | |
"<p>There is a long history of pre-training general language representations, and we briefly review the most widely-used approaches in this section.</p>\n", | |
"<h2>2.1 Unsupervised Feature-based Approaches</h2>\n", | |
"<p>Learning widely applicable representations of words has been an active area of research for decades, including non-neural (Brown et al., 1992; Ando and Zhang, 2005; Blitzer et al., 2006) and neural (Mikolov et al., 2013; Pennington et al., 2014) methods. Pre-trained word embeddings are an integral part of modern NLP systems, offering significant improvements over embeddings learned from scratch (Turian et al., 2010). To pretrain word embedding vectors, left-to-right language modeling objectives have been used (Mnih and Hinton, 2009), as well as objectives to discriminate correct from incorrect words in left and right context (Mikolov et al., 2013).</p>\n", | |
"<p>These approaches have been generalized to coarser granularities, such as sentence embeddings (Kiros et al., 2015; Logeswaran and Lee, 2018) or paragraph embeddings (Le and Mikolov, 2014). To train sentence representations, prior work has used objectives to rank candidate next sentences (Jernite et al., 2017; Logeswaran and Lee, 2018), left-to-right generation of next sentence words given a representation of the previous sentence (Kiros et al., 2015), or denoising autoencoder derived objectives (Hill et al., 2016).</p>\n", | |
"<p>ELMo and its predecessor (Peters et al., 2017, 2018a) generalize traditional word embedding research along a different dimension. They extract context-sensitive features from a left-to-right and a right-to-left language model. The contextual representation of each token is the concatenation of the left-to-right and right-to-left representations. When integrating contextual word embeddings with existing task-specific architectures, ELMo advances the state of the art for several major NLP benchmarks (Peters et al., 2018a) including question answering (Rajpurkar et al., 2016), sentiment analysis (Socher et al., 2013), and named entity recognition (Tjong Kim Sang and De Meulder, 2003). Melamud et al. (2016) proposed learning contextual representations through a task to predict a single word from both left and right context using LSTMs. Similar to ELMo, their model is feature-based and not deeply bidirectional. Fedus et al. (2018) shows that the cloze task can be used to improve the robustness of text generation models.</p>\n", | |
"<h2>2.2 Unsupervised Fine-tuning Approaches</h2>\n", | |
"<p>As with the feature-based approaches, the first works in this direction only pre-trained word embedding parameters from unlabeled text (Collobert and Weston, 2008).</p>\n", | |
"<p>More recently, sentence or document encoders which produce contextual token representations have been pre-trained from unlabeled text and fine-tuned for a supervised downstream task (Dai and Le, 2015; Howard and Ruder, 2018; Radford et al., 2018). The advantage of these approaches is that few parameters need to be learned from scratch. At least partly due to this advantage, OpenAI GPT (Radford et al., 2018) achieved previously state-of-the-art results on many sentencelevel tasks from the GLUE benchmark (Wang et al., 2018a). Left-to-right language model-</p>\n", | |
"<figure><figcaption>Figure 1: Overall pre-training and fine-tuning procedures for BERT. Apart from output layers, the same architectures are used in both pre-training and fine-tuning. The same pre-trained model parameters are used to initialize models for different down-stream tasks. During fine-tuning, all parameters are fine-tuned. [CLS] is a special symbol added in front of every input example, and [SEP] is a special separator token (e.g. separating questions/answers).</figcaption></figure>\n", | |
"<p>ing and auto-encoder objectives have been used for pre-training such models (Howard and Ruder, 2018; Radford et al., 2018; Dai and Le, 2015).</p>\n", | |
"<h2>2.3 Transfer Learning from Supervised Data</h2>\n", | |
"<p>There has also been work showing effective transfer from supervised tasks with large datasets, such as natural language inference (Conneau et al., 2017) and machine translation (McCann et al., 2017). Computer vision research has also demonstrated the importance of transfer learning from large pre-trained models, where an effective recipe is to fine-tune models pre-trained with ImageNet (Deng et al., 2009; Yosinski et al., 2014).</p>\n", | |
"<h2>3 BERT</h2>\n", | |
"<p>We introduce BERT and its detailed implementation in this section. There are two steps in our framework: pre-training and fine-tuning . During pre-training, the model is trained on unlabeled data over different pre-training tasks. For finetuning, the BERT model is first initialized with the pre-trained parameters, and all of the parameters are fine-tuned using labeled data from the downstream tasks. Each downstream task has separate fine-tuned models, even though they are initialized with the same pre-trained parameters. The question-answering example in Figure 1 will serve as a running example for this section.</p>\n", | |
"<p>A distinctive feature of BERT is its unified architecture across different tasks. There is mini-</p>\n", | |
"<p>mal difference between the pre-trained architecture and the final downstream architecture.</p>\n", | |
"<p>Model Architecture BERT's model architecture is a multi-layer bidirectional Transformer encoder based on the original implementation described in Vaswani et al. (2017) and released in the tensor2tensor library. 1 Because the use of Transformers has become common and our implementation is almost identical to the original, we will omit an exhaustive background description of the model architecture and refer readers to Vaswani et al. (2017) as well as excellent guides such as \"The Annotated Transformer.\" 2</p>\n", | |
"<p>In this work, we denote the number of layers (i.e., Transformer blocks) as L , the hidden size as H , and the number of self-attention heads as A . 3 We primarily report results on two model sizes: BERT$_{BASE}$ (L=12, H=768, A=12, Total Parameters=110M) and BERT$_{LARGE}$ (L=24, H=1024, A=16, Total Parameters=340M).</p>\n", | |
"<p>BERT$_{BASE}$ was chosen to have the same model size as OpenAI GPT for comparison purposes. Critically, however, the BERT Transformer uses bidirectional self-attention, while the GPT Transformer uses constrained self-attention where every token can only attend to context to its left. 4</p>\n", | |
"<p>$^{3}$In all cases we set the feed-forward/filter size to be 4 H , i.e., 3072 for the H = 768 and 4096 for the H = 1024 .</p>\n", | |
"<p>Input/Output Representations To make BERT handle a variety of down-stream tasks, our input representation is able to unambiguously represent both a single sentence and a pair of sentences (e.g., 〈 Question, Answer 〉 ) in one token sequence. Throughout this work, a \"sentence\" can be an arbitrary span of contiguous text, rather than an actual linguistic sentence. A \"sequence\" refers to the input token sequence to BERT, which may be a single sentence or two sentences packed together.</p>\n", | |
"<p>We use WordPiece embeddings (Wu et al., 2016) with a 30,000 token vocabulary. The first token of every sequence is always a special classification token ( [CLS] ). The final hidden state corresponding to this token is used as the aggregate sequence representation for classification tasks. Sentence pairs are packed together into a single sequence. We differentiate the sentences in two ways. First, we separate them with a special token ( [SEP] ). Second, we add a learned embedding to every token indicating whether it belongs to sentence A or sentence B . As shown in Figure 1, we denote input embedding as E , the final hidden vector of the special [CLS] token as C ∈ R $^{H}$, and the final hidden vector for the i th input token as T$_{i}$ ∈$_{R}$ $^{H}$.</p>\n", | |
"<p>For a given token, its input representation is constructed by summing the corresponding token, segment, and position embeddings. A visualization of this construction can be seen in Figure 2.</p>\n", | |
"<h2>3.1 Pre-training BERT</h2>\n", | |
"<p>Unlike Peters et al. (2018a) and Radford et al. (2018), we do not use traditional left-to-right or right-to-left language models to pre-train BERT. Instead, we pre-train BERT using two unsupervised tasks, described in this section. This step is presented in the left part of Figure 1.</p>\n", | |
"<p>Task #1: Masked LM Intuitively, it is reasonable to believe that a deep bidirectional model is strictly more powerful than either a left-to-right model or the shallow concatenation of a left-toright and a right-to-left model. Unfortunately, standard conditional language models can only be trained left-to-right or right-to-left, since bidirectional conditioning would allow each word to indirectly \"see itself\", and the model could trivially predict the target word in a multi-layered context.</p>\n", | |
"<p>former is often referred to as a \"Transformer encoder\" while the left-context-only version is referred to as a \"Transformer decoder\" since it can be used for text generation.</p>\n", | |
"<p>In order to train a deep bidirectional representation, we simply mask some percentage of the input tokens at random, and then predict those masked tokens. We refer to this procedure as a \"masked LM\" (MLM), although it is often referred to as a Cloze task in the literature (Taylor, 1953). In this case, the final hidden vectors corresponding to the mask tokens are fed into an output softmax over the vocabulary, as in a standard LM. In all of our experiments, we mask 15% of all WordPiece tokens in each sequence at random. In contrast to denoising auto-encoders (Vincent et al., 2008), we only predict the masked words rather than reconstructing the entire input.</p>\n", | |
"<p>Although this allows us to obtain a bidirectional pre-trained model, a downside is that we are creating a mismatch between pre-training and fine-tuning, since the [MASK] token does not appear during fine-tuning. To mitigate this, we do not always replace \"masked\" words with the actual [MASK] token. The training data generator chooses 15% of the token positions at random for prediction. If the i -th token is chosen, we replace the i -th token with (1) the [MASK] token 80% of the time (2) a random token 10% of the time (3) the unchanged i -th token 10% of the time. Then, T$_{i}$ will be used to predict the original token with cross entropy loss. We compare variations of this procedure in Appendix C.2.</p>\n", | |
"<h2>Task #2: Next Sentence Prediction (NSP)</h2>\n", | |
"<p>Many important downstream tasks such as Question Answering (QA) and Natural Language Inference (NLI) are based on understanding the relationship between two sentences, which is not directly captured by language modeling. In order to train a model that understands sentence relationships, we pre-train for a binarized next sentence prediction task that can be trivially generated from any monolingual corpus. Specifically, when choosing the sentences A and B for each pretraining example, 50% of the time B is the actual next sentence that follows A (labeled as IsNext ), and 50% of the time it is a random sentence from the corpus (labeled as NotNext ). As we show in Figure 1, C is used for next sentence prediction (NSP). 5 Despite its simplicity, we demonstrate in Section 5.1 that pre-training towards this task is very beneficial to both QA and NLI. 6</p>\n", | |
"<figure><figcaption>Figure 2: BERT input representation. The input embeddings are the sum of the token embeddings, the segmentation embeddings and the position embeddings.</figcaption></figure>\n", | |
"<p>The NSP task is closely related to representationlearning objectives used in Jernite et al. (2017) and Logeswaran and Lee (2018). However, in prior work, only sentence embeddings are transferred to down-stream tasks, where BERT transfers all parameters to initialize end-task model parameters.</p>\n", | |
"<p>Pre-training data The pre-training procedure largely follows the existing literature on language model pre-training. For the pre-training corpus we use the BooksCorpus (800M words) (Zhu et al., 2015) and English Wikipedia (2,500M words). For Wikipedia we extract only the text passages and ignore lists, tables, and headers. It is critical to use a document-level corpus rather than a shuffled sentence-level corpus such as the Billion Word Benchmark (Chelba et al., 2013) in order to extract long contiguous sequences.</p>\n", | |
"<h2>3.2 Fine-tuning BERT</h2>\n", | |
"<p>Fine-tuning is straightforward since the selfattention mechanism in the Transformer allows BERT to model many downstream taskswhether they involve single text or text pairs-by swapping out the appropriate inputs and outputs. For applications involving text pairs, a common pattern is to independently encode text pairs before applying bidirectional cross attention, such as Parikh et al. (2016); Seo et al. (2017). BERT instead uses the self-attention mechanism to unify these two stages, as encoding a concatenated text pair with self-attention effectively includes bidirectional cross attention between two sentences.</p>\n", | |
"<p>For each task, we simply plug in the taskspecific inputs and outputs into BERT and finetune all the parameters end-to-end. At the input, sentence A and sentence B from pre-training are analogous to (1) sentence pairs in paraphrasing, (2) hypothesis-premise pairs in entailment, (3) question-passage pairs in question answering, and</p>\n", | |
"<p>(4) a degenerate text-$_{}$ pair in text classification or sequence tagging. At the output, the token representations are fed into an output layer for tokenlevel tasks, such as sequence tagging or question answering, and the [CLS] representation is fed into an output layer for classification, such as entailment or sentiment analysis.</p>\n", | |
"<p>Compared to pre-training, fine-tuning is relatively inexpensive. All of the results in the paper can be replicated in at most 1 hour on a single Cloud TPU, or a few hours on a GPU, starting from the exact same pre-trained model. 7 We describe the task-specific details in the corresponding subsections of Section 4. More details can be found in Appendix A.5.</p>\n", | |
"<h2>4 Experiments</h2>\n", | |
"<p>In this section, we present BERT fine-tuning results on 11 NLP tasks.</p>\n", | |
"<h2>4.1 GLUE</h2>\n", | |
"<p>The General Language Understanding Evaluation (GLUE) benchmark (Wang et al., 2018a) is a collection of diverse natural language understanding tasks. Detailed descriptions of GLUE datasets are included in Appendix B.1.</p>\n", | |
"<p>To fine-tune on GLUE, we represent the input sequence (for single sentence or sentence pairs) as described in Section 3, and use the final hidden vector C ∈ R H corresponding to the first input token ( [CLS] ) as the aggregate representation. The only new parameters introduced during fine-tuning are classification layer weights W ∈ R K × $^{H}$, where K is the number of labels. We compute a standard classification loss with C and W , i.e., log(softmax( CW $^{T}$)) .</p>\n", | |
"<table><caption>Table 1: GLUE Test results, scored by the evaluation server ( https://gluebenchmark.com/leaderboard ). The number below each task denotes the number of training examples. The \"Average\" column is slightly different than the official GLUE score, since we exclude the problematic WNLI set. 8 BERT and OpenAI GPT are singlemodel, single task. F1 scores are reported for QQP and MRPC, Spearman correlations are reported for STS-B, and accuracy scores are reported for the other tasks. We exclude entries that use BERT as one of their components.</caption><tbody><tr><th>System</th><th>MNLI-(m/mm) 392k</th><th>QQP 363k</th><th>QNLI 108k</th><th>SST-2 67k</th><th>CoLA 8.5k</th><th>STS-B 5.7k</th><th>MRPC 3.5k</th><th>RTE 2.5k</th><th>Average -</th></tr><tr><td>Pre-OpenAI SOTA</td><td>80.6/80.1</td><td>66.1</td><td>82.3</td><td>93.2</td><td>35.0</td><td>81.0</td><td>86.0</td><td>61.7</td><td>74.0</td></tr><tr><td>BiLSTM+ELMo+Attn</td><td>76.4/76.1</td><td>64.8</td><td>79.8</td><td>90.4</td><td>36.0</td><td>73.3</td><td>84.9</td><td>56.8</td><td>71.0</td></tr><tr><td>OpenAI GPT</td><td>82.1/81.4</td><td>70.3</td><td>87.4</td><td>91.3</td><td>45.4</td><td>80.0</td><td>82.3</td><td>56.0</td><td>75.1</td></tr><tr><td>BERT$_{BASE}$</td><td>84.6/83.4</td><td>71.2</td><td>90.5</td><td>93.5</td><td>52.1</td><td>85.8</td><td>88.9</td><td>66.4</td><td>79.6</td></tr><tr><td>BERT$_{LARGE}$</td><td>86.7/85.9</td><td>72.1</td><td>92.7</td><td>94.9</td><td>60.5</td><td>86.5</td><td>89.3</td><td>70.1</td><td>82.1</td></tr></tbody></table>\n", | |
"<p>We use a batch size of 32 and fine-tune for 3 epochs over the data for all GLUE tasks. For each task, we selected the best fine-tuning learning rate (among 5e-5, 4e-5, 3e-5, and 2e-5) on the Dev set. Additionally, for BERT$_{LARGE}$ we found that finetuning was sometimes unstable on small datasets, so we ran several random restarts and selected the best model on the Dev set. With random restarts, we use the same pre-trained checkpoint but perform different fine-tuning data shuffling and classifier layer initialization. 9</p>\n", | |
"<p>Results are presented in Table 1. Both BERT$_{BASE}$ and BERT$_{LARGE}$ outperform all systems on all tasks by a substantial margin, obtaining 4.5% and 7.0% respective average accuracy improvement over the prior state of the art. Note that BERT$_{BASE}$ and OpenAI GPT are nearly identical in terms of model architecture apart from the attention masking. For the largest and most widely reported GLUE task, MNLI, BERT obtains a 4.6% absolute accuracy improvement. On the official GLUE leaderboard$^{10}$, BERT$_{LARGE}$ obtains a score of 80.5, compared to OpenAI GPT, which obtains 72.8 as of the date of writing.</p>\n", | |
"<p>We find that BERT$_{LARGE}$ significantly outperforms BERT$_{BASE}$ across all tasks, especially those with very little training data. The effect of model size is explored more thoroughly in Section 5.2.</p>\n", | |
"<h2>4.2 SQuAD v1.1</h2>\n", | |
"<p>The Stanford Question Answering Dataset (SQuAD v1.1) is a collection of 100k crowdsourced question/answer pairs (Rajpurkar et al., 2016). Given a question and a passage from</p>\n", | |
"<p>Wikipedia containing the answer, the task is to predict the answer text span in the passage.</p>\n", | |
"<p>As shown in Figure 1, in the question answering task, we represent the input question and passage as a single packed sequence, with the question using the A embedding and the passage using the B embedding. We only introduce a start vector S ∈ R H and an end vector E ∈ R H during fine-tuning. The probability of word i being the start of the answer span is computed as a dot product between T$_{i}$ and S followed by a softmax over all of the words in the paragraph: P$_{i}$ = e S · T i ∑ j e S · T j . The analogous formula is used for the end of the answer span. The score of a candidate span from position i to position j is defined as S · T$_{i}$ + E · T$_{j}$ , and the maximum scoring span where j ≥ i is used as a prediction. The training objective is the sum of the log-likelihoods of the correct start and end positions. We fine-tune for 3 epochs with a learning rate of 5e-5 and a batch size of 32.</p>\n", | |
"<p>Table 2 shows top leaderboard entries as well as results from top published systems (Seo et al., 2017; Clark and Gardner, 2018; Peters et al., 2018a; Hu et al., 2018). The top results from the SQuAD leaderboard do not have up-to-date public system descriptions available, 11 and are allowed to use any public data when training their systems. We therefore use modest data augmentation in our system by first fine-tuning on TriviaQA (Joshi et al., 2017) befor fine-tuning on SQuAD.</p>\n", | |
"<p>Our best performing system outperforms the top leaderboard system by +1.5 F1 in ensembling and +1.3 F1 as a single system. In fact, our single BERT model outperforms the top ensemble system in terms of F1 score. Without TriviaQA fine-</p>\n", | |
"<table><caption>Table 2: SQuAD 1.1 results. The BERT ensemble is 7x systems which use different pre-training checkpoints and fine-tuning seeds.</caption><tbody><tr><td>System</td><th colspan=\"2\">Dev</th><th colspan=\"2\">Test</th></tr><tr><td></td><th>EM</th><th>F1</th><th>EM</th><th>F1</th></tr><tr><td colspan=\"5\">Top Leaderboard Systems (Dec 10th, 2018)</td></tr><tr><td>Human</td><td>-</td><td>-</td><td>82.3</td><td>91.2</td></tr><tr><td>#1 Ensemble - nlnet</td><td>-</td><td>-</td><td>86.0</td><td>91.7</td></tr><tr><td>#2 Ensemble - QANet</td><td>-</td><td>-</td><td>84.5</td><td>90.5</td></tr><tr><td colspan=\"5\">Published</td></tr><tr><td>BiDAF+ELMo (Single)</td><td>-</td><td>85.6</td><td>-</td><td>85.8</td></tr><tr><td>R.M. Reader (Ensemble)</td><td>81.2</td><td></td><td></td><td>87.9 82.3 88.5</td></tr><tr><td colspan=\"5\">Ours</td></tr><tr><td>BERT$_{BASE}$ (Single)</td><td>80.8</td><td>88.5</td><td>-</td><td>-</td></tr><tr><td>BERT$_{LARGE}$ (Single)</td><td>84.1</td><td>90.9</td><td>-</td><td>-</td></tr><tr><td>BERT$_{LARGE}$ (Ensemble)</td><td>85.8</td><td>91.8</td><td>-</td><td>-</td></tr><tr><td>BERT$_{LARGE}$ (Sgl.+TriviaQA)</td><td>84.2</td><td></td><td></td><td>91.1 85.1 91.8</td></tr><tr><td>BERT$_{LARGE}$ (Ens.+TriviaQA)</td><td>86.2</td><td></td><td>92.2 87.4 93.2</td><td></td></tr></tbody></table>\n", | |
"<table><caption>Table 3: SQuAD 2.0 results. We exclude entries that use BERT as one of their components.</caption><tbody><tr><td>System</td><th colspan=\"2\">Dev</th><th colspan=\"2\">Test</th></tr><tr><td></td><th>EM</th><th>F1</th><th>EM</th><th>F1</th></tr><tr><td>Top Leaderboard Systems (Dec 10th, 2018)</td><td></td><td></td><td></td><td></td></tr><tr><td>Human</td><td>86.3</td><td></td><td></td><td>89.0 86.9 89.5</td></tr><tr><td>#1 Single - MIR-MRC (F-Net)</td><td>-</td><td>-</td><td>74.8</td><td>78.0</td></tr><tr><td>#2 Single - nlnet</td><td>-</td><td>-</td><td>74.2</td><td>77.1</td></tr><tr><td>unet (Ensemble)</td><td>-</td><td>-</td><td>71.4</td><td>74.9</td></tr><tr><td>SLQA+ (Single)</td><td>-</td><td></td><td>71.4</td><td>74.4</td></tr></tbody></table>\n", | |
"<p>tuning data, we only lose 0.1-0.4 F1, still outperforming all existing systems by a wide margin. 12</p>\n", | |
"<h2>4.3 SQuAD v2.0</h2>\n", | |
"<p>The SQuAD 2.0 task extends the SQuAD 1.1 problem definition by allowing for the possibility that no short answer exists in the provided paragraph, making the problem more realistic.</p>\n", | |
"<p>We use a simple approach to extend the SQuAD v1.1 BERT model for this task. We treat questions that do not have an answer as having an answer span with start and end at the [CLS] token. The probability space for the start and end answer span positions is extended to include the position of the [CLS] token. For prediction, we compare the score of the no-answer span: s$_{null}$ = S · C + E · C to the score of the best non-null span</p>\n", | |
"<table><caption>Table 4: SWAG Dev and Test accuracies. $^{†}$Human performance is measured with 100 samples, as reported in the SWAG paper.</caption><tbody><tr><td>System</td><th>Dev</th><th>Test</th></tr><tr><td>ESIM+GloVe</td><td>51.9</td><td>52.7</td></tr><tr><td>ESIM+ELMo</td><td>59.1</td><td>59.2</td></tr><tr><td>OpenAI GPT</td><td>-</td><td>78.0</td></tr><tr><td>BERT$_{BASE}$</td><td>81.6</td><td>-</td></tr><tr><td>BERT$_{LARGE}$</td><td>86.6</td><td>86.3</td></tr><tr><td>Human (expert) †</td><td>-</td><td>85.0</td></tr><tr><td>Human (5 annotations) †</td><td>-</td><td>88.0</td></tr></tbody></table>\n", | |
"<p>ˆ s$_{i,j}$ = max$_{j}$$_{≥}$$_{i}$ S · T$_{i}$ + E · T$_{j}$ . We predict a non-null answer when ˆ s$_{i,j}$ > s$_{null}$ + τ , where the threshold τ is selected on the dev set to maximize F1. We did not use TriviaQA data for this model. We fine-tuned for 2 epochs with a learning rate of 5e-5 and a batch size of 48.</p>\n", | |
"<p>The results compared to prior leaderboard entries and top published work (Sun et al., 2018; Wang et al., 2018b) are shown in Table 3, excluding systems that use BERT as one of their components. We observe a +5.1 F1 improvement over the previous best system.</p>\n", | |
"<h2>4.4 SWAG</h2>\n", | |
"<p>The Situations With Adversarial Generations (SWAG) dataset contains 113k sentence-pair completion examples that evaluate grounded commonsense inference (Zellers et al., 2018). Given a sentence, the task is to choose the most plausible continuation among four choices.</p>\n", | |
"<p>When fine-tuning on the SWAG dataset, we construct four input sequences, each containing the concatenation of the given sentence (sentence A ) and a possible continuation (sentence B ). The only task-specific parameters introduced is a vector whose dot product with the [CLS] token representation C denotes a score for each choice which is normalized with a softmax layer.</p>\n", | |
"<p>We fine-tune the model for 3 epochs with a learning rate of 2e-5 and a batch size of 16. Results are presented in Table 4. BERT$_{LARGE}$ outperforms the authors' baseline ESIM+ELMo system by +27.1% and OpenAI GPT by 8.3%.</p>\n", | |
"<h2>5 Ablation Studies</h2>\n", | |
"<p>In this section, we perform ablation experiments over a number of facets of BERT in order to better understand their relative importance. Additional</p>\n", | |
"<table><caption>Table 5: Ablation over the pre-training tasks using the BERT$_{BASE}$ architecture. \"No NSP\" is trained without the next sentence prediction task. \"LTR & No NSP\" is trained as a left-to-right LM without the next sentence prediction, like OpenAI GPT. \"+ BiLSTM\" adds a randomly initialized BiLSTM on top of the \"LTR + No NSP\" model during fine-tuning.</caption><tbody><tr><td>Tasks</td><td>(Acc)</td><th>(Acc)</th><th>Dev Set MNLI-m QNLI MRPC SST-2 SQuAD (Acc)</th><th>(Acc)</th><th>(F1)</th></tr><tr><td>BERT$_{BASE}$</td><td>84.4</td><td>88.4</td><td>86.7</td><td>92.7</td><td>88.5</td></tr><tr><td>No NSP</td><td>83.9</td><td>84.9</td><td>86.5</td><td>92.6</td><td>87.9</td></tr><tr><td>LTR & No NSP</td><td>82.1</td><td>84.3</td><td>77.5</td><td>92.1</td><td>77.8</td></tr><tr><td>+ BiLSTM</td><td>82.1</td><td>84.1</td><td>75.7</td><td>91.6</td><td>84.9</td></tr></tbody></table>\n", | |
"<p>ablation studies can be found in Appendix C.</p>\n", | |
"<h2>5.1 Effect of Pre-training Tasks</h2>\n", | |
"<p>We demonstrate the importance of the deep bidirectionality of BERT by evaluating two pretraining objectives using exactly the same pretraining data, fine-tuning scheme, and hyperparameters as BERT$_{BASE}$:</p>\n", | |
"<p>No NSP : A bidirectional model which is trained using the \"masked LM\" (MLM) but without the \"next sentence prediction\" (NSP) task.</p>\n", | |
"<p>LTR & No NSP : A left-context-only model which is trained using a standard Left-to-Right (LTR) LM, rather than an MLM. The left-only constraint was also applied at fine-tuning, because removing it introduced a pre-train/fine-tune mismatch that degraded downstream performance. Additionally, this model was pre-trained without the NSP task. This is directly comparable to OpenAI GPT, but using our larger training dataset, our input representation, and our fine-tuning scheme.</p>\n", | |
"<p>We first examine the impact brought by the NSP task. In Table 5, we show that removing NSP hurts performance significantly on QNLI, MNLI, and SQuAD 1.1. Next, we evaluate the impact of training bidirectional representations by comparing \"No NSP\" to \"LTR & No NSP\". The LTR model performs worse than the MLM model on all tasks, with large drops on MRPC and SQuAD.</p>\n", | |
"<p>For SQuAD it is intuitively clear that a LTR model will perform poorly at token predictions, since the token-level hidden states have no rightside context. In order to make a good faith attempt at strengthening the LTR system, we added a randomly initialized BiLSTM on top. This does significantly improve results on SQuAD, but the</p>\n", | |
"<p>results are still far worse than those of the pretrained bidirectional models. The BiLSTM hurts performance on the GLUE tasks.</p>\n", | |
"<p>We recognize that it would also be possible to train separate LTR and RTL models and represent each token as the concatenation of the two models, as ELMo does. However: (a) this is twice as expensive as a single bidirectional model; (b) this is non-intuitive for tasks like QA, since the RTL model would not be able to condition the answer on the question; (c) this it is strictly less powerful than a deep bidirectional model, since it can use both left and right context at every layer.</p>\n", | |
"<h2>5.2 Effect of Model Size</h2>\n", | |
"<p>In this section, we explore the effect of model size on fine-tuning task accuracy. We trained a number of BERT models with a differing number of layers, hidden units, and attention heads, while otherwise using the same hyperparameters and training procedure as described previously.</p>\n", | |
"<p>Results on selected GLUE tasks are shown in Table 6. In this table, we report the average Dev Set accuracy from 5 random restarts of fine-tuning. We can see that larger models lead to a strict accuracy improvement across all four datasets, even for MRPC which only has 3,600 labeled training examples, and is substantially different from the pre-training tasks. It is also perhaps surprising that we are able to achieve such significant improvements on top of models which are already quite large relative to the existing literature. For example, the largest Transformer explored in Vaswani et al. (2017) is (L=6, H=1024, A=16) with 100M parameters for the encoder, and the largest Transformer we have found in the literature is (L=64, H=512, A=2) with 235M parameters (Al-Rfou et al., 2018). By contrast, BERT$_{BASE}$ contains 110M parameters and BERT$_{LARGE}$ contains 340M parameters.</p>\n", | |
"<p>It has long been known that increasing the model size will lead to continual improvements on large-scale tasks such as machine translation and language modeling, which is demonstrated by the LM perplexity of held-out training data shown in Table 6. However, we believe that this is the first work to demonstrate convincingly that scaling to extreme model sizes also leads to large improvements on very small scale tasks, provided that the model has been sufficiently pre-trained. Peters et al. (2018b) presented</p>\n", | |
"<p>mixed results on the downstream task impact of increasing the pre-trained bi-LM size from two to four layers and Melamud et al. (2016) mentioned in passing that increasing hidden dimension size from 200 to 600 helped, but increasing further to 1,000 did not bring further improvements. Both of these prior works used a featurebased approach - we hypothesize that when the model is fine-tuned directly on the downstream tasks and uses only a very small number of randomly initialized additional parameters, the taskspecific models can benefit from the larger, more expressive pre-trained representations even when downstream task data is very small.</p>\n", | |
"<h2>5.3 Feature-based Approach with BERT</h2>\n", | |
"<p>All of the BERT results presented so far have used the fine-tuning approach, where a simple classification layer is added to the pre-trained model, and all parameters are jointly fine-tuned on a downstream task. However, the feature-based approach, where fixed features are extracted from the pretrained model, has certain advantages. First, not all tasks can be easily represented by a Transformer encoder architecture, and therefore require a task-specific model architecture to be added. Second, there are major computational benefits to pre-compute an expensive representation of the training data once and then run many experiments with cheaper models on top of this representation.</p>\n", | |
"<p>In this section, we compare the two approaches by applying BERT to the CoNLL-2003 Named Entity Recognition (NER) task (Tjong Kim Sang and De Meulder, 2003). In the input to BERT, we use a case-preserving WordPiece model, and we include the maximal document context provided by the data. Following standard practice, we formulate this as a tagging task but do not use a CRF</p>\n", | |
"<table><caption>Table 6: Ablation over BERT model size. #L = the number of layers; #H = hidden size; #A = number of attention heads. \"LM (ppl)\" is the masked LM perplexity of held-out training data.</caption><tbody><tr><th colspan=\"3\">Hyperparams</th><td></td><th colspan=\"3\">Dev Set Accuracy</th></tr><tr><th>#L</th><td></td><td></td><td></td><th>#H #A LM (ppl) MNLI-m MRPC SST-2</th><td></td><td></td></tr><tr><td>3</td><td>768</td><td>12</td><td>5.84</td><td>77.9</td><td>79.8</td><td>88.4</td></tr><tr><td>6</td><td>768</td><td>3</td><td>5.24</td><td>80.6</td><td>82.2</td><td>90.7</td></tr><tr><td>6</td><td>768</td><td>12</td><td>4.68</td><td>81.9</td><td>84.8</td><td>91.3</td></tr><tr><td>12</td><td>768</td><td>12</td><td>3.99</td><td>84.4</td><td>86.7</td><td>92.9</td></tr><tr><td>12</td><td>1024</td><td>16</td><td>3.54</td><td>85.7</td><td>86.9</td><td>93.3</td></tr><tr><td>24</td><td>1024</td><td>16</td><td>3.23</td><td>86.6</td><td>87.8</td><td>93.7</td></tr></tbody></table>\n", | |
"<table><caption>Table 7: CoNLL-2003 Named Entity Recognition results. Hyperparameters were selected using the Dev set. The reported Dev and Test scores are averaged over 5 random restarts using those hyperparameters.</caption><tbody><tr><td>System</td><td></td><th>Dev F1 Test F1</th></tr><tr><td>ELMo (Peters et al., 2018a)</td><td>95.7</td><td>92.2</td></tr><tr><td>CVT (Clark et al., 2018)</td><td>-</td><td>92.6</td></tr><tr><td>CSE (Akbik et al., 2018)</td><td>-</td><td>93.1</td></tr><tr><td>Fine-tuning approach</td><td></td><td></td></tr><tr><td>BERT$_{LARGE}$</td><td>96.6</td><td>92.8</td></tr><tr><td>BERT$_{BASE}$</td><td>96.4</td><td>92.4</td></tr><tr><td>Feature-based approach (BERT$_{BASE}$)</td><td></td><td></td></tr><tr><td>Embeddings</td><td>91.0</td><td>-</td></tr><tr><td>Second-to-Last Hidden</td><td>95.6</td><td>-</td></tr><tr><td>Last Hidden</td><td>94.9</td><td>-</td></tr><tr><td>Weighted Sum Last Four Hidden</td><td>95.9</td><td>-</td></tr><tr><td>Concat Last Four Hidden</td><td>96.1</td><td>-</td></tr><tr><td>Weighted Sum All 12 Layers</td><td>95.5</td><td>-</td></tr></tbody></table>\n", | |
"<p>layer in the output. We use the representation of the first sub-token as the input to the token-level classifier over the NER label set.</p>\n", | |
"<p>To ablate the fine-tuning approach, we apply the feature-based approach by extracting the activations from one or more layers without fine-tuning any parameters of BERT. These contextual embeddings are used as input to a randomly initialized two-layer 768-dimensional BiLSTM before the classification layer.</p>\n", | |
"<p>Results are presented in Table 7. BERT$_{LARGE}$ performs competitively with state-of-the-art methods. The best performing method concatenates the token representations from the top four hidden layers of the pre-trained Transformer, which is only 0.3 F1 behind fine-tuning the entire model. This demonstrates that BERT is effective for both finetuning and feature-based approaches.</p>\n", | |
"<h2>6 Conclusion</h2>\n", | |
"<p>Recent empirical improvements due to transfer learning with language models have demonstrated that rich, unsupervised pre-training is an integral part of many language understanding systems. In particular, these results enable even low-resource tasks to benefit from deep unidirectional architectures. Our major contribution is further generalizing these findings to deep bidirectional architectures, allowing the same pre-trained model to successfully tackle a broad set of NLP tasks.</p>\n", | |
"<h2>References</h2>\n", | |
"<p>Alan Akbik, Duncan Blythe, and Roland Vollgraf. 2018. Contextual string embeddings for sequence labeling. In Proceedings of the 27th International Conference on Computational Linguistics , pages 1638-1649.</p>\n", | |
"<p>Rami Al-Rfou, Dokook Choe, Noah Constant, Mandy Guo, and Llion Jones. 2018. Character-level language modeling with deeper self-attention. arXiv preprint arXiv:1808.04444 .</p>\n", | |
"<p>Rie Kubota Ando and Tong Zhang. 2005. A framework for learning predictive structures from multiple tasks and unlabeled data. Journal of Machine Learning Research , 6(Nov):1817-1853.</p>\n", | |
"<p>Luisa Bentivogli, Bernardo Magnini, Ido Dagan, Hoa Trang Dang, and Danilo Giampiccolo. 2009. The fifth PASCAL recognizing textual entailment challenge. In TAC . NIST.</p>\n", | |
"<p>John Blitzer, Ryan McDonald, and Fernando Pereira. 2006. Domain adaptation with structural correspondence learning. In Proceedings of the 2006 conference on empirical methods in natural language processing , pages 120-128. Association for Computational Linguistics.</p>\n", | |
"<p>Samuel R. Bowman, Gabor Angeli, Christopher Potts, and Christopher D. Manning. 2015. A large annotated corpus for learning natural language inference. In EMNLP . Association for Computational Linguistics.</p>\n", | |
"<p>Peter F Brown, Peter V Desouza, Robert L Mercer, Vincent J Della Pietra, and Jenifer C Lai. 1992. Class-based n-gram models of natural language. Computational linguistics , 18(4):467-479.</p>\n", | |
"<p>Daniel Cer, Mona Diab, Eneko Agirre, Inigo LopezGazpio, and Lucia Specia. 2017. Semeval-2017 task 1: Semantic textual similarity multilingual and crosslingual focused evaluation. In Proceedings of the 11th International Workshop on Semantic Evaluation (SemEval-2017) , pages 1-14, Vancouver, Canada. Association for Computational Linguistics.</p>\n", | |
"<p>Ciprian Chelba, Tomas Mikolov, Mike Schuster, Qi Ge, Thorsten Brants, Phillipp Koehn, and Tony Robinson. 2013. One billion word benchmark for measuring progress in statistical language modeling. arXiv preprint arXiv:1312.3005 .</p>\n", | |
"<p>Z. Chen, H. Zhang, X. Zhang, and L. Zhao. 2018. Quora question pairs.</p>\n", | |
"<p>Christopher Clark and Matt Gardner. 2018. Simple and effective multi-paragraph reading comprehension. In ACL .</p>\n", | |
"<p>Kevin Clark, Minh-Thang Luong, Christopher D Manning, and Quoc Le. 2018. Semi-supervised sequence modeling with cross-view training. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing , pages 19141925.</p>\n", | |
"<p>Ronan Collobert and Jason Weston. 2008. A unified architecture for natural language processing: Deep neural networks with multitask learning. In Proceedings of the 25th international conference on Machine learning , pages 160-167. ACM.</p>\n", | |
"<p>Alexis Conneau, Douwe Kiela, Holger Schwenk, Lo¨ıc Barrault, and Antoine Bordes. 2017. Supervised learning of universal sentence representations from natural language inference data. In Proceedings of the 2017 Conference on Empirical Methods in Natural Language Processing , pages 670-680, Copenhagen, Denmark. Association for Computational Linguistics.</p>\n", | |
"<p>Andrew M Dai and Quoc V Le. 2015. Semi-supervised sequence learning. In Advances in neural information processing systems , pages 3079-3087.</p>\n", | |
"<p>J. Deng, W. Dong, R. Socher, L.-J. Li, K. Li, and L. FeiFei. 2009. ImageNet: A Large-Scale Hierarchical Image Database. In CVPR09 .</p>\n", | |
"<p>William B Dolan and Chris Brockett. 2005. Automatically constructing a corpus of sentential paraphrases. In Proceedings of the Third International Workshop on Paraphrasing (IWP2005) .</p>\n", | |
"<p>William Fedus, Ian Goodfellow, and Andrew M Dai. 2018. Maskgan: Better text generation via filling in the . arXiv preprint arXiv:1801.07736 .</p>\n", | |
"<p>Dan Hendrycks and Kevin Gimpel. 2016. Bridging nonlinearities and stochastic regularizers with gaussian error linear units. CoRR , abs/1606.08415.</p>\n", | |
"<p>Felix Hill, Kyunghyun Cho, and Anna Korhonen. 2016. Learning distributed representations of sentences from unlabelled data. In Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies . Association for Computational Linguistics.</p>\n", | |
"<p>Jeremy Howard and Sebastian Ruder. 2018. Universal language model fine-tuning for text classification. In ACL . Association for Computational Linguistics.</p>\n", | |
"<p>Minghao Hu, Yuxing Peng, Zhen Huang, Xipeng Qiu, Furu Wei, and Ming Zhou. 2018. Reinforced mnemonic reader for machine reading comprehension. In IJCAI .</p>\n", | |
"<p>Yacine Jernite, Samuel R. Bowman, and David Sontag. 2017. Discourse-based objectives for fast unsupervised sentence representation learning. CoRR , abs/1705.00557.</p>\n", | |
"<p>Mandar Joshi, Eunsol Choi, Daniel S Weld, and Luke Zettlemoyer. 2017. Triviaqa: A large scale distantly supervised challenge dataset for reading comprehension. In ACL .</p>\n", | |
"<p>Ryan Kiros, Yukun Zhu, Ruslan R Salakhutdinov, Richard Zemel, Raquel Urtasun, Antonio Torralba, and Sanja Fidler. 2015. Skip-thought vectors. In Advances in neural information processing systems , pages 3294-3302.</p>\n", | |
"<p>Quoc Le and Tomas Mikolov. 2014. Distributed representations of sentences and documents. In International Conference on Machine Learning , pages 1188-1196.</p>\n", | |
"<p>Hector J Levesque, Ernest Davis, and Leora Morgenstern. 2011. The winograd schema challenge. In Aaai spring symposium: Logical formalizations of commonsense reasoning , volume 46, page 47.</p>\n", | |
"<p>Lajanugen Logeswaran and Honglak Lee. 2018. An efficient framework for learning sentence representations. In International Conference on Learning Representations .</p>\n", | |
"<p>Bryan McCann, James Bradbury, Caiming Xiong, and Richard Socher. 2017. Learned in translation: Contextualized word vectors. In NIPS .</p>\n", | |
"<p>Oren Melamud, Jacob Goldberger, and Ido Dagan. 2016. context2vec: Learning generic context embedding with bidirectional LSTM. In CoNLL .</p>\n", | |
"<p>Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg S Corrado, and Jeff Dean. 2013. Distributed representations of words and phrases and their compositionality. In Advances in Neural Information Processing Systems 26 , pages 3111-3119. Curran Associates, Inc.</p>\n", | |
"<p>Andriy Mnih and Geoffrey E Hinton. 2009. A scalable hierarchical distributed language model. In D. Koller, D. Schuurmans, Y. Bengio, and L. Bottou, editors, Advances in Neural Information Processing Systems 21 , pages 1081-1088. Curran Associates, Inc.</p>\n", | |
"<p>Ankur P Parikh, Oscar Tackstrom, Dipanjan Das, and Jakob Uszkoreit. 2016. A decomposable attention model for natural language inference. In EMNLP .</p>\n", | |
"<p>Jeffrey Pennington, Richard Socher, and Christopher D. Manning. 2014. Glove: Global vectors for word representation. In Empirical Methods in Natural Language Processing (EMNLP) , pages 15321543.</p>\n", | |
"<p>Matthew Peters, Waleed Ammar, Chandra Bhagavatula, and Russell Power. 2017. Semi-supervised sequence tagging with bidirectional language models. In ACL .</p>\n", | |
"<p>Matthew Peters, Mark Neumann, Mohit Iyyer, Matt Gardner, Christopher Clark, Kenton Lee, and Luke Zettlemoyer. 2018a. Deep contextualized word representations. In NAACL .</p>\n", | |
"<p>Matthew Peters, Mark Neumann, Luke Zettlemoyer, and Wen-tau Yih. 2018b. Dissecting contextual word embeddings: Architecture and representation. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing , pages 1499-1509.</p>\n", | |
"<p>Alec Radford, Karthik Narasimhan, Tim Salimans, and Ilya Sutskever. 2018. Improving language understanding with unsupervised learning. Technical report, OpenAI.</p>\n", | |
"<p>Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, and Percy Liang. 2016. Squad: 100,000+ questions for machine comprehension of text. In Proceedings of the 2016 Conference on Empirical Methods in Natural Language Processing , pages 2383-2392.</p>\n", | |
"<p>Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, and Hannaneh Hajishirzi. 2017. Bidirectional attention flow for machine comprehension. In ICLR .</p>\n", | |
"<p>Richard Socher, Alex Perelygin, Jean Wu, Jason Chuang, Christopher D Manning, Andrew Ng, and Christopher Potts. 2013. Recursive deep models for semantic compositionality over a sentiment treebank. In Proceedings of the 2013 conference on empirical methods in natural language processing , pages 1631-1642.</p>\n", | |
"<p>Fu Sun, Linyang Li, Xipeng Qiu, and Yang Liu. 2018. U-net: Machine reading comprehension with unanswerable questions. arXiv preprint arXiv:1810.06638 .</p>\n", | |
"<p>Wilson L Taylor. 1953. Cloze procedure: A new tool for measuring readability. Journalism Bulletin , 30(4):415-433.</p>\n", | |
"<p>Erik F Tjong Kim Sang and Fien De Meulder. 2003. Introduction to the conll-2003 shared task: Language-independent named entity recognition. In CoNLL .</p>\n", | |
"<p>Joseph Turian, Lev Ratinov, and Yoshua Bengio. 2010. Word representations: A simple and general method for semi-supervised learning. In Proceedings of the 48th Annual Meeting of the Association for Computational Linguistics , ACL '10, pages 384-394.</p>\n", | |
"<p>Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems , pages 6000-6010.</p>\n", | |
"<p>Pascal Vincent, Hugo Larochelle, Yoshua Bengio, and Pierre-Antoine Manzagol. 2008. Extracting and composing robust features with denoising autoencoders. In Proceedings of the 25th international conference on Machine learning , pages 1096-1103. ACM.</p>\n", | |
"<p>Alex Wang, Amanpreet Singh, Julian Michael, Felix Hill, Omer Levy, and Samuel Bowman. 2018a. Glue: A multi-task benchmark and analysis platform</p>\n", | |
"<p>for natural language understanding. In Proceedings of the 2018 EMNLP Workshop BlackboxNLP: Analyzing and Interpreting Neural Networks for NLP , pages 353-355.</p>\n", | |
"<p>Wei Wang, Ming Yan, and Chen Wu. 2018b. Multigranularity hierarchical attention fusion networks for reading comprehension and question answering. In Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) . Association for Computational Linguistics.</p>\n", | |
"<p>Alex Warstadt, Amanpreet Singh, and Samuel R Bowman. 2018. Neural network acceptability judgments. arXiv preprint arXiv:1805.12471 .</p>\n", | |
"<p>Adina Williams, Nikita Nangia, and Samuel R Bowman. 2018. A broad-coverage challenge corpus for sentence understanding through inference. In NAACL .</p>\n", | |
"<p>Yonghui Wu, Mike Schuster, Zhifeng Chen, Quoc V Le, Mohammad Norouzi, Wolfgang Macherey, Maxim Krikun, Yuan Cao, Qin Gao, Klaus Macherey, et al. 2016. Google's neural machine translation system: Bridging the gap between human and machine translation. arXiv preprint arXiv:1609.08144 .</p>\n", | |
"<p>Jason Yosinski, Jeff Clune, Yoshua Bengio, and Hod Lipson. 2014. How transferable are features in deep neural networks? In Advances in neural information processing systems , pages 3320-3328.</p>\n", | |
"<p>Adams Wei Yu, David Dohan, Minh-Thang Luong, Rui Zhao, Kai Chen, Mohammad Norouzi, and Quoc V Le. 2018. QANet: Combining local convolution with global self-attention for reading comprehension. In ICLR .</p>\n", | |
"<p>Rowan Zellers, Yonatan Bisk, Roy Schwartz, and Yejin Choi. 2018. Swag: A large-scale adversarial dataset for grounded commonsense inference. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing (EMNLP) .</p>\n", | |
"<p>Yukun Zhu, Ryan Kiros, Rich Zemel, Ruslan Salakhutdinov, Raquel Urtasun, Antonio Torralba, and Sanja Fidler. 2015. Aligning books and movies: Towards story-like visual explanations by watching movies and reading books. In Proceedings of the IEEE international conference on computer vision , pages 19-27.</p>\n", | |
"<h2>Appendix for \"BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding\"</h2>\n", | |
"<p>We organize the appendix into three sections:</p>\n", | |
"<ul>\n", | |
"<li>· Additional implementation details for BERT are presented in Appendix A;</li>\n", | |
"<li>· Additional details for our experiments are presented in Appendix B; and</li>\n", | |
"<li>· Additional ablation studies are presented in Appendix C.</li>\n", | |
"</ul>\n", | |
"<p>We present additional ablation studies for BERT including:</p>\n", | |
"<ul>\n", | |
"<li>-Effect of Number of Training Steps; and</li>\n", | |
"<li>-Ablation for Different Masking Procedures.</li>\n", | |
"</ul>\n", | |
"<h2>A Additional Details for BERT</h2>\n", | |
"<h2>A.1 Illustration of the Pre-training Tasks</h2>\n", | |
"<p>We provide examples of the pre-training tasks in the following.</p>\n", | |
"<p>Masked LM and the Masking Procedure Assuming the unlabeled sentence is my dog is hairy , and during the random masking procedure we chose the 4-th token (which corresponding to hairy ), our masking procedure can be further illustrated by</p>\n", | |
"<ul>\n", | |
"<li>· 80% of the time: Replace the word with the [MASK] token, e.g., my dog is hairy → my dog is [MASK]</li>\n", | |
"<li>· 10% of the time: Replace the word with a random word, e.g., my dog is hairy → my dog is apple</li>\n", | |
"<li>· 10% of the time: Keep the word unchanged, e.g., my dog is hairy → my dog is hairy . The purpose of this is to bias the representation towards the actual observed word.</li>\n", | |
"</ul>\n", | |
"<p>The advantage of this procedure is that the Transformer encoder does not know which words it will be asked to predict or which have been replaced by random words, so it is forced to keep a distributional contextual representation of every input token. Additionally, because random replacement only occurs for 1.5% of all tokens (i.e., 10% of 15%), this does not seem to harm the model's language understanding capability. In Section C.2, we evaluate the impact this procedure.</p>\n", | |
"<p>Compared to standard langauge model training, the masked LM only make predictions on 15% of tokens in each batch, which suggests that more pre-training steps may be required for the model</p>\n", | |
"<figure></figure>\n", | |
"<figure></figure>\n", | |
"<figure><figcaption>Figure 3: Differences in pre-training model architectures. BERT uses a bidirectional Transformer. OpenAI GPT uses a left-to-right Transformer. ELMo uses the concatenation of independently trained left-to-right and right-toleft LSTMs to generate features for downstream tasks. Among the three, only BERT representations are jointly conditioned on both left and right context in all layers. In addition to the architecture differences, BERT and OpenAI GPT are fine-tuning approaches, while ELMo is a feature-based approach.</figcaption></figure>\n", | |
"<p>to converge. In Section C.1 we demonstrate that MLM does converge marginally slower than a leftto-right model (which predicts every token), but the empirical improvements of the MLM model far outweigh the increased training cost.</p>\n", | |
"<p>Next Sentence Prediction The next sentence prediction task can be illustrated in the following examples.</p>\n", | |
"<p>Input</p>\n", | |
"<p>= [CLS] the man went to [MASK] store [SEP]</p>\n", | |
"<p>he bought a gallon [MASK] milk [SEP]</p>\n", | |
"<p>Label = IsNext</p>\n", | |
"<p>Input = [CLS] the man [MASK] to the store [SEP]</p>\n", | |
"<p>penguin [MASK] are flight ##less birds [SEP]</p>\n", | |
"<p>Label = NotNext</p>\n", | |
"<h2>A.2 Pre-training Procedure</h2>\n", | |
"<p>To generate each training input sequence, we sample two spans of text from the corpus, which we refer to as \"sentences\" even though they are typically much longer than single sentences (but can be shorter also). The first sentence receives the A embedding and the second receives the B embedding. 50% of the time B is the actual next sentence that follows A and 50% of the time it is a random sentence, which is done for the \"next sentence prediction\" task. They are sampled such that the combined length is ≤ 512 tokens. The LM masking is applied after WordPiece tokenization with a uniform masking rate of 15%, and no special consideration given to partial word pieces.</p>\n", | |
"<p>We train with batch size of 256 sequences (256 sequences * 512 tokens = 128,000 tokens/batch) for 1,000,000 steps, which is approximately 40</p>\n", | |
"<p>epochs over the 3.3 billion word corpus. We use Adam with learning rate of 1e-4, β$_{1}$ = 0 . 9 , β$_{2}$ = 0 . 999 , L2 weight decay of 0 . 01 , learning rate warmup over the first 10,000 steps, and linear decay of the learning rate. We use a dropout probability of 0.1 on all layers. We use a gelu activation (Hendrycks and Gimpel, 2016) rather than the standard relu , following OpenAI GPT. The training loss is the sum of the mean masked LM likelihood and the mean next sentence prediction likelihood.</p>\n", | |
"<p>Training of BERT$_{BASE}$ was performed on 4 Cloud TPUs in Pod configuration (16 TPU chips total). 13 Training of BERT$_{LARGE}$ was performed on 16 Cloud TPUs (64 TPU chips total). Each pretraining took 4 days to complete.</p>\n", | |
"<p>Longer sequences are disproportionately expensive because attention is quadratic to the sequence length. To speed up pretraing in our experiments, we pre-train the model with sequence length of 128 for 90% of the steps. Then, we train the rest 10% of the steps of sequence of 512 to learn the positional embeddings.</p>\n", | |
"<h2>A.3 Fine-tuning Procedure</h2>\n", | |
"<p>For fine-tuning, most model hyperparameters are the same as in pre-training, with the exception of the batch size, learning rate, and number of training epochs. The dropout probability was always kept at 0.1. The optimal hyperparameter values are task-specific, but we found the following range of possible values to work well across all tasks:</p>\n", | |
"<p>·</p>\n", | |
"<p>Batch size : 16, 32</p>\n", | |
"<ul>\n", | |
"<li>· Learning rate (Adam) : 5e-5, 3e-5, 2e-5</li>\n", | |
"<li>· Number of epochs : 2, 3, 4</li>\n", | |
"</ul>\n", | |
"<p>We also observed that large data sets (e.g., 100k+ labeled training examples) were far less sensitive to hyperparameter choice than small data sets. Fine-tuning is typically very fast, so it is reasonable to simply run an exhaustive search over the above parameters and choose the model that performs best on the development set.</p>\n", | |
"<h2>A.4 Comparison of BERT, ELMo ,and OpenAI GPT</h2>\n", | |
"<p>Here we studies the differences in recent popular representation learning models including ELMo, OpenAI GPT and BERT. The comparisons between the model architectures are shown visually in Figure 3. Note that in addition to the architecture differences, BERT and OpenAI GPT are finetuning approaches, while ELMo is a feature-based approach.</p>\n", | |
"<p>The most comparable existing pre-training method to BERT is OpenAI GPT, which trains a left-to-right Transformer LM on a large text corpus. In fact, many of the design decisions in BERT were intentionally made to make it as close to GPT as possible so that the two methods could be minimally compared. The core argument of this work is that the bi-directionality and the two pretraining tasks presented in Section 3.1 account for the majority of the empirical improvements, but we do note that there are several other differences between how BERT and GPT were trained:</p>\n", | |
"<ul>\n", | |
"<li>· GPT is trained on the BooksCorpus (800M words); BERT is trained on the BooksCorpus (800M words) and Wikipedia (2,500M words).</li>\n", | |
"<li>· GPT uses a sentence separator ( [SEP] ) and classifier token ( [CLS] ) which are only introduced at fine-tuning time; BERT learns [SEP] , [CLS] and sentence A / B embeddings during pre-training.</li>\n", | |
"<li>· GPT was trained for 1M steps with a batch size of 32,000 words; BERT was trained for 1M steps with a batch size of 128,000 words.</li>\n", | |
"<li>· GPT used the same learning rate of 5e-5 for all fine-tuning experiments; BERT chooses a task-specific fine-tuning learning rate which performs the best on the development set.</li>\n", | |
"</ul>\n", | |
"<p>To isolate the effect of these differences, we perform ablation experiments in Section 5.1 which demonstrate that the majority of the improvements are in fact coming from the two pre-training tasks and the bidirectionality they enable.</p>\n", | |
"<h2>A.5 Illustrations of Fine-tuning on Different Tasks</h2>\n", | |
"<p>The illustration of fine-tuning BERT on different tasks can be seen in Figure 4. Our task-specific models are formed by incorporating BERT with one additional output layer, so a minimal number of parameters need to be learned from scratch. Among the tasks, (a) and (b) are sequence-level tasks while (c) and (d) are token-level tasks. In the figure, E represents the input embedding, T$_{i}$ represents the contextual representation of token i , [CLS] is the special symbol for classification output, and [SEP] is the special symbol to separate non-consecutive token sequences.</p>\n", | |
"<h2>B Detailed Experimental Setup</h2>\n", | |
"<h2>B.1 Detailed Descriptions for the GLUE Benchmark Experiments.</h2>\n", | |
"<p>Our GLUE results in Table1 are obtained from https://gluebenchmark.com/ leaderboard and https://blog. openai.com/language-unsupervised .</p>\n", | |
"<p>The GLUE benchmark includes the following datasets, the descriptions of which were originally summarized in Wang et al. (2018a):</p>\n", | |
"<p>MNLI Multi-Genre Natural Language Inference is a large-scale, crowdsourced entailment classification task (Williams et al., 2018). Given a pair of sentences, the goal is to predict whether the second sentence is an entailment , contradiction , or neutral with respect to the first one.</p>\n", | |
"<p>QQP Quora Question Pairs is a binary classification task where the goal is to determine if two questions asked on Quora are semantically equivalent (Chen et al., 2018).</p>\n", | |
"<p>QNLI Question Natural Language Inference is a version of the Stanford Question Answering Dataset (Rajpurkar et al., 2016) which has been converted to a binary classification task (Wang et al., 2018a). The positive examples are (question, sentence) pairs which do contain the correct answer, and the negative examples are (question, sentence) from the same paragraph which do not contain the answer.</p>\n", | |
"<figure></figure>\n", | |
"<figure></figure>\n", | |
"<figure></figure>\n", | |
"<figure><figcaption>Figure 4: Illustrations of Fine-tuning BERT on Different Tasks.</figcaption></figure>\n", | |
"<p>SST-2 The Stanford Sentiment Treebank is a binary single-sentence classification task consisting of sentences extracted from movie reviews with human annotations of their sentiment (Socher et al., 2013).</p>\n", | |
"<p>CoLA The Corpus of Linguistic Acceptability is a binary single-sentence classification task, where the goal is to predict whether an English sentence is linguistically \"acceptable\" or not (Warstadt et al., 2018).</p>\n", | |
"<p>STS-B The Semantic Textual Similarity Benchmark is a collection of sentence pairs drawn from news headlines and other sources (Cer et al., 2017). They were annotated with a score from 1 to 5 denoting how similar the two sentences are in terms of semantic meaning.</p>\n", | |
"<p>MRPC Microsoft Research Paraphrase Corpus consists of sentence pairs automatically extracted from online news sources, with human annotations</p>\n", | |
"<p>for whether the sentences in the pair are semantically equivalent (Dolan and Brockett, 2005).</p>\n", | |
"<p>RTE Recognizing Textual Entailment is a binary entailment task similar to MNLI, but with much less training data (Bentivogli et al., 2009). 14</p>\n", | |
"<p>WNLI Winograd NLI is a small natural language inference dataset (Levesque et al., 2011). The GLUE webpage notes that there are issues with the construction of this dataset, 15 and every trained system that's been submitted to GLUE has performed worse than the 65.1 baseline accuracy of predicting the majority class. We therefore exclude this set to be fair to OpenAI GPT. For our GLUE submission, we always predicted the ma-</p>\n", | |
"<h2>C Additional Ablation Studies</h2>\n", | |
"<h2>C.1 Effect of Number of Training Steps</h2>\n", | |
"<p>Figure 5 presents MNLI Dev accuracy after finetuning from a checkpoint that has been pre-trained for k steps. This allows us to answer the following questions:</p>\n", | |
"<ul>\n", | |
"<li>1. Question: Does BERT really need such a large amount of pre-training (128,000 words/batch * 1,000,000 steps) to achieve high fine-tuning accuracy?</li>\n", | |
"<li>Answer: Yes, BERT$_{BASE}$ achieves almost 1.0% additional accuracy on MNLI when trained on 1M steps compared to 500k steps.</li>\n", | |
"<li>2. Question: Does MLM pre-training converge slower than LTR pre-training, since only 15% of words are predicted in each batch rather than every word?</li>\n", | |
"<li>Answer: The MLM model does converge slightly slower than the LTR model. However, in terms of absolute accuracy the MLM model begins to outperform the LTR model almost immediately.</li>\n", | |
"</ul>\n", | |
"<h2>C.2 Ablation for Different Masking Procedures</h2>\n", | |
"<p>In Section 3.1, we mention that BERT uses a mixed strategy for masking the target tokens when pre-training with the masked language model (MLM) objective. The following is an ablation study to evaluate the effect of different masking strategies.</p>\n", | |
"<figure><figcaption>Figure 5: Ablation over number of training steps. This shows the MNLI accuracy after fine-tuning, starting from model parameters that have been pre-trained for k steps. The x-axis is the value of k .</figcaption></figure>\n", | |
"<p>Note that the purpose of the masking strategies is to reduce the mismatch between pre-training and fine-tuning, as the [MASK] symbol never appears during the fine-tuning stage. We report the Dev results for both MNLI and NER. For NER, we report both fine-tuning and feature-based approaches, as we expect the mismatch will be amplified for the feature-based approach as the model will not have the chance to adjust the representations.</p>\n", | |
"<table><caption>Table 8: Ablation over different masking strategies.</caption><tbody><tr><th colspan=\"3\">Masking Rates</th><th colspan=\"3\">Dev Set Results</th></tr><tr><th>MASK</th><th>SAME</th><th>RND</th><th>MNLI</th><td></td><th>NER Fine-tune Fine-tune Feature-based</th></tr><tr><td>80%</td><td>10%</td><td>10%</td><td>84.2</td><td>95.4</td><td>94.9</td></tr><tr><td>100%</td><td>0%</td><td>0%</td><td>84.3</td><td>94.9</td><td>94.0</td></tr><tr><td>80%</td><td>0%</td><td>20%</td><td>84.1</td><td>95.2</td><td>94.6</td></tr><tr><td>80%</td><td>20%</td><td>0%</td><td>84.4</td><td>95.2</td><td>94.7</td></tr><tr><td>0%</td><td>20%</td><td>80%</td><td>83.7</td><td>94.8</td><td>94.6</td></tr><tr><td>0%</td><td></td><td>0% 100%</td><td>83.6</td><td>94.9</td><td>94.6</td></tr></tbody></table>\n", | |
"<p>The results are presented in Table 8. In the table, MASK means that we replace the target token with the [MASK] symbol for MLM; SAME means that we keep the target token as is; RND means that we replace the target token with another random token.</p>\n", | |
"<p>The numbers in the left part of the table represent the probabilities of the specific strategies used during MLM pre-training (BERT uses 80%, 10%, 10%). The right part of the paper represents the Dev set results. For the feature-based approach, we concatenate the last 4 layers of BERT as the features, which was shown to be the best approach in Section 5.3.</p>\n", | |
"<p>From the table it can be seen that fine-tuning is surprisingly robust to different masking strategies. However, as expected, using only the MASK strategy was problematic when applying the featurebased approach to NER. Interestingly, using only the RND strategy performs much worse than our strategy as well.</p>\n", | |
"</html></blockquote>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"show(run(docling, \"https://arxiv.org/pdf/1810.04805\"))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Much better with Docling! Docling preserved headers, sections, tables and other formatting. The tradeoff is speed. It took `0.24s` to parse this paper with Apache Tika but `14.56s` with Docling. " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Wrapping up\n", | |
"\n", | |
"This notebook compared the speed and accuracy between Apache Tika and Docling.\n", | |
"\n", | |
"A summary is shown below.\n", | |
"\n", | |
"### Apache Tika\n", | |
"\n", | |
"**Pros**\n", | |
"- In production for over 15 years\n", | |
"- Supports a wide variety of formats (Office documents, PDFs) including older formats like DOC\n", | |
"- Fast for all document types with the tradeoff of formatting accuracy with PDFs\n", | |
"\n", | |
"**Cons**\n", | |
"- Older, new features not as common\n", | |
"- Limited support for PDF formatting, no support for table parsing\n", | |
"- Requires Java\n", | |
"\n", | |
"### Docling\n", | |
"\n", | |
"**Pros**\n", | |
"- New library with a lot of excitement, actively developed\n", | |
"- Supports the important formats (DOCX/XLSX/PDF)\n", | |
"- Impressive formatting accuracy for PDFs (supports tables, formatting, sections)\n", | |
"\n", | |
"**Cons**\n", | |
"- Order of magnitude slower for PDFs\n", | |
"- Doesn't support older office formats (DOC, XLS)\n", | |
"- Accuracy issues for office documents compared to Tika\n", | |
"\n", | |
"Apache Tika is a gritty library that still gets the job done. If PDF formatting isn't all that important and/or you just want text, Tika is the better choice given the speed differences at this time. \n", | |
"\n", | |
"Docling is an extremely promising library but it's still in it's infancy. The trajectory looks like Docling will continue to rapidly get better. There is an [active issue that is working to improve GPU performance](https://github.com/DS4SD/docling-ibm-models/pull/50), which should help with the PDF/vision model speed. Retrieval Augmented Generation (RAG) accuracy is often improved with Markdown formatting. If formatting accuracy is a top priority, then the speed tradeoff is worth it to build better applications!" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "local", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.20" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment