introduction_en.ipynb 4.7 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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d373572",
   "metadata": {},
   "source": [
    "# GPT-2\n",
    "\n",
    "You can get more details from [GPT2 in PaddleNLP](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/model_zoo/gpt/README.md)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00be5831",
   "metadata": {},
   "source": [
    "Test the whole generation capabilities here: https://transformer.huggingface.co/doc/gpt2-large\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5857cc2",
   "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": "b0abac76",
   "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": "fa2c7f4b",
   "metadata": {},
   "source": [
    "## Model description\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "294521bd",
   "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": "b1204c32",
   "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": "a019cc9e",
   "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": "54ae8500",
   "metadata": {},
   "source": [
    "## How to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d33fddda",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --upgrade paddlenlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0e160c6",
   "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": "fcb8a843",
   "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": "513848f8",
   "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",
    "\n",
    "> The model introduction and model weights originate from [https://huggingface.co/gpt2](https://huggingface.co/gpt2) and were converted to PaddlePaddle format for ease of use in PaddleNLP.\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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}