Team Ai
Modelpublic

google-bert/bert-large-uncased

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
148likes946kdownloads
README.md229 linesDownload Raw Back to root
1---2language: en3license: apache-2.04datasets:5- bookcorpus6- wikipedia7---8 9# BERT large model (uncased)10 11Pretrained model on English language using a masked language modeling (MLM) objective. It was introduced in12[this paper](https://arxiv.org/abs/1810.04805) and first released in13[this repository](https://github.com/google-research/bert). This model is uncased: it does not make a difference14between english and English.15 16Disclaimer: The team releasing BERT did not write a model card for this model so this model card has been written by17the Hugging Face team.18 19## Model description20 21BERT is a transformers model pretrained on a large corpus of English data in a self-supervised fashion. This means it22was pretrained on the raw texts only, with no humans labelling them in any way (which is why it can use lots of23publicly available data) with an automatic process to generate inputs and labels from those texts. More precisely, it24was 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 run27  the entire masked sentence through the model and has to predict the masked words. This is different from traditional28  recurrent neural networks (RNNs) that usually see the words one after the other, or from autoregressive models like29  GPT which internally mask the future tokens. It allows the model to learn a bidirectional representation of the30  sentence.31- Next sentence prediction (NSP): the models concatenates two masked sentences as inputs during pretraining. Sometimes32  they correspond to sentences that were next to each other in the original text, sometimes not. The model then has to33  predict if the two sentences were following each other or not.34 35This way, the model learns an inner representation of the English language that can then be used to extract features36useful for downstream tasks: if you have a dataset of labeled sentences for instance, you can train a standard37classifier using the features produced by the BERT model as inputs.38 39This model has the following configuration:40 41- 24-layer42- 1024 hidden dimension43- 16 attention heads44- 336M parameters.45 46 47## Intended uses & limitations48 49You can use the raw model for either masked language modeling or next sentence prediction, but it's mostly intended to50be fine-tuned on a downstream task. See the [model hub](https://huggingface.co/models?filter=bert) to look for51fine-tuned versions on a task that interests you.52 53Note that this model is primarily aimed at being fine-tuned on tasks that use the whole sentence (potentially masked)54to make decisions, such as sequence classification, token classification or question answering. For tasks such as text55generation you should look at model like GPT2.56 57### How to use58 59You can use this model directly with a pipeline for masked language modeling:60 61```python62>>> from transformers import pipeline63>>> 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 87Here is how to use this model to get the features of a given text in PyTorch:88 89```python90from transformers import BertTokenizer, BertModel91tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')92model = BertModel.from_pretrained("bert-large-uncased")93text = "Replace me by any text you'd like."94encoded_input = tokenizer(text, return_tensors='pt')95output = model(**encoded_input)96```97 98and in TensorFlow:99 100```python101from transformers import BertTokenizer, TFBertModel102tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')103model = TFBertModel.from_pretrained("bert-large-uncased")104text = "Replace me by any text you'd like."105encoded_input = tokenizer(text, return_tensors='tf')106output = model(encoded_input)107```108 109### Limitations and bias110 111Even if the training data used for this model could be characterized as fairly neutral, this model can have biased112predictions:113 114```python115>>> from transformers import pipeline116>>> 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 164This bias will also affect all fine-tuned versions of this model.165 166## Training data167 168The BERT model was pretrained on [BookCorpus](https://yknzhu.wixsite.com/mbweb), a dataset consisting of 11,038169unpublished books and [English Wikipedia](https://en.wikipedia.org/wiki/English_Wikipedia) (excluding lists, tables and170headers).171 172## Training procedure173 174### Preprocessing175 176The texts are lowercased and tokenized using WordPiece and a vocabulary size of 30,000. The inputs of the model are177then of the form:178 179```180[CLS] Sentence A [SEP] Sentence B [SEP]181```182 183With probability 0.5, sentence A and sentence B correspond to two consecutive sentences in the original corpus and in184the other cases, it's another random sentence in the corpus. Note that what is considered a sentence here is a185consecutive span of text usually longer than a single sentence. The only constrain is that the result with the two186"sentences" has a combined length of less than 512 tokens.187 188The 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### Pretraining195 196The model was trained on 4 cloud TPUs in Pod configuration (16 TPU chips total) for one million steps with a batch size197of 256. The sequence length was limited to 128 tokens for 90% of the steps and 512 for the remaining 10%. The optimizer198used is Adam with a learning rate of 1e-4, \\(\beta_{1} = 0.9\\) and \\(\beta_{2} = 0.999\\), a weight decay of 0.01,199learning rate warmup for 10,000 steps and linear decay of the learning rate after.200 201## Evaluation results202 203When fine-tuned on downstream tasks, this model achieves the following results:204 205Model                                    | SQUAD 1.1 F1/EM | Multi NLI Accuracy206---------------------------------------- | :-------------: | :----------------:207BERT-Large, Uncased (Original)           | 91.0/84.3       | 86.05208 209### BibTeX entry and citation info210 211```bibtex212@article{DBLP:journals/corr/abs-1810-04805,213  author    = {Jacob Devlin and214               Ming{-}Wei Chang and215               Kenton Lee and216               Kristina Toutanova},217  title     = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language218               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```