introduction_cn.ipynb 7.2 KB
Notebook
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a14866e7",
   "metadata": {},
   "source": [
    "# BERT base model (uncased)\n",
    "\n",
    "详细内容请看[Bert in PaddleNLP](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/model_zoo/bert/README.md)。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d348c680",
   "metadata": {},
   "source": [
    "Pretrained model on English language using a masked language modeling (MLM) objective. It was introduced in\n",
    "[this paper](https://arxiv.org/abs/1810.04805) and first released in\n",
    "[this repository](https://github.com/google-research/bert). This model is uncased: it does not make a difference\n",
    "between english and English.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a790b40",
   "metadata": {},
   "source": [
    "Disclaimer: The team releasing BERT did not write a model card for this model so this model card has been written by\n",
    "the Hugging Face team.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985d2894",
   "metadata": {},
   "source": [
    "## Model description\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985bd7ee",
   "metadata": {},
   "source": [
    "BERT is a transformers model pretrained on a large corpus of English data in a self-supervised fashion. This means it\n",
    "was pretrained on the raw texts only, with no humans labeling them in any way (which is why it can use lots of\n",
    "publicly available data) with an automatic process to generate inputs and labels from those texts. More precisely, it\n",
    "was pretrained with two objectives:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e1ee5f4",
   "metadata": {},
   "source": [
    "- Masked language modeling (MLM): taking a sentence, the model randomly masks 15% of the words in the input then run\n",
    "the entire masked sentence through the model and has to predict the masked words. This is different from traditional\n",
    "recurrent neural networks (RNNs) that usually see the words one after the other, or from autoregressive models like\n",
    "GPT which internally masks the future tokens. It allows the model to learn a bidirectional representation of the\n",
    "sentence.\n",
    "- Next sentence prediction (NSP): the models concatenates two masked sentences as inputs during pretraining. Sometimes\n",
    "they correspond to sentences that were next to each other in the original text, sometimes not. The model then has to\n",
    "predict if the two sentences were following each other or not.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae584a51",
   "metadata": {},
   "source": [
    "This way, the model learns an inner representation of the English language that can then be used to extract features\n",
    "useful for downstream tasks: if you have a dataset of labeled sentences, for instance, you can train a standard\n",
    "classifier using the features produced by the BERT model as inputs.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4d48848",
   "metadata": {},
   "source": [
    "## Model variations\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcb46068",
   "metadata": {},
   "source": [
    "BERT has originally been released in base and large variations, for cased and uncased input text. The uncased models also strips out an accent markers.\n",
    "Chinese and multilingual uncased and cased versions followed shortly after.\n",
    "Modified preprocessing with whole word masking has replaced subpiece masking in a following work, with the release of two models.\n",
    "Other 24 smaller models are released afterward.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdf3ec7e",
   "metadata": {},
   "source": [
    "The detailed release history can be found on the [google-research/bert readme](https://github.com/google-research/bert/blob/master/README.md) on github.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d66e6fc4",
   "metadata": {},
   "source": [
    "| Model | #params | Language |\n",
    "|------------------------|--------------------------------|-------|\n",
    "| [`bert-base-uncased`](https://huggingface.co/bert-base-uncased) | 110M   | English |\n",
    "| [`bert-large-uncased`](https://huggingface.co/bert-large-uncased)              | 340M    | English | sub\n",
    "| [`bert-base-cased`](https://huggingface.co/bert-base-cased)        | 110M    | English |\n",
    "| [`bert-large-cased`](https://huggingface.co/bert-large-cased) | 340M    |  English |\n",
    "| [`bert-base-chinese`](https://huggingface.co/bert-base-chinese) | 110M    | Chinese |\n",
    "| [`bert-base-multilingual-cased`](https://huggingface.co/bert-base-multilingual-cased) | 110M | Multiple |\n",
    "| [`bert-large-uncased-whole-word-masking`](https://huggingface.co/bert-large-uncased-whole-word-masking) | 340M | English |\n",
    "| [`bert-large-cased-whole-word-masking`](https://huggingface.co/bert-large-cased-whole-word-masking) | 340M | English |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93c97712",
   "metadata": {},
   "source": [
    "## How to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4daab88",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --upgrade paddlenlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09dec4f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "from paddlenlp.transformers import AutoModel\n",
    "\n",
    "model = AutoModel.from_pretrained(\"bert-base-uncased\")\n",
    "input_ids = paddle.randint(100, 200, shape=[1, 20])\n",
    "print(model(input_ids))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85541d34",
   "metadata": {},
   "source": [
    "```\n",
    "@article{DBLP:journals/corr/abs-1810-04805,\n",
    "author    = {Jacob Devlin and\n",
    "Ming{-}Wei Chang and\n",
    "Kenton Lee and\n",
    "Kristina Toutanova},\n",
    "title     = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language\n",
    "Understanding},\n",
    "journal   = {CoRR},\n",
    "volume    = {abs/1810.04805},\n",
    "year      = {2018},\n",
    "url       = {http://arxiv.org/abs/1810.04805},\n",
    "archivePrefix = {arXiv},\n",
    "eprint    = {1810.04805},\n",
    "timestamp = {Tue, 30 Oct 2018 20:39:56 +0100},\n",
    "biburl    = {https://dblp.org/rec/journals/corr/abs-1810-04805.bib},\n",
    "bibsource = {dblp computer science bibliography, https://dblp.org}\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82898490",
   "metadata": {},
   "source": [
    "<a href=\"https://huggingface.co/exbert/?model=bert-base-uncased\">\n",
    "<img width=\"300px\" src=\"https://cdn-media.huggingface.co/exbert/button.png\">\n",
    "</a>\n",
    "\n",
    "> 此模型介绍及权重来源于 https://huggingface.co/bert-base-uncased ,并转换为飞桨模型格式。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}