README.md
| 1 | --- |
| 2 | language: en |
| 3 | license: apache-2.0 |
| 4 | datasets: |
| 5 | - bookcorpus |
| 6 | - wikipedia |
| 7 | --- |
| 8 | |
| 9 | # BERT large model (uncased) |
| 10 | |
| 11 | Pretrained model on English language using a masked language modeling (MLM) objective. It was introduced in |
| 12 | [this paper](https://arxiv.org/abs/1810.04805) and first released in |
| 13 | [this repository](https://github.com/google-research/bert). This model is uncased: it does not make a difference |
| 14 | between english and English. |
| 15 | |
| 16 | Disclaimer: The team releasing BERT did not write a model card for this model so this model card has been written by |
| 17 | the Hugging Face team. |
| 18 | |
| 19 | ## Model description |
| 20 | |
| 21 | BERT is a transformers model pretrained on a large corpus of English data in a self-supervised fashion. This means it |
| 22 | was pretrained on the raw texts only, with no humans labelling them in any way (which is why it can use lots of |
| 23 | publicly available data) with an automatic process to generate inputs and labels from those texts. More precisely, it |
| 24 | was pretrained with two objectives: |
| 25 | |
| 26 | - Masked language modeling (MLM): taking a sentence, the model randomly masks 15% of the words in the input then run |
| 27 | the entire masked sentence through the model and has to predict the masked words. This is different from traditional |
| 28 | recurrent neural networks (RNNs) that usually see the words one after the other, or from autoregressive models like |
| 29 | GPT which internally mask the future tokens. It allows the model to learn a bidirectional representation of the |
| 30 | sentence. |
| 31 | - Next sentence prediction (NSP): the models concatenates two masked sentences as inputs during pretraining. Sometimes |
| 32 | they correspond to sentences that were next to each other in the original text, sometimes not. The model then has to |
| 33 | predict if the two sentences were following each other or not. |
| 34 | |
| 35 | This way, the model learns an inner representation of the English language that can then be used to extract features |
| 36 | useful for downstream tasks: if you have a dataset of labeled sentences for instance, you can train a standard |
| 37 | classifier using the features produced by the BERT model as inputs. |
| 38 | |
| 39 | This model has the following configuration: |
| 40 | |
| 41 | - 24-layer |
| 42 | - 1024 hidden dimension |
| 43 | - 16 attention heads |
| 44 | - 336M parameters. |
| 45 | |
| 46 | |
| 47 | ## Intended uses & limitations |
| 48 | |
| 49 | You can use the raw model for either masked language modeling or next sentence prediction, but it's mostly intended to |
| 50 | be fine-tuned on a downstream task. See the [model hub](https://huggingface.co/models?filter=bert) to look for |
| 51 | fine-tuned versions on a task that interests you. |
| 52 | |
| 53 | Note that this model is primarily aimed at being fine-tuned on tasks that use the whole sentence (potentially masked) |
| 54 | to make decisions, such as sequence classification, token classification or question answering. For tasks such as text |
| 55 | generation you should look at model like GPT2. |
| 56 | |
| 57 | ### How to use |
| 58 | |
| 59 | You can use this model directly with a pipeline for masked language modeling: |
| 60 | |
| 61 | ```python |
| 62 | >>> from transformers import pipeline |
| 63 | >>> unmasker = pipeline('fill-mask', model='bert-large-uncased') |
| 64 | >>> unmasker("Hello I'm a [MASK] model.") |
| 65 | [{'sequence': "[CLS] hello i'm a fashion model. [SEP]", |
| 66 | 'score': 0.1886913776397705, |
| 67 | 'token': 4827, |
| 68 | 'token_str': 'fashion'}, |
| 69 | {'sequence': "[CLS] hello i'm a professional model. [SEP]", |
| 70 | 'score': 0.07157472521066666, |
| 71 | 'token': 2658, |
| 72 | 'token_str': 'professional'}, |
| 73 | {'sequence': "[CLS] hello i'm a male model. [SEP]", |
| 74 | 'score': 0.04053466394543648, |
| 75 | 'token': 3287, |
| 76 | 'token_str': 'male'}, |
| 77 | {'sequence': "[CLS] hello i'm a role model. [SEP]", |
| 78 | 'score': 0.03891477733850479, |
| 79 | 'token': 2535, |
| 80 | 'token_str': 'role'}, |
| 81 | {'sequence': "[CLS] hello i'm a fitness model. [SEP]", |
| 82 | 'score': 0.03038121573626995, |
| 83 | 'token': 10516, |
| 84 | 'token_str': 'fitness'}] |
| 85 | ``` |
| 86 | |
| 87 | Here is how to use this model to get the features of a given text in PyTorch: |
| 88 | |
| 89 | ```python |
| 90 | from transformers import BertTokenizer, BertModel |
| 91 | tokenizer = BertTokenizer.from_pretrained('bert-large-uncased') |
| 92 | model = BertModel.from_pretrained("bert-large-uncased") |
| 93 | text = "Replace me by any text you'd like." |
| 94 | encoded_input = tokenizer(text, return_tensors='pt') |
| 95 | output = model(**encoded_input) |
| 96 | ``` |
| 97 | |
| 98 | and in TensorFlow: |
| 99 | |
| 100 | ```python |
| 101 | from transformers import BertTokenizer, TFBertModel |
| 102 | tokenizer = BertTokenizer.from_pretrained('bert-large-uncased') |
| 103 | model = TFBertModel.from_pretrained("bert-large-uncased") |
| 104 | text = "Replace me by any text you'd like." |
| 105 | encoded_input = tokenizer(text, return_tensors='tf') |
| 106 | output = model(encoded_input) |
| 107 | ``` |
| 108 | |
| 109 | ### Limitations and bias |
| 110 | |
| 111 | Even if the training data used for this model could be characterized as fairly neutral, this model can have biased |
| 112 | predictions: |
| 113 | |
| 114 | ```python |
| 115 | >>> from transformers import pipeline |
| 116 | >>> unmasker = pipeline('fill-mask', model='bert-large-uncased') |
| 117 | >>> unmasker("The man worked as a [MASK].") |
| 118 | |
| 119 | [{'sequence': '[CLS] the man worked as a bartender. [SEP]', |
| 120 | 'score': 0.10426565259695053, |
| 121 | 'token': 15812, |
| 122 | 'token_str': 'bartender'}, |
| 123 | {'sequence': '[CLS] the man worked as a waiter. [SEP]', |
| 124 | 'score': 0.10232779383659363, |
| 125 | 'token': 15610, |
| 126 | 'token_str': 'waiter'}, |
| 127 | {'sequence': '[CLS] the man worked as a mechanic. [SEP]', |
| 128 | 'score': 0.06281787157058716, |
| 129 | 'token': 15893, |
| 130 | 'token_str': 'mechanic'}, |
| 131 | {'sequence': '[CLS] the man worked as a lawyer. [SEP]', |
| 132 | 'score': 0.050936125218868256, |
| 133 | 'token': 5160, |
| 134 | 'token_str': 'lawyer'}, |
| 135 | {'sequence': '[CLS] the man worked as a carpenter. [SEP]', |
| 136 | 'score': 0.041034240275621414, |
| 137 | 'token': 10533, |
| 138 | 'token_str': 'carpenter'}] |
| 139 | |
| 140 | >>> unmasker("The woman worked as a [MASK].") |
| 141 | |
| 142 | [{'sequence': '[CLS] the woman worked as a waitress. [SEP]', |
| 143 | 'score': 0.28473711013793945, |
| 144 | 'token': 13877, |
| 145 | 'token_str': 'waitress'}, |
| 146 | {'sequence': '[CLS] the woman worked as a nurse. [SEP]', |
| 147 | 'score': 0.11336520314216614, |
| 148 | 'token': 6821, |
| 149 | 'token_str': 'nurse'}, |
| 150 | {'sequence': '[CLS] the woman worked as a bartender. [SEP]', |
| 151 | 'score': 0.09574324637651443, |
| 152 | 'token': 15812, |
| 153 | 'token_str': 'bartender'}, |
| 154 | {'sequence': '[CLS] the woman worked as a maid. [SEP]', |
| 155 | 'score': 0.06351090222597122, |
| 156 | 'token': 10850, |
| 157 | 'token_str': 'maid'}, |
| 158 | {'sequence': '[CLS] the woman worked as a secretary. [SEP]', |
| 159 | 'score': 0.048970773816108704, |
| 160 | 'token': 3187, |
| 161 | 'token_str': 'secretary'}] |
| 162 | ``` |
| 163 | |
| 164 | This bias will also affect all fine-tuned versions of this model. |
| 165 | |
| 166 | ## Training data |
| 167 | |
| 168 | The BERT model was pretrained on [BookCorpus](https://yknzhu.wixsite.com/mbweb), a dataset consisting of 11,038 |
| 169 | unpublished books and [English Wikipedia](https://en.wikipedia.org/wiki/English_Wikipedia) (excluding lists, tables and |
| 170 | headers). |
| 171 | |
| 172 | ## Training procedure |
| 173 | |
| 174 | ### Preprocessing |
| 175 | |
| 176 | The texts are lowercased and tokenized using WordPiece and a vocabulary size of 30,000. The inputs of the model are |
| 177 | then of the form: |
| 178 | |
| 179 | ``` |
| 180 | [CLS] Sentence A [SEP] Sentence B [SEP] |
| 181 | ``` |
| 182 | |
| 183 | With probability 0.5, sentence A and sentence B correspond to two consecutive sentences in the original corpus and in |
| 184 | the other cases, it's another random sentence in the corpus. Note that what is considered a sentence here is a |
| 185 | consecutive span of text usually longer than a single sentence. The only constrain is that the result with the two |
| 186 | "sentences" has a combined length of less than 512 tokens. |
| 187 | |
| 188 | The details of the masking procedure for each sentence are the following: |
| 189 | - 15% of the tokens are masked. |
| 190 | - In 80% of the cases, the masked tokens are replaced by `[MASK]`. |
| 191 | - In 10% of the cases, the masked tokens are replaced by a random token (different) from the one they replace. |
| 192 | - In the 10% remaining cases, the masked tokens are left as is. |
| 193 | |
| 194 | ### Pretraining |
| 195 | |
| 196 | The model was trained on 4 cloud TPUs in Pod configuration (16 TPU chips total) for one million steps with a batch size |
| 197 | of 256. The sequence length was limited to 128 tokens for 90% of the steps and 512 for the remaining 10%. The optimizer |
| 198 | used is Adam with a learning rate of 1e-4, \\(\beta_{1} = 0.9\\) and \\(\beta_{2} = 0.999\\), a weight decay of 0.01, |
| 199 | learning rate warmup for 10,000 steps and linear decay of the learning rate after. |
| 200 | |
| 201 | ## Evaluation results |
| 202 | |
| 203 | When fine-tuned on downstream tasks, this model achieves the following results: |
| 204 | |
| 205 | Model | SQUAD 1.1 F1/EM | Multi NLI Accuracy |
| 206 | ---------------------------------------- | :-------------: | :----------------: |
| 207 | BERT-Large, Uncased (Original) | 91.0/84.3 | 86.05 |
| 208 | |
| 209 | ### BibTeX entry and citation info |
| 210 | |
| 211 | ```bibtex |
| 212 | @article{DBLP:journals/corr/abs-1810-04805, |
| 213 | author = {Jacob Devlin and |
| 214 | Ming{-}Wei Chang and |
| 215 | Kenton Lee and |
| 216 | Kristina Toutanova}, |
| 217 | title = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language |
| 218 | Understanding}, |
| 219 | journal = {CoRR}, |
| 220 | volume = {abs/1810.04805}, |
| 221 | year = {2018}, |
| 222 | url = {http://arxiv.org/abs/1810.04805}, |
| 223 | archivePrefix = {arXiv}, |
| 224 | eprint = {1810.04805}, |
| 225 | timestamp = {Tue, 30 Oct 2018 20:39:56 +0100}, |
| 226 | biburl = {https://dblp.org/rec/journals/corr/abs-1810-04805.bib}, |
| 227 | bibsource = {dblp computer science bibliography, https://dblp.org} |
| 228 | } |
| 229 | ``` |