{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-1 fw=all -->\n",
    "\n",
    "# The Dataset for Pretraining Word Embeddings\n",
    "\n",
    "Now that we know the technical details of \n",
    "the word2vec models and approximate training methods,\n",
    "let's walk through their implementations. \n",
    "Specifically,\n",
    "we will take the skip-gram model in that section\n",
    "and negative sampling in that section\n",
    "as an example.\n",
    "In this section,\n",
    "we begin with the dataset\n",
    "for pretraining the word embedding model:\n",
    "the original format of the data\n",
    "will be transformed\n",
    "into minibatches\n",
    "that can be iterated over during training."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-the-dataset-for-pretraining-word-embeddings"
   },
   "source": [
    "import collections\n",
    "from d2l import mxnet as d2l\n",
    "import math\n",
    "from mxnet import gluon, np\n",
    "import os\n",
    "import random"
   ],
   "execution_count": 1,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-2 fw=all -->\n",
    "\n",
    "## Reading the Dataset\n",
    "\n",
    "The dataset that we use here\n",
    "is [Penn Tree Bank (PTB)]( https://catalog.ldc.upenn.edu/LDC99T42). \n",
    "This corpus is sampled\n",
    "from Wall Street Journal articles,\n",
    "split into training, validation, and test sets.\n",
    "In the original format,\n",
    "each line of the text file\n",
    "represents a sentence of words that are separated by spaces.\n",
    "Here we treat each word as a token."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-reading-the-dataset-1"
   },
   "source": [
    "\n",
    "d2l.DATA_HUB['ptb'] = (d2l.DATA_URL + 'ptb.zip',\n",
    "                       '319d85e578af0cdc590547f26231e4e31cdf1e42')\n",
    "\n",
    "\n",
    "def read_ptb():\n",
    "    \"\"\"Load the PTB dataset into a list of text lines.\"\"\"\n",
    "    data_dir = d2l.download_extract('ptb')\n",
    "    # Read the training set\n",
    "    with open(os.path.join(data_dir, 'ptb.train.txt')) as f:\n",
    "        raw_text = f.read()\n",
    "    return [line.split() for line in raw_text.split('\\n')]\n",
    "\n",
    "sentences = read_ptb()\n",
    "f'# sentences: {len(sentences)}'"
   ],
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'# sentences: 42069'"
     },
     "metadata": {},
     "execution_count": 2
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-3 fw=all -->\n",
    "\n",
    "After reading the training set,\n",
    "we build a vocabulary for the corpus,\n",
    "where any word that appears \n",
    "less than 10 times is replaced by \n",
    "the \"&lt;unk&gt;\" token.\n",
    "Note that the original dataset\n",
    "also contains \"&lt;unk&gt;\" tokens that represent rare (unknown) words."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-reading-the-dataset-2"
   },
   "source": [
    "vocab = d2l.Vocab(sentences, min_freq=10)\n",
    "f'vocab size: {len(vocab)}'"
   ],
   "execution_count": 3,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'vocab size: 6720'"
     },
     "metadata": {},
     "execution_count": 3
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-4 fw=all -->\n",
    "\n",
    "## Subsampling\n",
    "\n",
    "Text data\n",
    "typically have high-frequency words\n",
    "such as \"the\", \"a\", and \"in\":\n",
    "they may even occur billions of times in\n",
    "very large corpora.\n",
    "However,\n",
    "these words often co-occur\n",
    "with many different words in\n",
    "context windows, providing little useful signals.\n",
    "For instance,\n",
    "consider the word \"chip\" in a context window:\n",
    "intuitively\n",
    "its co-occurrence with a low-frequency word \"intel\"\n",
    "is more useful in training\n",
    "than \n",
    "the co-occurrence with a high-frequency word \"a\".\n",
    "Moreover, training with vast amounts of (high-frequency) words\n",
    "is slow.\n",
    "Thus, when training word embedding models, \n",
    "high-frequency words can be *subsampled* [@Mikolov.Sutskever.Chen.ea.2013].\n",
    "Specifically, \n",
    "each indexed word $w_i$ \n",
    "in the dataset will be discarded with probability\n",
    "\n",
    "\n",
    "$$ P(w_i) = \\max\\left(1 - \\sqrt{\\frac{t}{f(w_i)}}, 0\\right),$$\n",
    "\n",
    "where $f(w_i)$ is the ratio of \n",
    "the number of words $w_i$\n",
    "to the total number of words in the dataset, \n",
    "and the constant $t$ is a hyperparameter\n",
    "($10^{-4}$ in the experiment). \n",
    "We can see that only when\n",
    "the relative frequency\n",
    "$f(w_i) > t$  can the (high-frequency) word $w_i$ be discarded, \n",
    "and the higher the relative frequency of the word, \n",
    "the greater the probability of being discarded."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-subsampling-1"
   },
   "source": [
    "\n",
    "def subsample(sentences, vocab):\n",
    "    \"\"\"Subsample high-frequency words.\"\"\"\n",
    "    # Exclude unknown tokens ('<unk>')\n",
    "    sentences = [[token for token in line if vocab[token] != vocab.unk]\n",
    "                 for line in sentences]\n",
    "    counter = collections.Counter([\n",
    "        token for line in sentences for token in line])\n",
    "    num_tokens = sum(counter.values())\n",
    "\n",
    "    # Return True if `token` is kept during subsampling\n",
    "    def keep(token):\n",
    "        return(random.uniform(0, 1) <\n",
    "               math.sqrt(1e-4 / counter[token] * num_tokens))\n",
    "\n",
    "    return ([[token for token in line if keep(token)] for line in sentences],\n",
    "            counter)\n",
    "\n",
    "subsampled, counter = subsample(sentences, vocab)"
   ],
   "execution_count": 4,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-5 fw=all -->\n",
    "\n",
    "The following code snippet \n",
    "plots the histogram of\n",
    "the number of tokens per sentence\n",
    "before and after subsampling.\n",
    "As expected, \n",
    "subsampling significantly shortens sentences\n",
    "by dropping high-frequency words,\n",
    "which will lead to training speedup."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-subsampling-2"
   },
   "source": [
    "d2l.show_list_len_pair_hist(['origin', 'subsampled'], '# tokens per sentence',\n",
    "                            'count', sentences, subsampled);"
   ],
   "execution_count": 5,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"262.190625pt\" height=\"185.501977pt\" viewBox=\"0 0 262.190625 185.501977\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T14:03:59.382452</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 185.501977 \nL 262.190625 185.501977 \nL 262.190625 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 59.690625 147.945727 \nL 254.990625 147.945727 \nL 254.990625 9.345727 \nL 59.690625 9.345727 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 68.567898 147.945727 \nL 75.814651 147.945727 \nL 75.814651 126.22601 \nL 68.567898 126.22601 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 86.684781 147.945727 \nL 93.931534 147.945727 \nL 93.931534 87.727608 \nL 86.684781 87.727608 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 104.801664 147.945727 \nL 112.048417 147.945727 \nL 112.048417 78.376901 \nL 104.801664 78.376901 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 122.918547 147.945727 \nL 130.1653 147.945727 \nL 130.1653 98.699762 \nL 122.918547 98.699762 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 141.03543 147.945727 \nL 148.282183 147.945727 \nL 148.282183 127.414706 \nL 141.03543 127.414706 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 159.152313 147.945727 \nL 166.399067 147.945727 \nL 166.399067 140.97241 \nL 159.152313 140.97241 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 177.269196 147.945727 \nL 184.51595 147.945727 \nL 184.51595 146.368103 \nL 177.269196 146.368103 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 195.38608 147.945727 \nL 202.632833 147.945727 \nL 202.632833 147.545843 \nL 195.38608 147.545843 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 213.502963 147.945727 \nL 220.749716 147.945727 \nL 220.749716 147.80878 \nL 213.502963 147.80878 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 231.619846 147.945727 \nL 238.866599 147.945727 \nL 238.866599 147.869037 \nL 231.619846 147.869037 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 75.814651 147.945727 \nL 83.061404 147.945727 \nL 83.061404 15.945727 \nL 75.814651 15.945727 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 93.931534 147.945727 \nL 101.178287 147.945727 \nL 101.178287 60.228749 \nL 93.931534 60.228749 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 112.048417 147.945727 \nL 119.29517 147.945727 \nL 119.29517 137.828119 \nL 112.048417 137.828119 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 130.1653 147.945727 \nL 137.412054 147.945727 \nL 137.412054 147.376029 \nL 130.1653 147.376029 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_17\">\n    <path d=\"M 148.282183 147.945727 \nL 155.528937 147.945727 \nL 155.528937 147.901904 \nL 148.282183 147.901904 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_18\">\n    <path d=\"M 166.399067 147.945727 \nL 173.64582 147.945727 \nL 173.64582 147.945727 \nL 166.399067 147.945727 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_19\">\n    <path d=\"M 184.51595 147.945727 \nL 191.762703 147.945727 \nL 191.762703 147.945727 \nL 184.51595 147.945727 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_20\">\n    <path d=\"M 202.632833 147.945727 \nL 209.879586 147.945727 \nL 209.879586 147.945727 \nL 202.632833 147.945727 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_21\">\n    <path d=\"M 220.749716 147.945727 \nL 227.996469 147.945727 \nL 227.996469 147.945727 \nL 220.749716 147.945727 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"patch_22\">\n    <path d=\"M 238.866599 147.945727 \nL 246.113352 147.945727 \nL 246.113352 147.945727 \nL 238.866599 147.945727 \nz\n\" clip-path=\"url(#pbe662095c5)\" style=\"fill: url(#h27bc278b12)\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"m9ae7691896\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m9ae7691896\" x=\"66.756209\" y=\"147.945727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(63.574959 162.544164) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#m9ae7691896\" x=\"110.943729\" y=\"147.945727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 20 -->\n      <g transform=\"translate(104.581229 162.544164) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#m9ae7691896\" x=\"155.131249\" y=\"147.945727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 40 -->\n      <g transform=\"translate(148.768749 162.544164) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m9ae7691896\" x=\"199.318769\" y=\"147.945727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 60 -->\n      <g transform=\"translate(192.956269 162.544164) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#m9ae7691896\" x=\"243.506289\" y=\"147.945727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 80 -->\n      <g transform=\"translate(237.143789 162.544164) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_6\">\n     <!-- # tokens per sentence -->\n     <g transform=\"translate(100.6125 176.222289) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-23\" d=\"M 3272 2816 \nL 2363 2816 \nL 2100 1772 \nL 3016 1772 \nL 3272 2816 \nz\nM 2803 4594 \nL 2478 3297 \nL 3391 3297 \nL 3719 4594 \nL 4219 4594 \nL 3897 3297 \nL 4872 3297 \nL 4872 2816 \nL 3775 2816 \nL 3519 1772 \nL 4513 1772 \nL 4513 1294 \nL 3397 1294 \nL 3072 0 \nL 2572 0 \nL 2894 1294 \nL 1978 1294 \nL 1656 0 \nL 1153 0 \nL 1478 1294 \nL 494 1294 \nL 494 1772 \nL 1594 1772 \nL 1856 2816 \nL 850 2816 \nL 850 3297 \nL 1978 3297 \nL 2297 4594 \nL 2803 4594 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6b\" d=\"M 581 4863 \nL 1159 4863 \nL 1159 1991 \nL 2875 3500 \nL 3609 3500 \nL 1753 1863 \nL 3688 0 \nL 2938 0 \nL 1159 1709 \nL 1159 0 \nL 581 0 \nL 581 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \nL 2834 2853 \nQ 2591 2978 2328 3040 \nQ 2066 3103 1784 3103 \nQ 1356 3103 1142 2972 \nQ 928 2841 928 2578 \nQ 928 2378 1081 2264 \nQ 1234 2150 1697 2047 \nL 1894 2003 \nQ 2506 1872 2764 1633 \nQ 3022 1394 3022 966 \nQ 3022 478 2636 193 \nQ 2250 -91 1575 -91 \nQ 1294 -91 989 -36 \nQ 684 19 347 128 \nL 347 722 \nQ 666 556 975 473 \nQ 1284 391 1588 391 \nQ 1994 391 2212 530 \nQ 2431 669 2431 922 \nQ 2431 1156 2273 1281 \nQ 2116 1406 1581 1522 \nL 1381 1569 \nQ 847 1681 609 1914 \nQ 372 2147 372 2553 \nQ 372 3047 722 3315 \nQ 1072 3584 1716 3584 \nQ 2034 3584 2315 3537 \nQ 2597 3491 2834 3397 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \nQ 2534 3019 2420 3045 \nQ 2306 3072 2169 3072 \nQ 1681 3072 1420 2755 \nQ 1159 2438 1159 1844 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1341 3275 1631 3429 \nQ 1922 3584 2338 3584 \nQ 2397 3584 2469 3576 \nQ 2541 3569 2628 3553 \nL 2631 2963 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-23\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(83.789062 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(115.576172 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(154.785156 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6b\" transform=\"translate(215.966797 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(270.251953 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(331.775391 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(395.154297 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(447.253906 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(479.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(542.517578 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(604.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(645.154297 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(676.941406 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(729.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(790.564453 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(853.943359 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(893.152344 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(954.675781 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(1018.054688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(1073.035156 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_6\">\n      <defs>\n       <path id=\"m0c6eb7d145\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m0c6eb7d145\" x=\"59.690625\" y=\"147.945727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 0 -->\n      <g transform=\"translate(46.328125 151.744945) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_7\">\n      <g>\n       <use xlink:href=\"#m0c6eb7d145\" x=\"59.690625\" y=\"120.556425\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 5000 -->\n      <g transform=\"translate(27.240625 124.355644) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-35\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#m0c6eb7d145\" x=\"59.690625\" y=\"93.167123\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 10000 -->\n      <g transform=\"translate(20.878125 96.966342) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(254.492188 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#m0c6eb7d145\" x=\"59.690625\" y=\"65.777822\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 15000 -->\n      <g transform=\"translate(20.878125 69.577041) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(254.492188 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#m0c6eb7d145\" x=\"59.690625\" y=\"38.38852\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_11\">\n      <!-- 20000 -->\n      <g transform=\"translate(20.878125 42.187739) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(254.492188 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_6\">\n     <g id=\"line2d_11\">\n      <g>\n       <use xlink:href=\"#m0c6eb7d145\" x=\"59.690625\" y=\"10.999219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_12\">\n      <!-- 25000 -->\n      <g transform=\"translate(20.878125 14.798437) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(254.492188 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_13\">\n     <!-- count -->\n     <g transform=\"translate(14.798438 92.751977) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-75\" d=\"M 544 1381 \nL 544 3500 \nL 1119 3500 \nL 1119 1403 \nQ 1119 906 1312 657 \nQ 1506 409 1894 409 \nQ 2359 409 2629 706 \nQ 2900 1003 2900 1516 \nL 2900 3500 \nL 3475 3500 \nL 3475 0 \nL 2900 0 \nL 2900 538 \nQ 2691 219 2414 64 \nQ 2138 -91 1772 -91 \nQ 1169 -91 856 284 \nQ 544 659 544 1381 \nz\nM 1991 3584 \nL 1991 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-63\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(54.980469 0)\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(116.162109 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(179.541016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(242.919922 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"patch_23\">\n    <path d=\"M 59.690625 147.945727 \nL 59.690625 9.345727 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_24\">\n    <path d=\"M 254.990625 147.945727 \nL 254.990625 9.345727 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_25\">\n    <path d=\"M 59.690625 147.945727 \nL 254.990625 147.945727 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_26\">\n    <path d=\"M 59.690625 9.345727 \nL 254.990625 9.345727 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_27\">\n     <path d=\"M 155.389063 46.701977 \nL 247.990625 46.701977 \nQ 249.990625 46.701977 249.990625 44.701977 \nL 249.990625 16.345727 \nQ 249.990625 14.345727 247.990625 14.345727 \nL 155.389063 14.345727 \nQ 153.389063 14.345727 153.389063 16.345727 \nL 153.389063 44.701977 \nQ 153.389063 46.701977 155.389063 46.701977 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"patch_28\">\n     <path d=\"M 157.389063 25.944164 \nL 177.389063 25.944164 \nL 177.389063 18.944164 \nL 157.389063 18.944164 \nz\n\" style=\"fill: #1f77b4\"/>\n    </g>\n    <g id=\"text_14\">\n     <!-- origin -->\n     <g transform=\"translate(185.389063 25.944164) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \nL 1178 3500 \nL 1178 0 \nL 603 0 \nL 603 3500 \nz\nM 603 4863 \nL 1178 4863 \nL 1178 4134 \nL 603 4134 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-67\" d=\"M 2906 1791 \nQ 2906 2416 2648 2759 \nQ 2391 3103 1925 3103 \nQ 1463 3103 1205 2759 \nQ 947 2416 947 1791 \nQ 947 1169 1205 825 \nQ 1463 481 1925 481 \nQ 2391 481 2648 825 \nQ 2906 1169 2906 1791 \nz\nM 3481 434 \nQ 3481 -459 3084 -895 \nQ 2688 -1331 1869 -1331 \nQ 1566 -1331 1297 -1286 \nQ 1028 -1241 775 -1147 \nL 775 -588 \nQ 1028 -725 1275 -790 \nQ 1522 -856 1778 -856 \nQ 2344 -856 2625 -561 \nQ 2906 -266 2906 331 \nL 2906 616 \nQ 2728 306 2450 153 \nQ 2172 0 1784 0 \nQ 1141 0 747 490 \nQ 353 981 353 1791 \nQ 353 2603 747 3093 \nQ 1141 3584 1784 3584 \nQ 2172 3584 2450 3431 \nQ 2728 3278 2906 2969 \nL 2906 3500 \nL 3481 3500 \nL 3481 434 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-6f\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(61.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(102.294922 0)\"/>\n      <use xlink:href=\"#DejaVuSans-67\" transform=\"translate(130.078125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(193.554688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(221.337891 0)\"/>\n     </g>\n    </g>\n    <g id=\"patch_29\">\n     <path d=\"M 157.389063 40.622289 \nL 177.389063 40.622289 \nL 177.389063 33.622289 \nL 157.389063 33.622289 \nz\n\" style=\"fill: url(#h27bc278b12)\"/>\n    </g>\n    <g id=\"text_15\">\n     <!-- subsampled -->\n     <g transform=\"translate(185.389063 40.622289) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-62\" d=\"M 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\nM 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2969 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \nQ 1497 1759 1228 1600 \nQ 959 1441 959 1056 \nQ 959 750 1161 570 \nQ 1363 391 1709 391 \nQ 2188 391 2477 730 \nQ 2766 1069 2766 1631 \nL 2766 1759 \nL 2194 1759 \nz\nM 3341 1997 \nL 3341 0 \nL 2766 0 \nL 2766 531 \nQ 2569 213 2275 61 \nQ 1981 -91 1556 -91 \nQ 1019 -91 701 211 \nQ 384 513 384 1019 \nQ 384 1609 779 1909 \nQ 1175 2209 1959 2209 \nL 2766 2209 \nL 2766 2266 \nQ 2766 2663 2505 2880 \nQ 2244 3097 1772 3097 \nQ 1472 3097 1187 3025 \nQ 903 2953 641 2809 \nL 641 3341 \nQ 956 3463 1253 3523 \nQ 1550 3584 1831 3584 \nQ 2591 3584 2966 3190 \nQ 3341 2797 3341 1997 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6d\" d=\"M 3328 2828 \nQ 3544 3216 3844 3400 \nQ 4144 3584 4550 3584 \nQ 5097 3584 5394 3201 \nQ 5691 2819 5691 2113 \nL 5691 0 \nL 5113 0 \nL 5113 2094 \nQ 5113 2597 4934 2840 \nQ 4756 3084 4391 3084 \nQ 3944 3084 3684 2787 \nQ 3425 2491 3425 1978 \nL 3425 0 \nL 2847 0 \nL 2847 2094 \nQ 2847 2600 2669 2842 \nQ 2491 3084 2119 3084 \nQ 1678 3084 1418 2786 \nQ 1159 2488 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1356 3278 1631 3431 \nQ 1906 3584 2284 3584 \nQ 2666 3584 2933 3390 \nQ 3200 3197 3328 2828 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-64\" d=\"M 2906 2969 \nL 2906 4863 \nL 3481 4863 \nL 3481 0 \nL 2906 0 \nL 2906 525 \nQ 2725 213 2448 61 \nQ 2172 -91 1784 -91 \nQ 1150 -91 751 415 \nQ 353 922 353 1747 \nQ 353 2572 751 3078 \nQ 1150 3584 1784 3584 \nQ 2172 3584 2448 3432 \nQ 2725 3281 2906 2969 \nz\nM 947 1747 \nQ 947 1113 1208 752 \nQ 1469 391 1925 391 \nQ 2381 391 2643 752 \nQ 2906 1113 2906 1747 \nQ 2906 2381 2643 2742 \nQ 2381 3103 1925 3103 \nQ 1469 3103 1208 2742 \nQ 947 2381 947 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-73\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(52.099609 0)\"/>\n      <use xlink:href=\"#DejaVuSans-62\" transform=\"translate(115.478516 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(178.955078 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(231.054688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6d\" transform=\"translate(292.333984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(389.746094 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(453.222656 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(481.005859 0)\"/>\n      <use xlink:href=\"#DejaVuSans-64\" transform=\"translate(542.529297 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"pbe662095c5\">\n   <rect x=\"59.690625\" y=\"9.345727\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n <defs>\n  <pattern id=\"h27bc278b12\" patternUnits=\"userSpaceOnUse\" x=\"0\" y=\"0\" width=\"72\" height=\"72\">\n   <rect x=\"0\" y=\"0\" width=\"73\" height=\"73\" fill=\"#ff7f0e\"/>\n   <path d=\"M -36 36 \nL 36 -36 \nM -24 48 \nL 48 -24 \nM -12 60 \nL 60 -12 \nM 0 72 \nL 72 0 \nM 12 84 \nL 84 12 \nM 24 96 \nL 96 24 \nM 36 108 \nL 108 36 \n\" style=\"fill: #000000; stroke: #000000; stroke-width: 1.0; stroke-linecap: butt; stroke-linejoin: miter\"/>\n  </pattern>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-6 fw=all -->\n",
    "\n",
    "For individual tokens, the sampling rate of the high-frequency word \"the\" is less than 1/20."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-subsampling-3"
   },
   "source": [
    "def compare_counts(token):\n",
    "    return (f'# of \"{token}\": '\n",
    "            f'before={sum([l.count(token) for l in sentences])}, '\n",
    "            f'after={sum([l.count(token) for l in subsampled])}')\n",
    "\n",
    "compare_counts('the')"
   ],
   "execution_count": 6,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'# of \"the\": before=50770, after=2095'"
     },
     "metadata": {},
     "execution_count": 6
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-7 fw=all -->\n",
    "\n",
    "In contrast, \n",
    "low-frequency words \"join\" are completely kept."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-subsampling-4"
   },
   "source": [
    "compare_counts('join')"
   ],
   "execution_count": 7,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'# of \"join\": before=45, after=45'"
     },
     "metadata": {},
     "execution_count": 7
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-8 fw=all -->\n",
    "\n",
    "After subsampling, we map tokens to their indices for the corpus."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-subsampling-5"
   },
   "source": [
    "corpus = [vocab[line] for line in subsampled]\n",
    "corpus[:3]"
   ],
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[], [3, 2116, 19, 407], [141, 5278, 3055, 1581]]"
     },
     "metadata": {},
     "execution_count": 8
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-9 fw=all -->\n",
    "\n",
    "## Extracting Center Words and Context Words\n",
    "\n",
    "\n",
    "The following `get_centers_and_contexts`\n",
    "function extracts all the \n",
    "center words and their context words\n",
    "from `corpus`.\n",
    "It uniformly samples an integer between 1 and `max_window_size`\n",
    "at random as the context window size.\n",
    "For any center word,\n",
    "those words \n",
    "whose distance from it\n",
    "does not exceed the sampled\n",
    "context window size\n",
    "are its context words."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-extracting-center-words-and-context-words-1"
   },
   "source": [
    "\n",
    "def get_centers_and_contexts(corpus, max_window_size):\n",
    "    \"\"\"Return center words and context words in skip-gram.\"\"\"\n",
    "    centers, contexts = [], []\n",
    "    for line in corpus:\n",
    "        # To form a \"center word--context word\" pair, each sentence needs to\n",
    "        # have at least 2 words\n",
    "        if len(line) < 2:\n",
    "            continue\n",
    "        centers += line\n",
    "        for i in range(len(line)):  # Context window centered at `i`\n",
    "            window_size = random.randint(1, max_window_size)\n",
    "            indices = list(range(max(0, i - window_size),\n",
    "                                 min(len(line), i + 1 + window_size)))\n",
    "            # Exclude the center word from the context words\n",
    "            indices.remove(i)\n",
    "            contexts.append([line[idx] for idx in indices])\n",
    "    return centers, contexts"
   ],
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-10 fw=all -->\n",
    "\n",
    "Next, we create an artificial dataset containing two sentences of 7 and 3 words, respectively. \n",
    "Let the maximum context window size be 2 \n",
    "and print all the center words and their context words."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-extracting-center-words-and-context-words-2"
   },
   "source": [
    "tiny_dataset = [list(range(7)), list(range(7, 10))]\n",
    "print('dataset', tiny_dataset)\n",
    "for center, context in zip(*get_centers_and_contexts(tiny_dataset, 2)):\n",
    "    print('center', center, 'has contexts', context)"
   ],
   "execution_count": 10,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "dataset [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9]]\ncenter 0 has contexts [1]\ncenter 1 has contexts [0, 2, 3]\ncenter 2 has contexts [0, 1, 3, 4]\ncenter 3 has contexts [1, 2, 4, 5]\ncenter 4 has contexts [2, 3, 5, 6]\ncenter 5 has contexts [4, 6]\ncenter 6 has contexts [5]\ncenter 7 has contexts [8]\ncenter 8 has contexts [7, 9]\ncenter 9 has contexts [7, 8]\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-11 fw=all -->\n",
    "\n",
    "When training on the PTB dataset,\n",
    "we set the maximum context window size to 5. \n",
    "The following extracts all the center words and their context words in the dataset."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-extracting-center-words-and-context-words-3"
   },
   "source": [
    "all_centers, all_contexts = get_centers_and_contexts(corpus, 5)\n",
    "f'# center-context pairs: {sum([len(contexts) for contexts in all_contexts])}'"
   ],
   "execution_count": 11,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'# center-context pairs: 1502232'"
     },
     "metadata": {},
     "execution_count": 11
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-12 fw=all -->\n",
    "\n",
    "## Negative Sampling\n",
    "\n",
    "We use negative sampling for approximate training. \n",
    "To sample noise words according to \n",
    "a predefined distribution,\n",
    "we define the following `RandomGenerator` class,\n",
    "where the (possibly unnormalized) sampling distribution is passed\n",
    "via the argument `sampling_weights`."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-negative-sampling-1"
   },
   "source": [
    "\n",
    "class RandomGenerator:\n",
    "    \"\"\"Randomly draw among {1, ..., n} according to n sampling weights.\"\"\"\n",
    "    def __init__(self, sampling_weights):\n",
    "        # Exclude \n",
    "        self.population = list(range(1, len(sampling_weights) + 1))\n",
    "        self.sampling_weights = sampling_weights\n",
    "        self.candidates = []\n",
    "        self.i = 0\n",
    "\n",
    "    def draw(self):\n",
    "        if self.i == len(self.candidates):\n",
    "            # Cache `k` random sampling results\n",
    "            self.candidates = random.choices(\n",
    "                self.population, self.sampling_weights, k=10000)\n",
    "            self.i = 0\n",
    "        self.i += 1\n",
    "        return self.candidates[self.i - 1]"
   ],
   "execution_count": 12,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-13 fw=all -->\n",
    "\n",
    "For example, \n",
    "we can draw 10 random variables $X$\n",
    "among indices 1, 2, and 3\n",
    "with sampling probabilities $P(X=1)=2/9, P(X=2)=3/9$, and $P(X=3)=4/9$ as follows."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-negative-sampling-2"
   },
   "source": [
    "generator = RandomGenerator([2, 3, 4])\n",
    "[generator.draw() for _ in range(10)]"
   ],
   "execution_count": 13,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[2, 2, 2, 3, 2, 3, 3, 2, 3, 1]"
     },
     "metadata": {},
     "execution_count": 13
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-14 fw=all -->\n",
    "\n",
    "For a pair of center word and context word, \n",
    "we randomly sample `K` (5 in the experiment) noise words. According to the suggestions in the word2vec paper,\n",
    "the sampling probability $P(w)$ of \n",
    "a noise word $w$\n",
    "is \n",
    "set to its relative frequency \n",
    "in the dictionary\n",
    "raised to \n",
    "the power of 0.75 [@Mikolov.Sutskever.Chen.ea.2013]."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-negative-sampling-3"
   },
   "source": [
    "\n",
    "def get_negatives(all_contexts, vocab, counter, K):\n",
    "    \"\"\"Return noise words in negative sampling.\"\"\"\n",
    "    # Sampling weights for words with indices 1, 2, ... (index 0 is the\n",
    "    # excluded unknown token) in the vocabulary\n",
    "    sampling_weights = [counter[vocab.to_tokens(i)]**0.75\n",
    "                        for i in range(1, len(vocab))]\n",
    "    all_negatives, generator = [], RandomGenerator(sampling_weights)\n",
    "    for contexts in all_contexts:\n",
    "        negatives = []\n",
    "        while len(negatives) < len(contexts) * K:\n",
    "            neg = generator.draw()\n",
    "            # Noise words cannot be context words\n",
    "            if neg not in contexts:\n",
    "                negatives.append(neg)\n",
    "        all_negatives.append(negatives)\n",
    "    return all_negatives\n",
    "\n",
    "all_negatives = get_negatives(all_contexts, vocab, counter, 5)"
   ],
   "execution_count": 14,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-15 fw=all -->\n",
    "\n",
    "## Loading Training Examples in Minibatches\n",
    "\n",
    "After\n",
    "all the center words\n",
    "together with their\n",
    "context words and sampled noise words are extracted,\n",
    "they will be transformed into \n",
    "minibatches of examples\n",
    "that can be iteratively loaded\n",
    "during training.\n",
    "\n",
    "\n",
    "In a minibatch,\n",
    "the $i^\\textrm{th}$ example includes a center word\n",
    "and its $n_i$ context words and $m_i$ noise words. \n",
    "Due to varying context window sizes,\n",
    "$n_i+m_i$ varies for different $i$.\n",
    "Thus,\n",
    "for each example\n",
    "we concatenate its context words and noise words in \n",
    "the `contexts_negatives` variable,\n",
    "and pad zeros until the concatenation length\n",
    "reaches $\\max_i n_i+m_i$ (`max_len`).\n",
    "To exclude paddings\n",
    "in the calculation of the loss,\n",
    "we define a mask variable `masks`.\n",
    "There is a one-to-one correspondence\n",
    "between elements in `masks` and elements in `contexts_negatives`,\n",
    "where zeros (otherwise ones) in `masks` correspond to paddings in `contexts_negatives`.\n",
    "\n",
    "\n",
    "To distinguish between positive and negative examples,\n",
    "we separate context words from noise words in  `contexts_negatives` via a `labels` variable. \n",
    "Similar to `masks`,\n",
    "there is also a one-to-one correspondence\n",
    "between elements in `labels` and elements in `contexts_negatives`,\n",
    "where ones (otherwise zeros) in `labels` correspond to context words (positive examples) in `contexts_negatives`.\n",
    "\n",
    "\n",
    "The above idea is implemented in the following `batchify` function.\n",
    "Its input `data` is a list with length\n",
    "equal to the batch size,\n",
    "where each element is an example\n",
    "consisting of\n",
    "the center word `center`, its context words `context`, and its noise words `negative`.\n",
    "This function returns \n",
    "a minibatch that can be loaded for calculations \n",
    "during training,\n",
    "such as including the mask variable."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-loading-training-examples-in-minibatches-1"
   },
   "source": [
    "\n",
    "def batchify(data):\n",
    "    \"\"\"Return a minibatch of examples for skip-gram with negative sampling.\"\"\"\n",
    "    max_len = max(len(c) + len(n) for _, c, n in data)\n",
    "    centers, contexts_negatives, masks, labels = [], [], [], []\n",
    "    for center, context, negative in data:\n",
    "        cur_len = len(context) + len(negative)\n",
    "        centers += [center]\n",
    "        contexts_negatives += [context + negative + [0] * (max_len - cur_len)]\n",
    "        masks += [[1] * cur_len + [0] * (max_len - cur_len)]\n",
    "        labels += [[1] * len(context) + [0] * (max_len - len(context))]\n",
    "    return (d2l.reshape(d2l.tensor(centers), (-1, 1)), d2l.tensor(\n",
    "        contexts_negatives), d2l.tensor(masks), d2l.tensor(labels))"
   ],
   "execution_count": 15,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-16 fw=all -->\n",
    "\n",
    "Let's test this function using a minibatch of two examples."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-loading-training-examples-in-minibatches-2"
   },
   "source": [
    "x_1 = (1, [2, 2], [3, 3, 3, 3])\n",
    "x_2 = (1, [2, 2, 2], [3, 3])\n",
    "batch = batchify((x_1, x_2))\n",
    "\n",
    "names = ['centers', 'contexts_negatives', 'masks', 'labels']\n",
    "for name, data in zip(names, batch):\n",
    "    print(name, '=', data)"
   ],
   "execution_count": 16,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "centers = [[1.]\n [1.]]\ncontexts_negatives = [[2. 2. 3. 3. 3. 3.]\n [2. 2. 2. 3. 3. 0.]]\nmasks = [[1. 1. 1. 1. 1. 1.]\n [1. 1. 1. 1. 1. 0.]]\nlabels = [[1. 1. 0. 0. 0. 0.]\n [1. 1. 1. 0. 0. 0.]]\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-17 fw=all -->\n",
    "\n",
    "## Putting It All Together\n",
    "\n",
    "`batchify` makes the padding/masking logic explicit on a single minibatch,\n",
    "but calling it once per training step would dominate the cost of our tiny\n",
    "skip-gram step. A simple optimization is to apply the same idea *once*\n",
    "to the whole dataset at load time: pad every example to the *global* max\n",
    "of `len(context) + len(negative)`, and store the result as dense NumPy\n",
    "arrays. Per-epoch shuffling and per-batch slicing are then both O(1)\n",
    "per batch. The helper `_pad_ptb` below does the one-time padding and is\n",
    "what `load_data_ptb` actually calls."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-putting-it-all-together-1"
   },
   "source": [
    "\n",
    "def _pad_ptb(all_centers, all_contexts, all_negatives):\n",
    "    \"\"\"Pre-pad all skip-gram examples to the global max length.\n",
    "\n",
    "    Returns four NumPy arrays: centers (N,), contexts_negatives (N, L),\n",
    "    masks (N, L), labels (N, L), where L = max(len(c) + len(n)).\"\"\"\n",
    "    import numpy as _np\n",
    "    n = len(all_centers)\n",
    "    max_len = max(len(c) + len(neg)\n",
    "                  for c, neg in zip(all_contexts, all_negatives))\n",
    "    centers = _np.asarray(all_centers, dtype=_np.int64)\n",
    "    contexts_negatives = _np.zeros((n, max_len), dtype=_np.int64)\n",
    "    masks = _np.zeros((n, max_len), dtype=_np.float32)\n",
    "    labels = _np.zeros((n, max_len), dtype=_np.float32)\n",
    "    for i, (c, neg) in enumerate(zip(all_contexts, all_negatives)):\n",
    "        cur_len = len(c) + len(neg)\n",
    "        contexts_negatives[i, :cur_len] = c + neg\n",
    "        masks[i, :cur_len] = 1.\n",
    "        labels[i, :len(c)] = 1.\n",
    "    return centers, contexts_negatives, masks, labels"
   ],
   "execution_count": 17,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-18 fw=all -->\n",
    "\n",
    "Finally, `load_data_ptb` wires everything together: read the PTB dataset,\n",
    "build the vocabulary, extract centers/contexts/negatives, pad once, and\n",
    "return a framework-native data iterator backed by those arrays."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-putting-it-all-together-2"
   },
   "source": [
    "\n",
    "def load_data_ptb(batch_size, max_window_size, num_noise_words):\n",
    "    \"\"\"Download the PTB dataset and then load it into memory.\"\"\"\n",
    "    sentences = read_ptb()\n",
    "    vocab = d2l.Vocab(sentences, min_freq=10)\n",
    "    subsampled, counter = subsample(sentences, vocab)\n",
    "    corpus = [vocab[line] for line in subsampled]\n",
    "    all_centers, all_contexts = get_centers_and_contexts(\n",
    "        corpus, max_window_size)\n",
    "    all_negatives = get_negatives(\n",
    "        all_contexts, vocab, counter, num_noise_words)\n",
    "    centers, cn, masks, labels = _pad_ptb(\n",
    "        all_centers, all_contexts, all_negatives)\n",
    "    # reshape(-1, 1) for centers matches the shape batchify used to produce\n",
    "    dataset = gluon.data.ArrayDataset(\n",
    "        centers.reshape(-1, 1), cn, masks, labels)\n",
    "    data_iter = gluon.data.DataLoader(\n",
    "        dataset, batch_size, shuffle=True,\n",
    "        num_workers=d2l.get_dataloader_workers())\n",
    "    return data_iter, vocab"
   ],
   "execution_count": 18,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-19 fw=all -->\n",
    "\n",
    "Let's print the first minibatch of the data iterator."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "word-embedding-dataset-putting-it-all-together-3"
   },
   "source": [
    "data_iter, vocab = load_data_ptb(512, 5, 5)\n",
    "for batch in data_iter:\n",
    "    for name, data in zip(names, batch):\n",
    "        print(name, 'shape:', data.shape)\n",
    "    break"
   ],
   "execution_count": 19,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "centers shape: (512, 1)\ncontexts_negatives shape: (512, 60)\nmasks shape: (512, 60)\nlabels shape: (512, 60)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-20 fw=all -->\n",
    "\n",
    "## Summary\n",
    "\n",
    "* High-frequency words may not be so useful in training. We can subsample them for speedup in training.\n",
    "* For computational efficiency, we load examples in minibatches. We can define other variables to distinguish paddings from non-paddings, and positive examples from negative ones.\n",
    "\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. How does the running time of code in this section changes if not using subsampling?\n",
    "1. The `RandomGenerator` class caches `k` random sampling results. Set `k` to other values and see how it affects the data loading speed.\n",
    "1. What other hyperparameters in the code of this section may affect the data loading speed?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=word-embedding-dataset-md-21 fw=mxnet -->\n",
    "\n",
    "[Discussions](https://d2l.discourse.group/t/383)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-mxnet",
   "display_name": "d2l (MXNet)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
