{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 0
   },
   "source": [
    "# Deep Factorization Machines\n",
    "\n",
    "Learning effective feature combinations is critical to the success of click-through rate prediction task. Factorization machines model feature interactions in a linear paradigm (e.g., bilinear interactions). This is often insufficient for real-world data where inherent feature crossing structures are usually very complex and nonlinear. What's worse, second-order feature interactions are generally used in factorization machines in practice. Modeling higher degrees of feature combinations with factorization machines is possible theoretically but it is usually not adopted due to numerical instability and high computational complexity.\n",
    "\n",
    "One effective solution is using deep neural networks. Deep neural networks are powerful in feature representation learning and have the potential to learn sophisticated feature interactions. As such, it is natural to integrate deep neural networks to factorization machines. Adding nonlinear transformation layers to factorization machines gives it the capability to model both low-order feature combinations and high-order feature combinations. Moreover, non-linear inherent structures from inputs can also be captured with deep neural networks. In this section, we will introduce a representative model named deep factorization machines (DeepFM) :cite:`Guo.Tang.Ye.ea.2017` which combine FM and deep neural networks.\n",
    "\n",
    "\n",
    "## Model Architectures\n",
    "\n",
    "DeepFM consists of an FM component and a deep component which are integrated in a parallel structure. The FM component is the same as the 2-way factorization machines which is used to model the low-order feature interactions. The deep component is an MLP that is used to capture high-order feature interactions and nonlinearities. These two components share the same inputs/embeddings and their outputs are summed up as the final prediction. It is worth pointing out that the spirit of DeepFM resembles that of the Wide \\& Deep architecture which can capture both memorization and generalization. The advantages of DeepFM over the Wide \\& Deep model is that it reduces the effort of hand-crafted feature engineering by identifying feature combinations automatically.\n",
    "\n",
    "We omit the description of the FM component for brevity and denote the output as $\\hat{y}^{(FM)}$. Readers are referred to the last section for more details. Let $\\mathbf{e}_i \\in \\mathbb{R}^{k}$ denote the latent feature vector of the $i^\\mathrm{th}$ field.  The input of the deep component is the concatenation of the dense embeddings of all fields that are looked up with the sparse categorical feature input, denoted as:\n",
    "\n",
    "$$\n",
    "\\mathbf{z}^{(0)}  = [\\mathbf{e}_1, \\mathbf{e}_2, ..., \\mathbf{e}_f],\n",
    "$$\n",
    "\n",
    "where $f$ is the number of fields.  It is then fed into the following neural network:\n",
    "\n",
    "$$\n",
    "\\mathbf{z}^{(l)}  = \\alpha(\\mathbf{W}^{(l)}\\mathbf{z}^{(l-1)} + \\mathbf{b}^{(l)}),\n",
    "$$\n",
    "\n",
    "where $\\alpha$ is the activation function.  $\\mathbf{W}_{l}$ and $\\mathbf{b}_{l}$ are the weight and bias at the $l^\\mathrm{th}$ layer. Let $y_{DNN}$ denote the output of the prediction. The ultimate prediction of DeepFM is the summation of the outputs from both FM and DNN. So we have:\n",
    "\n",
    "$$\n",
    "\\hat{y} = \\sigma(\\hat{y}^{(FM)} + \\hat{y}^{(DNN)}),\n",
    "$$\n",
    "\n",
    "where $\\sigma$ is the sigmoid function. The architecture of DeepFM is illustrated below.\n",
    "![Illustration of the DeepFM model](../img/rec-deepfm.svg)\n",
    "\n",
    "It is worth noting that DeepFM is not the only way to combine deep neural networks with FM. We can also add nonlinear layers over the feature interactions :cite:`He.Chua.2017`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "2"
    },
    "origin_pos": 1,
    "tab": [
     "mxnet"
    ]
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from mxnet import gluon, init, np, npx\n",
    "from mxnet.gluon import nn\n",
    "from d2l import mxnet as d2l\n",
    "\n",
    "npx.set_np()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 2
   },
   "source": [
    "## Implemenation of DeepFM\n",
    "The implementation of DeepFM is similar to that of FM. We keep the FM part unchanged and use an MLP block with `relu` as the activation function. Dropout is also used to regularize the model. The number of neurons of the MLP can be adjusted with the `mlp_dims` hyperparameter.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "2"
    },
    "origin_pos": 3,
    "tab": [
     "mxnet"
    ]
   },
   "outputs": [],
   "source": [
    "class DeepFM(nn.Block):\n",
    "    def __init__(self, field_dims, num_factors, mlp_dims, drop_rate=0.1):\n",
    "        super(DeepFM, self).__init__()\n",
    "        num_inputs = int(sum(field_dims))\n",
    "        self.embedding = nn.Embedding(num_inputs, num_factors)\n",
    "        self.fc = nn.Embedding(num_inputs, 1)\n",
    "        self.linear_layer = nn.Dense(1, use_bias=True)\n",
    "        input_dim = self.embed_output_dim = len(field_dims) * num_factors\n",
    "        self.mlp = nn.Sequential()\n",
    "        for dim in mlp_dims:\n",
    "            self.mlp.add(nn.Dense(dim, 'relu', True, in_units=input_dim))\n",
    "            self.mlp.add(nn.Dropout(rate=drop_rate))\n",
    "            input_dim = dim\n",
    "        self.mlp.add(nn.Dense(in_units=input_dim, units=1))\n",
    "\n",
    "    def forward(self, x):\n",
    "        embed_x = self.embedding(x)\n",
    "        square_of_sum = np.sum(embed_x, axis=1) ** 2\n",
    "        sum_of_square = np.sum(embed_x ** 2, axis=1)\n",
    "        inputs = np.reshape(embed_x, (-1, self.embed_output_dim))\n",
    "        x = self.linear_layer(self.fc(x).sum(1)) \\\n",
    "            + 0.5 * (square_of_sum - sum_of_square).sum(1, keepdims=True) \\\n",
    "            + self.mlp(inputs)\n",
    "        x = npx.sigmoid(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 4
   },
   "source": [
    "## Training and Evaluating the Model\n",
    "The data loading process is the same as that of FM. We set the MLP component of DeepFM to a three-layered dense network with the a pyramid structure (30-20-10). All other hyperparameters remain the same as FM.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "4"
    },
    "origin_pos": 5,
    "tab": [
     "mxnet"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loss 0.509, train acc 0.457, test acc 0.457\n",
      "89680.6 examples/sec on [gpu(0), gpu(1)]\n"
     ]
    },
    {
     "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=\"238.965625pt\" height=\"184.455469pt\" viewBox=\"0 0 238.965625 184.455469\" 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>2022-03-24T11:01:47.319341</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.5.1, 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 184.455469 \n",
       "L 238.965625 184.455469 \n",
       "L 238.965625 -0 \n",
       "L 0 -0 \n",
       "L 0 184.455469 \n",
       "z\n",
       "\" style=\"fill: none\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 30.103125 146.899219 \n",
       "L 225.403125 146.899219 \n",
       "L 225.403125 10.999219 \n",
       "L 30.103125 10.999219 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <path d=\"M 57.041056 146.899219 \n",
       "L 57.041056 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_2\">\n",
       "      <defs>\n",
       "       <path id=\"m108a8b77fe\" d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m108a8b77fe\" x=\"57.041056\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- 5 -->\n",
       "      <g transform=\"translate(53.859806 161.497656)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \n",
       "L 3169 4666 \n",
       "L 3169 4134 \n",
       "L 1269 4134 \n",
       "L 1269 2991 \n",
       "Q 1406 3038 1543 3061 \n",
       "Q 1681 3084 1819 3084 \n",
       "Q 2600 3084 3056 2656 \n",
       "Q 3513 2228 3513 1497 \n",
       "Q 3513 744 3044 326 \n",
       "Q 2575 -91 1722 -91 \n",
       "Q 1428 -91 1123 -41 \n",
       "Q 819 9 494 109 \n",
       "L 494 744 \n",
       "Q 775 591 1075 516 \n",
       "Q 1375 441 1709 441 \n",
       "Q 2250 441 2565 725 \n",
       "Q 2881 1009 2881 1497 \n",
       "Q 2881 1984 2565 2268 \n",
       "Q 2250 2553 1709 2553 \n",
       "Q 1456 2553 1204 2497 \n",
       "Q 953 2441 691 2322 \n",
       "L 691 4666 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <path d=\"M 90.71347 146.899219 \n",
       "L 90.71347 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m108a8b77fe\" x=\"90.71347\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- 10 -->\n",
       "      <g transform=\"translate(84.35097 161.497656)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-31\" d=\"M 794 531 \n",
       "L 1825 531 \n",
       "L 1825 4091 \n",
       "L 703 3866 \n",
       "L 703 4441 \n",
       "L 1819 4666 \n",
       "L 2450 4666 \n",
       "L 2450 531 \n",
       "L 3481 531 \n",
       "L 3481 0 \n",
       "L 794 0 \n",
       "L 794 531 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \n",
       "Q 1547 4250 1301 3770 \n",
       "Q 1056 3291 1056 2328 \n",
       "Q 1056 1369 1301 889 \n",
       "Q 1547 409 2034 409 \n",
       "Q 2525 409 2770 889 \n",
       "Q 3016 1369 3016 2328 \n",
       "Q 3016 3291 2770 3770 \n",
       "Q 2525 4250 2034 4250 \n",
       "z\n",
       "M 2034 4750 \n",
       "Q 2819 4750 3233 4129 \n",
       "Q 3647 3509 3647 2328 \n",
       "Q 3647 1150 3233 529 \n",
       "Q 2819 -91 2034 -91 \n",
       "Q 1250 -91 836 529 \n",
       "Q 422 1150 422 2328 \n",
       "Q 422 3509 836 4129 \n",
       "Q 1250 4750 2034 4750 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"63.623047\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <path d=\"M 124.385884 146.899219 \n",
       "L 124.385884 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m108a8b77fe\" x=\"124.385884\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 15 -->\n",
       "      <g transform=\"translate(118.023384 161.497656)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-35\" x=\"63.623047\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <path d=\"M 158.058297 146.899219 \n",
       "L 158.058297 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m108a8b77fe\" x=\"158.058297\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 20 -->\n",
       "      <g transform=\"translate(151.695797 161.497656)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \n",
       "L 3431 531 \n",
       "L 3431 0 \n",
       "L 469 0 \n",
       "L 469 531 \n",
       "Q 828 903 1448 1529 \n",
       "Q 2069 2156 2228 2338 \n",
       "Q 2531 2678 2651 2914 \n",
       "Q 2772 3150 2772 3378 \n",
       "Q 2772 3750 2511 3984 \n",
       "Q 2250 4219 1831 4219 \n",
       "Q 1534 4219 1204 4116 \n",
       "Q 875 4013 500 3803 \n",
       "L 500 4441 \n",
       "Q 881 4594 1212 4672 \n",
       "Q 1544 4750 1819 4750 \n",
       "Q 2544 4750 2975 4387 \n",
       "Q 3406 4025 3406 3419 \n",
       "Q 3406 3131 3298 2873 \n",
       "Q 3191 2616 2906 2266 \n",
       "Q 2828 2175 2409 1742 \n",
       "Q 1991 1309 1228 531 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"63.623047\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <path d=\"M 191.730711 146.899219 \n",
       "L 191.730711 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m108a8b77fe\" x=\"191.730711\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- 25 -->\n",
       "      <g transform=\"translate(185.368211 161.497656)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-35\" x=\"63.623047\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <path d=\"M 225.403125 146.899219 \n",
       "L 225.403125 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m108a8b77fe\" x=\"225.403125\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- 30 -->\n",
       "      <g transform=\"translate(219.040625 161.497656)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \n",
       "Q 3050 2419 3304 2112 \n",
       "Q 3559 1806 3559 1356 \n",
       "Q 3559 666 3084 287 \n",
       "Q 2609 -91 1734 -91 \n",
       "Q 1441 -91 1130 -33 \n",
       "Q 819 25 488 141 \n",
       "L 488 750 \n",
       "Q 750 597 1062 519 \n",
       "Q 1375 441 1716 441 \n",
       "Q 2309 441 2620 675 \n",
       "Q 2931 909 2931 1356 \n",
       "Q 2931 1769 2642 2001 \n",
       "Q 2353 2234 1838 2234 \n",
       "L 1294 2234 \n",
       "L 1294 2753 \n",
       "L 1863 2753 \n",
       "Q 2328 2753 2575 2939 \n",
       "Q 2822 3125 2822 3475 \n",
       "Q 2822 3834 2567 4026 \n",
       "Q 2313 4219 1838 4219 \n",
       "Q 1578 4219 1281 4162 \n",
       "Q 984 4106 628 3988 \n",
       "L 628 4550 \n",
       "Q 988 4650 1302 4700 \n",
       "Q 1616 4750 1894 4750 \n",
       "Q 2613 4750 3031 4423 \n",
       "Q 3450 4097 3450 3541 \n",
       "Q 3450 3153 3228 2886 \n",
       "Q 3006 2619 2597 2516 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-33\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"63.623047\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_7\">\n",
       "     <!-- epoch -->\n",
       "     <g transform=\"translate(112.525 175.175781)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \n",
       "L 3597 1613 \n",
       "L 953 1613 \n",
       "Q 991 1019 1311 708 \n",
       "Q 1631 397 2203 397 \n",
       "Q 2534 397 2845 478 \n",
       "Q 3156 559 3463 722 \n",
       "L 3463 178 \n",
       "Q 3153 47 2828 -22 \n",
       "Q 2503 -91 2169 -91 \n",
       "Q 1331 -91 842 396 \n",
       "Q 353 884 353 1716 \n",
       "Q 353 2575 817 3079 \n",
       "Q 1281 3584 2069 3584 \n",
       "Q 2775 3584 3186 3129 \n",
       "Q 3597 2675 3597 1894 \n",
       "z\n",
       "M 3022 2063 \n",
       "Q 3016 2534 2758 2815 \n",
       "Q 2500 3097 2075 3097 \n",
       "Q 1594 3097 1305 2825 \n",
       "Q 1016 2553 972 2059 \n",
       "L 3022 2063 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \n",
       "L 1159 -1331 \n",
       "L 581 -1331 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2969 \n",
       "Q 1341 3281 1617 3432 \n",
       "Q 1894 3584 2278 3584 \n",
       "Q 2916 3584 3314 3078 \n",
       "Q 3713 2572 3713 1747 \n",
       "Q 3713 922 3314 415 \n",
       "Q 2916 -91 2278 -91 \n",
       "Q 1894 -91 1617 61 \n",
       "Q 1341 213 1159 525 \n",
       "z\n",
       "M 3116 1747 \n",
       "Q 3116 2381 2855 2742 \n",
       "Q 2594 3103 2138 3103 \n",
       "Q 1681 3103 1420 2742 \n",
       "Q 1159 2381 1159 1747 \n",
       "Q 1159 1113 1420 752 \n",
       "Q 1681 391 2138 391 \n",
       "Q 2594 391 2855 752 \n",
       "Q 3116 1113 3116 1747 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \n",
       "Q 1497 3097 1228 2736 \n",
       "Q 959 2375 959 1747 \n",
       "Q 959 1119 1226 758 \n",
       "Q 1494 397 1959 397 \n",
       "Q 2419 397 2687 759 \n",
       "Q 2956 1122 2956 1747 \n",
       "Q 2956 2369 2687 2733 \n",
       "Q 2419 3097 1959 3097 \n",
       "z\n",
       "M 1959 3584 \n",
       "Q 2709 3584 3137 3096 \n",
       "Q 3566 2609 3566 1747 \n",
       "Q 3566 888 3137 398 \n",
       "Q 2709 -91 1959 -91 \n",
       "Q 1206 -91 779 398 \n",
       "Q 353 888 353 1747 \n",
       "Q 353 2609 779 3096 \n",
       "Q 1206 3584 1959 3584 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \n",
       "L 3122 2828 \n",
       "Q 2878 2963 2633 3030 \n",
       "Q 2388 3097 2138 3097 \n",
       "Q 1578 3097 1268 2742 \n",
       "Q 959 2388 959 1747 \n",
       "Q 959 1106 1268 751 \n",
       "Q 1578 397 2138 397 \n",
       "Q 2388 397 2633 464 \n",
       "Q 2878 531 3122 666 \n",
       "L 3122 134 \n",
       "Q 2881 22 2623 -34 \n",
       "Q 2366 -91 2075 -91 \n",
       "Q 1284 -91 818 406 \n",
       "Q 353 903 353 1747 \n",
       "Q 353 2603 823 3093 \n",
       "Q 1294 3584 2113 3584 \n",
       "Q 2378 3584 2631 3529 \n",
       "Q 2884 3475 3122 3366 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 4863 \n",
       "L 1159 4863 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-70\" x=\"61.523438\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6f\" x=\"125\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-63\" x=\"186.181641\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-68\" x=\"241.162109\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path d=\"M 30.103125 146.899219 \n",
       "L 225.403125 146.899219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\">\n",
       "      <defs>\n",
       "       <path id=\"m4631ecf519\" d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m4631ecf519\" x=\"30.103125\" y=\"146.899219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 0.0 -->\n",
       "      <g transform=\"translate(7.2 150.698437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-2e\" d=\"M 684 794 \n",
       "L 1344 794 \n",
       "L 1344 0 \n",
       "L 684 0 \n",
       "L 684 794 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_15\">\n",
       "      <path d=\"M 30.103125 119.719219 \n",
       "L 225.403125 119.719219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_16\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m4631ecf519\" x=\"30.103125\" y=\"119.719219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- 0.2 -->\n",
       "      <g transform=\"translate(7.2 123.518437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-32\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_17\">\n",
       "      <path d=\"M 30.103125 92.539219 \n",
       "L 225.403125 92.539219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m4631ecf519\" x=\"30.103125\" y=\"92.539219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <!-- 0.4 -->\n",
       "      <g transform=\"translate(7.2 96.338437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \n",
       "L 825 1625 \n",
       "L 2419 1625 \n",
       "L 2419 4116 \n",
       "z\n",
       "M 2253 4666 \n",
       "L 3047 4666 \n",
       "L 3047 1625 \n",
       "L 3713 1625 \n",
       "L 3713 1100 \n",
       "L 3047 1100 \n",
       "L 3047 0 \n",
       "L 2419 0 \n",
       "L 2419 1100 \n",
       "L 313 1100 \n",
       "L 313 1709 \n",
       "L 2253 4666 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-34\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_19\">\n",
       "      <path d=\"M 30.103125 65.359219 \n",
       "L 225.403125 65.359219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_20\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m4631ecf519\" x=\"30.103125\" y=\"65.359219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <!-- 0.6 -->\n",
       "      <g transform=\"translate(7.2 69.158437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \n",
       "Q 1688 2584 1439 2293 \n",
       "Q 1191 2003 1191 1497 \n",
       "Q 1191 994 1439 701 \n",
       "Q 1688 409 2113 409 \n",
       "Q 2538 409 2786 701 \n",
       "Q 3034 994 3034 1497 \n",
       "Q 3034 2003 2786 2293 \n",
       "Q 2538 2584 2113 2584 \n",
       "z\n",
       "M 3366 4563 \n",
       "L 3366 3988 \n",
       "Q 3128 4100 2886 4159 \n",
       "Q 2644 4219 2406 4219 \n",
       "Q 1781 4219 1451 3797 \n",
       "Q 1122 3375 1075 2522 \n",
       "Q 1259 2794 1537 2939 \n",
       "Q 1816 3084 2150 3084 \n",
       "Q 2853 3084 3261 2657 \n",
       "Q 3669 2231 3669 1497 \n",
       "Q 3669 778 3244 343 \n",
       "Q 2819 -91 2113 -91 \n",
       "Q 1303 -91 875 529 \n",
       "Q 447 1150 447 2328 \n",
       "Q 447 3434 972 4092 \n",
       "Q 1497 4750 2381 4750 \n",
       "Q 2619 4750 2861 4703 \n",
       "Q 3103 4656 3366 4563 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-36\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_21\">\n",
       "      <path d=\"M 30.103125 38.179219 \n",
       "L 225.403125 38.179219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_22\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m4631ecf519\" x=\"30.103125\" y=\"38.179219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <!-- 0.8 -->\n",
       "      <g transform=\"translate(7.2 41.978437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \n",
       "Q 1584 2216 1326 1975 \n",
       "Q 1069 1734 1069 1313 \n",
       "Q 1069 891 1326 650 \n",
       "Q 1584 409 2034 409 \n",
       "Q 2484 409 2743 651 \n",
       "Q 3003 894 3003 1313 \n",
       "Q 3003 1734 2745 1975 \n",
       "Q 2488 2216 2034 2216 \n",
       "z\n",
       "M 1403 2484 \n",
       "Q 997 2584 770 2862 \n",
       "Q 544 3141 544 3541 \n",
       "Q 544 4100 942 4425 \n",
       "Q 1341 4750 2034 4750 \n",
       "Q 2731 4750 3128 4425 \n",
       "Q 3525 4100 3525 3541 \n",
       "Q 3525 3141 3298 2862 \n",
       "Q 3072 2584 2669 2484 \n",
       "Q 3125 2378 3379 2068 \n",
       "Q 3634 1759 3634 1313 \n",
       "Q 3634 634 3220 271 \n",
       "Q 2806 -91 2034 -91 \n",
       "Q 1263 -91 848 271 \n",
       "Q 434 634 434 1313 \n",
       "Q 434 1759 690 2068 \n",
       "Q 947 2378 1403 2484 \n",
       "z\n",
       "M 1172 3481 \n",
       "Q 1172 3119 1398 2916 \n",
       "Q 1625 2713 2034 2713 \n",
       "Q 2441 2713 2670 2916 \n",
       "Q 2900 3119 2900 3481 \n",
       "Q 2900 3844 2670 4047 \n",
       "Q 2441 4250 2034 4250 \n",
       "Q 1625 4250 1398 4047 \n",
       "Q 1172 3844 1172 3481 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-38\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_6\">\n",
       "     <g id=\"line2d_23\">\n",
       "      <path d=\"M 30.103125 10.999219 \n",
       "L 225.403125 10.999219 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_24\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m4631ecf519\" x=\"30.103125\" y=\"10.999219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_13\">\n",
       "      <!-- 1.0 -->\n",
       "      <g transform=\"translate(7.2 14.798437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_25\">\n",
       "    <path d=\"M 24.330711 48.988563 \n",
       "L 26.254849 49.728087 \n",
       "L 27.216918 50.116834 \n",
       "L 29.141056 51.128916 \n",
       "L 30.103125 51.489453 \n",
       "L 31.065194 54.73347 \n",
       "L 32.027263 55.785853 \n",
       "L 32.989332 57.046025 \n",
       "L 33.951401 57.501334 \n",
       "L 34.91347 58.645613 \n",
       "L 36.837608 60.066385 \n",
       "L 37.799677 66.570473 \n",
       "L 38.761746 67.645942 \n",
       "L 39.723815 68.527376 \n",
       "L 41.647953 69.513035 \n",
       "L 42.610022 69.858568 \n",
       "L 43.572091 70.374518 \n",
       "L 44.534159 72.941013 \n",
       "L 45.496228 73.822425 \n",
       "L 47.420366 74.011687 \n",
       "L 48.382435 74.356056 \n",
       "L 50.306573 74.401495 \n",
       "L 51.268642 74.570087 \n",
       "L 51.268642 76.007962 \n",
       "L 52.230711 76.02821 \n",
       "L 53.19278 76.248187 \n",
       "L 54.154849 76.171116 \n",
       "L 55.116918 75.967487 \n",
       "L 57.041056 75.85362 \n",
       "L 58.003125 75.108094 \n",
       "L 58.965194 75.993796 \n",
       "L 59.927263 75.88411 \n",
       "L 62.81347 76.168865 \n",
       "L 63.775539 76.049434 \n",
       "L 64.737608 76.058017 \n",
       "L 66.661746 76.535852 \n",
       "L 67.623815 76.581275 \n",
       "L 68.585884 76.458778 \n",
       "L 70.510022 76.544159 \n",
       "L 71.472091 76.616164 \n",
       "L 71.472091 75.96697 \n",
       "L 72.434159 76.50629 \n",
       "L 74.358297 76.654771 \n",
       "L 76.282435 76.946022 \n",
       "L 77.244504 76.896884 \n",
       "L 78.206573 76.397001 \n",
       "L 79.168642 77.006077 \n",
       "L 80.130711 76.952429 \n",
       "L 81.09278 77.135693 \n",
       "L 83.016918 77.11996 \n",
       "L 83.978987 77.16955 \n",
       "L 84.941056 76.707489 \n",
       "L 85.903125 76.781868 \n",
       "L 86.865194 77.1033 \n",
       "L 89.751401 77.281333 \n",
       "L 90.71347 77.142461 \n",
       "L 91.675539 77.163515 \n",
       "L 91.675539 77.916706 \n",
       "L 92.637608 77.851932 \n",
       "L 93.599677 77.966528 \n",
       "L 94.561746 77.525839 \n",
       "L 95.523815 77.499304 \n",
       "L 96.485884 77.339818 \n",
       "L 97.447953 77.468339 \n",
       "L 98.410022 76.052363 \n",
       "L 99.372091 77.275516 \n",
       "L 101.296228 77.496372 \n",
       "L 103.220366 77.701614 \n",
       "L 104.182435 77.601007 \n",
       "L 105.144504 76.886424 \n",
       "L 106.106573 77.298886 \n",
       "L 107.068642 77.107806 \n",
       "L 108.030711 77.315222 \n",
       "L 109.954849 77.409511 \n",
       "L 110.916918 77.564111 \n",
       "L 111.878987 77.596762 \n",
       "L 111.878987 76.443404 \n",
       "L 112.841056 77.699186 \n",
       "L 113.803125 77.657851 \n",
       "L 114.765194 77.50348 \n",
       "L 117.651401 77.462762 \n",
       "L 118.61347 78.258914 \n",
       "L 119.575539 77.78575 \n",
       "L 121.499677 77.69689 \n",
       "L 123.423815 77.88571 \n",
       "L 124.385884 77.831061 \n",
       "L 125.347953 76.862763 \n",
       "L 126.310022 76.944801 \n",
       "L 127.272091 77.444863 \n",
       "L 129.196228 77.585789 \n",
       "L 130.158297 77.466468 \n",
       "L 131.120366 77.588314 \n",
       "L 132.082435 77.587475 \n",
       "L 132.082435 77.276092 \n",
       "L 133.044504 77.327004 \n",
       "L 134.006573 77.671327 \n",
       "L 134.968642 77.418997 \n",
       "L 135.930711 77.639772 \n",
       "L 136.89278 77.573231 \n",
       "L 137.854849 77.690698 \n",
       "L 138.816918 78.948247 \n",
       "L 139.778987 78.194059 \n",
       "L 140.741056 77.814572 \n",
       "L 141.703125 77.943472 \n",
       "L 144.589332 77.708111 \n",
       "L 145.551401 78.892572 \n",
       "L 146.51347 78.014965 \n",
       "L 147.475539 78.172901 \n",
       "L 148.437608 77.961142 \n",
       "L 149.399677 77.524325 \n",
       "L 151.323815 77.689912 \n",
       "L 152.285884 77.771259 \n",
       "L 152.285884 77.310781 \n",
       "L 153.247953 77.725908 \n",
       "L 154.210022 77.548447 \n",
       "L 155.172091 77.588197 \n",
       "L 156.134159 77.782621 \n",
       "L 158.058297 77.749334 \n",
       "L 159.020366 78.062223 \n",
       "L 159.982435 77.596239 \n",
       "L 160.944504 77.786186 \n",
       "L 163.830711 77.798216 \n",
       "L 164.79278 77.775977 \n",
       "L 165.754849 76.8234 \n",
       "L 166.716918 76.835034 \n",
       "L 167.678987 77.318861 \n",
       "L 169.603125 77.510712 \n",
       "L 170.565194 77.36814 \n",
       "L 172.489332 77.533617 \n",
       "L 172.489332 78.189182 \n",
       "L 173.451401 78.208666 \n",
       "L 174.41347 77.888081 \n",
       "L 176.337608 77.74496 \n",
       "L 177.299677 77.682762 \n",
       "L 178.261746 77.73742 \n",
       "L 179.223815 77.525094 \n",
       "L 180.185884 78.171501 \n",
       "L 181.147953 78.061156 \n",
       "L 182.110022 78.062838 \n",
       "L 183.072091 77.819326 \n",
       "L 184.996228 77.745561 \n",
       "L 185.958297 77.117699 \n",
       "L 186.920366 77.176888 \n",
       "L 188.844504 77.574764 \n",
       "L 189.806573 77.517542 \n",
       "L 192.69278 77.615644 \n",
       "L 192.69278 78.009041 \n",
       "L 193.654849 78.209144 \n",
       "L 196.541056 77.921415 \n",
       "L 198.465194 77.737139 \n",
       "L 200.389332 78.146646 \n",
       "L 201.351401 78.171737 \n",
       "L 202.31347 77.867545 \n",
       "L 204.237608 77.646325 \n",
       "L 205.199677 77.63249 \n",
       "L 206.161746 78.332294 \n",
       "L 207.123815 78.82259 \n",
       "L 208.085884 78.361249 \n",
       "L 209.047953 78.081427 \n",
       "L 210.972091 77.821529 \n",
       "L 212.896228 77.675431 \n",
       "L 212.896228 77.979671 \n",
       "L 213.858297 77.475681 \n",
       "L 214.820366 77.553884 \n",
       "L 215.782435 77.74666 \n",
       "L 218.668642 77.516596 \n",
       "L 219.630711 77.680477 \n",
       "L 220.59278 77.985864 \n",
       "L 221.554849 77.568918 \n",
       "L 223.478987 77.808753 \n",
       "L 224.441056 77.879181 \n",
       "L 225.403125 77.778954 \n",
       "L 225.403125 77.778954 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_26\">\n",
       "    <path d=\"M 24.330711 146.899219 \n",
       "L 51.268642 146.816272 \n",
       "L 51.268642 146.700146 \n",
       "L 55.116918 146.660332 \n",
       "L 57.041056 146.548472 \n",
       "L 58.003125 146.567432 \n",
       "L 59.927263 146.191406 \n",
       "L 61.851401 146.156016 \n",
       "L 63.775539 146.046052 \n",
       "L 64.737608 145.439355 \n",
       "L 65.699677 145.937036 \n",
       "L 68.585884 145.598613 \n",
       "L 71.472091 145.414471 \n",
       "L 71.472091 144.510352 \n",
       "L 72.434159 144.410815 \n",
       "L 73.396228 144.620947 \n",
       "L 77.244504 144.718903 \n",
       "L 78.206573 144.244922 \n",
       "L 79.168642 144.178564 \n",
       "L 80.130711 143.935254 \n",
       "L 82.054849 144.125479 \n",
       "L 83.978987 144.083768 \n",
       "L 84.941056 143.315918 \n",
       "L 85.903125 143.249561 \n",
       "L 86.865194 143.382275 \n",
       "L 87.827263 143.216382 \n",
       "L 89.751401 143.371216 \n",
       "L 91.675539 143.349097 \n",
       "L 91.675539 142.718701 \n",
       "L 92.637608 142.48645 \n",
       "L 93.599677 142.718701 \n",
       "L 97.447953 142.813497 \n",
       "L 98.410022 142.121484 \n",
       "L 99.372091 142.254199 \n",
       "L 100.334159 142.033008 \n",
       "L 103.220366 141.72334 \n",
       "L 104.182435 141.789697 \n",
       "L 105.144504 142.453271 \n",
       "L 107.068642 141.19248 \n",
       "L 108.030711 140.960229 \n",
       "L 108.99278 140.542178 \n",
       "L 109.954849 140.484668 \n",
       "L 110.916918 140.291915 \n",
       "L 111.878987 140.213708 \n",
       "L 111.878987 138.670898 \n",
       "L 112.841056 138.438647 \n",
       "L 113.803125 138.670898 \n",
       "L 114.765194 138.305933 \n",
       "L 115.727263 138.339111 \n",
       "L 117.651401 137.884089 \n",
       "L 118.61347 136.680176 \n",
       "L 120.537608 135.861768 \n",
       "L 121.499677 135.734583 \n",
       "L 124.385884 134.310268 \n",
       "L 125.347953 133.163232 \n",
       "L 126.310022 132.831445 \n",
       "L 127.272091 131.570654 \n",
       "L 129.196228 130.429307 \n",
       "L 132.082435 129.273029 \n",
       "L 132.082435 127.589209 \n",
       "L 133.044504 126.02981 \n",
       "L 134.006573 125.399414 \n",
       "L 134.968642 125.333057 \n",
       "L 136.89278 124.238159 \n",
       "L 137.854849 123.806836 \n",
       "L 138.816918 120.223535 \n",
       "L 139.778987 120.123999 \n",
       "L 140.741056 120.289893 \n",
       "L 142.665194 120.223535 \n",
       "L 143.627263 119.526782 \n",
       "L 144.589332 119.123898 \n",
       "L 145.551401 118.232812 \n",
       "L 146.51347 116.50752 \n",
       "L 148.437608 115.744409 \n",
       "L 149.399677 115.97666 \n",
       "L 150.361746 115.412622 \n",
       "L 152.285884 114.732458 \n",
       "L 152.285884 114.18501 \n",
       "L 154.210022 111.796143 \n",
       "L 155.172091 111.132568 \n",
       "L 156.134159 110.986582 \n",
       "L 158.058297 110.241483 \n",
       "L 159.020366 108.212842 \n",
       "L 160.944504 108.102246 \n",
       "L 162.868642 107.190937 \n",
       "L 163.830711 106.896753 \n",
       "L 164.79278 106.771938 \n",
       "L 165.754849 105.624902 \n",
       "L 166.716918 106.18894 \n",
       "L 167.678987 105.403711 \n",
       "L 168.641056 105.525366 \n",
       "L 169.603125 105.319658 \n",
       "L 170.565194 105.602783 \n",
       "L 171.527263 105.283636 \n",
       "L 172.489332 105.152106 \n",
       "L 172.489332 103.236035 \n",
       "L 173.451401 102.539282 \n",
       "L 174.41347 103.523584 \n",
       "L 175.375539 103.567822 \n",
       "L 177.299677 103.435107 \n",
       "L 178.261746 103.36875 \n",
       "L 179.223815 103.63418 \n",
       "L 180.185884 102.174316 \n",
       "L 181.147953 102.572461 \n",
       "L 183.072091 102.267217 \n",
       "L 184.034159 102.008423 \n",
       "L 184.996228 101.615018 \n",
       "L 185.958297 100.913525 \n",
       "L 186.920366 101.079419 \n",
       "L 187.882435 100.648096 \n",
       "L 188.844504 100.050879 \n",
       "L 189.806573 99.586377 \n",
       "L 190.768642 98.933862 \n",
       "L 191.730711 98.638414 \n",
       "L 192.69278 98.018683 \n",
       "L 192.69278 97.794727 \n",
       "L 193.654849 96.135791 \n",
       "L 194.616918 95.206787 \n",
       "L 195.578987 94.510034 \n",
       "L 196.541056 94.105254 \n",
       "L 197.503125 93.968115 \n",
       "L 198.465194 93.490974 \n",
       "L 199.427263 90.561768 \n",
       "L 200.389332 89.931372 \n",
       "L 201.351401 89.898193 \n",
       "L 202.31347 89.383923 \n",
       "L 203.275539 89.500049 \n",
       "L 204.237608 89.765479 \n",
       "L 205.199677 89.793917 \n",
       "L 206.161746 85.784033 \n",
       "L 207.123815 85.153638 \n",
       "L 208.085884 86.160059 \n",
       "L 209.047953 86.613501 \n",
       "L 210.010022 86.805937 \n",
       "L 210.972091 86.690918 \n",
       "L 211.934159 87.111182 \n",
       "L 212.896228 87.136066 \n",
       "L 212.896228 88.239258 \n",
       "L 214.820366 87.066943 \n",
       "L 215.782435 87.40979 \n",
       "L 216.744504 87.323525 \n",
       "L 217.706573 87.055884 \n",
       "L 218.668642 86.6372 \n",
       "L 219.630711 82.797949 \n",
       "L 220.59278 82.632056 \n",
       "L 221.554849 84.257812 \n",
       "L 222.516918 83.975793 \n",
       "L 223.478987 84.297627 \n",
       "L 224.441056 84.445825 \n",
       "L 225.403125 84.82659 \n",
       "L 225.403125 84.82659 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_27\">\n",
       "    <path d=\"M 30.103125 146.899219 \n",
       "L 36.837608 146.899219 \n",
       "L 43.572091 146.899219 \n",
       "L 50.306573 146.832861 \n",
       "L 57.041056 146.567432 \n",
       "L 63.775539 145.704785 \n",
       "L 70.510022 145.140747 \n",
       "L 77.244504 144.908496 \n",
       "L 83.978987 144.410815 \n",
       "L 90.71347 143.51499 \n",
       "L 97.447953 143.249561 \n",
       "L 104.182435 142.187842 \n",
       "L 110.916918 140.230298 \n",
       "L 117.651401 138.206396 \n",
       "L 124.385884 134.822168 \n",
       "L 131.120366 128.186426 \n",
       "L 137.854849 124.603125 \n",
       "L 144.589332 118.03374 \n",
       "L 151.323815 114.450439 \n",
       "L 158.058297 109.340918 \n",
       "L 164.79278 106.819336 \n",
       "L 171.527263 103.965967 \n",
       "L 178.261746 102.174316 \n",
       "L 184.996228 98.557837 \n",
       "L 191.730711 93.547852 \n",
       "L 198.465194 89.533228 \n",
       "L 205.199677 87.044824 \n",
       "L 211.934159 86.447607 \n",
       "L 218.668642 83.527881 \n",
       "L 225.403125 84.855029 \n",
       "\" clip-path=\"url(#pfa58519342)\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 30.103125 146.899219 \n",
       "L 30.103125 10.999219 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 225.403125 146.899219 \n",
       "L 225.403125 10.999219 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 30.103125 146.899219 \n",
       "L 225.403125 146.899219 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 30.103125 10.999219 \n",
       "L 225.403125 10.999219 \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_7\">\n",
       "     <path d=\"M 140.634375 63.033594 \n",
       "L 218.403125 63.033594 \n",
       "Q 220.403125 63.033594 220.403125 61.033594 \n",
       "L 220.403125 17.999219 \n",
       "Q 220.403125 15.999219 218.403125 15.999219 \n",
       "L 140.634375 15.999219 \n",
       "Q 138.634375 15.999219 138.634375 17.999219 \n",
       "L 138.634375 61.033594 \n",
       "Q 138.634375 63.033594 140.634375 63.033594 \n",
       "z\n",
       "\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_28\">\n",
       "     <path d=\"M 142.634375 24.097656 \n",
       "L 152.634375 24.097656 \n",
       "L 162.634375 24.097656 \n",
       "\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
       "    </g>\n",
       "    <g id=\"text_14\">\n",
       "     <!-- train loss -->\n",
       "     <g transform=\"translate(170.634375 27.597656)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \n",
       "L 1172 3500 \n",
       "L 2356 3500 \n",
       "L 2356 3053 \n",
       "L 1172 3053 \n",
       "L 1172 1153 \n",
       "Q 1172 725 1289 603 \n",
       "Q 1406 481 1766 481 \n",
       "L 2356 481 \n",
       "L 2356 0 \n",
       "L 1766 0 \n",
       "Q 1100 0 847 248 \n",
       "Q 594 497 594 1153 \n",
       "L 594 3053 \n",
       "L 172 3053 \n",
       "L 172 3500 \n",
       "L 594 3500 \n",
       "L 594 4494 \n",
       "L 1172 4494 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \n",
       "Q 2534 3019 2420 3045 \n",
       "Q 2306 3072 2169 3072 \n",
       "Q 1681 3072 1420 2755 \n",
       "Q 1159 2438 1159 1844 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1341 3275 1631 3429 \n",
       "Q 1922 3584 2338 3584 \n",
       "Q 2397 3584 2469 3576 \n",
       "Q 2541 3569 2628 3553 \n",
       "L 2631 2963 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \n",
       "Q 1497 1759 1228 1600 \n",
       "Q 959 1441 959 1056 \n",
       "Q 959 750 1161 570 \n",
       "Q 1363 391 1709 391 \n",
       "Q 2188 391 2477 730 \n",
       "Q 2766 1069 2766 1631 \n",
       "L 2766 1759 \n",
       "L 2194 1759 \n",
       "z\n",
       "M 3341 1997 \n",
       "L 3341 0 \n",
       "L 2766 0 \n",
       "L 2766 531 \n",
       "Q 2569 213 2275 61 \n",
       "Q 1981 -91 1556 -91 \n",
       "Q 1019 -91 701 211 \n",
       "Q 384 513 384 1019 \n",
       "Q 384 1609 779 1909 \n",
       "Q 1175 2209 1959 2209 \n",
       "L 2766 2209 \n",
       "L 2766 2266 \n",
       "Q 2766 2663 2505 2880 \n",
       "Q 2244 3097 1772 3097 \n",
       "Q 1472 3097 1187 3025 \n",
       "Q 903 2953 641 2809 \n",
       "L 641 3341 \n",
       "Q 956 3463 1253 3523 \n",
       "Q 1550 3584 1831 3584 \n",
       "Q 2591 3584 2966 3190 \n",
       "Q 3341 2797 3341 1997 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \n",
       "L 1178 3500 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 3500 \n",
       "z\n",
       "M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 4134 \n",
       "L 603 4134 \n",
       "L 603 4863 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 4863 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \n",
       "L 2834 2853 \n",
       "Q 2591 2978 2328 3040 \n",
       "Q 2066 3103 1784 3103 \n",
       "Q 1356 3103 1142 2972 \n",
       "Q 928 2841 928 2578 \n",
       "Q 928 2378 1081 2264 \n",
       "Q 1234 2150 1697 2047 \n",
       "L 1894 2003 \n",
       "Q 2506 1872 2764 1633 \n",
       "Q 3022 1394 3022 966 \n",
       "Q 3022 478 2636 193 \n",
       "Q 2250 -91 1575 -91 \n",
       "Q 1294 -91 989 -36 \n",
       "Q 684 19 347 128 \n",
       "L 347 722 \n",
       "Q 666 556 975 473 \n",
       "Q 1284 391 1588 391 \n",
       "Q 1994 391 2212 530 \n",
       "Q 2431 669 2431 922 \n",
       "Q 2431 1156 2273 1281 \n",
       "Q 2116 1406 1581 1522 \n",
       "L 1381 1569 \n",
       "Q 847 1681 609 1914 \n",
       "Q 372 2147 372 2553 \n",
       "Q 372 3047 722 3315 \n",
       "Q 1072 3584 1716 3584 \n",
       "Q 2034 3584 2315 3537 \n",
       "Q 2597 3491 2834 3397 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-74\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-72\" x=\"39.208984\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"80.322266\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-69\" x=\"141.601562\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6e\" x=\"169.384766\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"232.763672\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6c\" x=\"264.550781\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6f\" x=\"292.333984\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"353.515625\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"405.615234\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_29\">\n",
       "     <path d=\"M 142.634375 38.775781 \n",
       "L 152.634375 38.775781 \n",
       "L 162.634375 38.775781 \n",
       "\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n",
       "    </g>\n",
       "    <g id=\"text_15\">\n",
       "     <!-- train acc -->\n",
       "     <g transform=\"translate(170.634375 42.275781)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-74\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-72\" x=\"39.208984\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"80.322266\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-69\" x=\"141.601562\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6e\" x=\"169.384766\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"232.763672\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"264.550781\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-63\" x=\"325.830078\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-63\" x=\"380.810547\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_30\">\n",
       "     <path d=\"M 142.634375 53.453906 \n",
       "L 152.634375 53.453906 \n",
       "L 162.634375 53.453906 \n",
       "\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n",
       "    </g>\n",
       "    <g id=\"text_16\">\n",
       "     <!-- test acc -->\n",
       "     <g transform=\"translate(170.634375 56.953906)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-74\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-65\" x=\"39.208984\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"100.732422\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-74\" x=\"152.832031\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"192.041016\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"223.828125\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-63\" x=\"285.107422\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-63\" x=\"340.087891\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"pfa58519342\">\n",
       "   <rect x=\"30.103125\" y=\"10.999219\" width=\"195.3\" height=\"135.9\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "batch_size = 2048\n",
    "data_dir = d2l.download_extract('ctr')\n",
    "train_data = d2l.CTRDataset(os.path.join(data_dir, 'train.csv'))\n",
    "test_data = d2l.CTRDataset(os.path.join(data_dir, 'test.csv'),\n",
    "                           feat_mapper=train_data.feat_mapper,\n",
    "                           defaults=train_data.defaults)\n",
    "field_dims = train_data.field_dims\n",
    "train_iter = gluon.data.DataLoader(\n",
    "    train_data, shuffle=True, last_batch='rollover', batch_size=batch_size,\n",
    "    num_workers=d2l.get_dataloader_workers())\n",
    "test_iter = gluon.data.DataLoader(\n",
    "    test_data, shuffle=False, last_batch='rollover', batch_size=batch_size,\n",
    "    num_workers=d2l.get_dataloader_workers())\n",
    "devices = d2l.try_all_gpus()\n",
    "net = DeepFM(field_dims, num_factors=10, mlp_dims=[30, 20, 10])\n",
    "net.initialize(init.Xavier(), ctx=devices)\n",
    "lr, num_epochs, optimizer = 0.01, 30, 'adam'\n",
    "trainer = gluon.Trainer(net.collect_params(), optimizer,\n",
    "                        {'learning_rate': lr})\n",
    "loss = gluon.loss.SigmoidBinaryCrossEntropyLoss()\n",
    "d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 6
   },
   "source": [
    "Compared with FM, DeepFM converges faster and achieves better performance.\n",
    "\n",
    "## Summary\n",
    "\n",
    "* Integrating neural networks to FM enables it to model complex and high-order interactions.\n",
    "* DeepFM outperforms the original FM on the advertising dataset.\n",
    "\n",
    "## Exercises\n",
    "\n",
    "* Vary the structure of the MLP to check its impact on model performance.\n",
    "* Change the dataset to Criteo and compare it with the original FM model.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 7,
    "tab": [
     "mxnet"
    ]
   },
   "source": [
    "[Discussions](https://discuss.d2l.ai/t/407)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}