introduction_cn.ipynb 4.5 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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a4cd103f",
   "metadata": {},
   "source": [
    "# GPT-2\n",
    "\n",
    "详细内容请看[GPT2 in PaddleNLP](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/model_zoo/gpt/README.md)。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e10dfe6d",
   "metadata": {},
   "source": [
    "Pretrained model on English language using a causal language modeling (CLM) objective. It was introduced in\n",
    "[this paper](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)\n",
    "and first released at [this page](https://openai.com/blog/better-language-models/).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1b13043",
   "metadata": {},
   "source": [
    "Disclaimer: The team releasing GPT-2 also wrote a\n",
    "[model card](https://github.com/openai/gpt-2/blob/master/model_card.md) for their model. Content from this model card\n",
    "has been written by the Hugging Face team to complete the information they provided and give specific examples of bias.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "016271a5",
   "metadata": {},
   "source": [
    "## Model description\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a53155",
   "metadata": {},
   "source": [
    "GPT-2 is a transformers model pretrained on a very large corpus of English data in a self-supervised fashion. This\n",
    "means it was pretrained on the raw texts only, with no humans labelling them in any way (which is why it can use lots\n",
    "of publicly available data) with an automatic process to generate inputs and labels from those texts. More precisely,\n",
    "it was trained to guess the next word in sentences.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6836ad17",
   "metadata": {},
   "source": [
    "More precisely, inputs are sequences of continuous text of a certain length and the targets are the same sequence,\n",
    "shifted one token (word or piece of word) to the right. The model uses internally a mask-mechanism to make sure the\n",
    "predictions for the token `i` only uses the inputs from `1` to `i` but not the future tokens.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26946ce6",
   "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. The model is best at what it was pretrained for however, which is generating texts from a\n",
    "prompt.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "571b41cf",
   "metadata": {},
   "source": [
    "## How to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6233e8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --upgrade paddlenlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e906136",
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "from paddlenlp.transformers import AutoModel\n",
    "\n",
    "model = AutoModel.from_pretrained(\"gpt2\")\n",
    "input_ids = paddle.randint(100, 200, shape=[1, 20])\n",
    "print(model(input_ids))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78f26b7f",
   "metadata": {},
   "source": [
    "## Citation\n",
    "\n",
    "```\n",
    "@article{radford2019language,\n",
    "title={Language Models are Unsupervised Multitask Learners},\n",
    "author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},\n",
    "year={2019}\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f646c57",
   "metadata": {},
   "source": [
    "<a href=\"https://huggingface.co/exbert/?model=gpt2\">\n",
    "<img width=\"300px\" src=\"https://cdn-media.huggingface.co/exbert/button.png\">\n",
    "</a>\n",
    "\n",
    "> 此模型介绍及权重来源于[https://huggingface.co/gpt2](https://huggingface.co/gpt2),并转换为飞桨模型格式。\n"
   ]
  }
 ],
 "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"
  },
  "vscode": {
   "interpreter": {
    "hash": "606ea184b8fed3419d714b545dc1784fad6c99d0cc940b6b9d787dccf225faa5"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}