{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2.0.8'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import keras\n",
    "keras.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overfitting and underfitting\n",
    "\n",
    "This notebook contains the code samples found in Chapter 3, Section 6 of [Deep Learning with Python](https://www.manning.com/books/deep-learning-with-python?a_aid=keras&a_bid=76564dff). Note that the original text features far more content, in particular further explanations and figures: in this notebook, you will only find source code and related comments.\n",
    "\n",
    "----\n",
    "\n",
    "\n",
    "In all the examples we saw in the previous chapter -- movie review sentiment prediction, topic classification, and house price regression -- \n",
    "we could notice that the performance of our model on the held-out validation data would always peak after a few epochs and would then start \n",
    "degrading, i.e. our model would quickly start to _overfit_ to the training data. Overfitting happens in every single machine learning \n",
    "problem. Learning how to deal with overfitting is essential to mastering machine learning.\n",
    "\n",
    "The fundamental issue in machine learning is the tension between optimization and generalization. \"Optimization\" refers to the process of \n",
    "adjusting a model to get the best performance possible on the training data (the \"learning\" in \"machine learning\"), while \"generalization\" \n",
    "refers to how well the trained model would perform on data it has never seen before. The goal of the game is to get good generalization, of \n",
    "course, but you do not control generalization; you can only adjust the model based on its training data.\n",
    "\n",
    "At the beginning of training, optimization and generalization are correlated: the lower your loss on training data, the lower your loss on \n",
    "test data. While this is happening, your model is said to be _under-fit_: there is still progress to be made; the network hasn't yet \n",
    "modeled all relevant patterns in the training data. But after a certain number of iterations on the training data, generalization stops \n",
    "improving, validation metrics stall then start degrading: the model is then starting to over-fit, i.e. is it starting to learn patterns \n",
    "that are specific to the training data but that are misleading or irrelevant when it comes to new data.\n",
    "\n",
    "To prevent a model from learning misleading or irrelevant patterns found in the training data, _the best solution is of course to get \n",
    "more training data_. A model trained on more data will naturally generalize better. When that is no longer possible, the next best solution \n",
    "is to modulate the quantity of information that your model is allowed to store, or to add constraints on what information it is allowed to \n",
    "store. If a network can only afford to memorize a small number of patterns, the optimization process will force it to focus on the most \n",
    "prominent patterns, which have a better chance of generalizing well.\n",
    "\n",
    "The processing of fighting overfitting in this way is called _regularization_. Let's review some of the most common regularization \n",
    "techniques, and let's apply them in practice to improve our movie classification model from  the previous chapter."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: in this notebook we will be using the IMDB test set as our validation set. It doesn't matter in this context.\n",
    "\n",
    "Let's prepare the data using the code from Chapter 3, Section 5:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.datasets import imdb\n",
    "import numpy as np\n",
    "\n",
    "(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)\n",
    "\n",
    "def vectorize_sequences(sequences, dimension=10000):\n",
    "    # Create an all-zero matrix of shape (len(sequences), dimension)\n",
    "    results = np.zeros((len(sequences), dimension))\n",
    "    for i, sequence in enumerate(sequences):\n",
    "        results[i, sequence] = 1.  # set specific indices of results[i] to 1s\n",
    "    return results\n",
    "\n",
    "# Our vectorized training data\n",
    "x_train = vectorize_sequences(train_data)\n",
    "# Our vectorized test data\n",
    "x_test = vectorize_sequences(test_data)\n",
    "# Our vectorized labels\n",
    "y_train = np.asarray(train_labels).astype('float32')\n",
    "y_test = np.asarray(test_labels).astype('float32')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fighting overfitting\n",
    "\n",
    "## Reducing the network's size\n",
    "\n",
    "\n",
    "The simplest way to prevent overfitting is to reduce the size of the model, i.e. the number of learnable parameters in the model (which is \n",
    "determined by the number of layers and the number of units per layer). In deep learning, the number of learnable parameters in a model is \n",
    "often referred to as the model's \"capacity\". Intuitively, a model with more parameters will have more \"memorization capacity\" and therefore \n",
    "will be able to easily learn a perfect dictionary-like mapping between training samples and their targets, a mapping without any \n",
    "generalization power. For instance, a model with 500,000 binary parameters could easily be made to learn the class of every digits in the \n",
    "MNIST training set: we would only need 10 binary parameters for each of the 50,000 digits. Such a model would be useless for classifying \n",
    "new digit samples. Always keep this in mind: deep learning models tend to be good at fitting to the training data, but the real challenge \n",
    "is generalization, not fitting.\n",
    "\n",
    "On the other hand, if the network has limited memorization resources, it will not be able to learn this mapping as easily, and thus, in \n",
    "order to minimize its loss, it will have to resort to learning compressed representations that have predictive power regarding the targets \n",
    "-- precisely the type of representations that we are interested in. At the same time, keep in mind that you should be using models that have \n",
    "enough parameters that they won't be underfitting: your model shouldn't be starved for memorization resources. There is a compromise to be \n",
    "found between \"too much capacity\" and \"not enough capacity\".\n",
    "\n",
    "Unfortunately, there is no magical formula to determine what the right number of layers is, or what the right size for each layer is. You \n",
    "will have to evaluate an array of different architectures (on your validation set, not on your test set, of course) in order to find the \n",
    "right model size for your data. The general workflow to find an appropriate model size is to start with relatively few layers and \n",
    "parameters, and start increasing the size of the layers or adding new layers until you see diminishing returns with regard to the \n",
    "validation loss.\n",
    "\n",
    "Let's try this on our movie review classification network. Our original network was as such:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras import models\n",
    "from keras import layers\n",
    "\n",
    "original_model = models.Sequential()\n",
    "original_model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))\n",
    "original_model.add(layers.Dense(16, activation='relu'))\n",
    "original_model.add(layers.Dense(1, activation='sigmoid'))\n",
    "\n",
    "original_model.compile(optimizer='rmsprop',\n",
    "                       loss='binary_crossentropy',\n",
    "                       metrics=['acc'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's try to replace it with this smaller network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "smaller_model = models.Sequential()\n",
    "smaller_model.add(layers.Dense(4, activation='relu', input_shape=(10000,)))\n",
    "smaller_model.add(layers.Dense(4, activation='relu'))\n",
    "smaller_model.add(layers.Dense(1, activation='sigmoid'))\n",
    "\n",
    "smaller_model.compile(optimizer='rmsprop',\n",
    "                      loss='binary_crossentropy',\n",
    "                      metrics=['acc'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Here's a comparison of the validation losses of the original network and the smaller network. The dots are the validation loss values of \n",
    "the smaller network, and the crosses are the initial network (remember: a lower validation loss signals a better model)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.4594 - acc: 0.8188 - val_loss: 0.3429 - val_acc: 0.8812\n",
      "Epoch 2/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2659 - acc: 0.9075 - val_loss: 0.2873 - val_acc: 0.8905\n",
      "Epoch 3/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2060 - acc: 0.9276 - val_loss: 0.2827 - val_acc: 0.8879\n",
      "Epoch 4/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1697 - acc: 0.9401 - val_loss: 0.2921 - val_acc: 0.8851\n",
      "Epoch 5/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1495 - acc: 0.9470 - val_loss: 0.3100 - val_acc: 0.8812\n",
      "Epoch 6/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1283 - acc: 0.9572 - val_loss: 0.3336 - val_acc: 0.8748\n",
      "Epoch 7/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1121 - acc: 0.9624 - val_loss: 0.3987 - val_acc: 0.8593\n",
      "Epoch 8/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0994 - acc: 0.9670 - val_loss: 0.3788 - val_acc: 0.8702\n",
      "Epoch 9/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0889 - acc: 0.9716 - val_loss: 0.4242 - val_acc: 0.8603\n",
      "Epoch 10/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0782 - acc: 0.9757 - val_loss: 0.4256 - val_acc: 0.8653\n",
      "Epoch 11/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0691 - acc: 0.9792 - val_loss: 0.4515 - val_acc: 0.8638\n",
      "Epoch 12/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0603 - acc: 0.9820 - val_loss: 0.5102 - val_acc: 0.8610\n",
      "Epoch 13/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0518 - acc: 0.9851 - val_loss: 0.5281 - val_acc: 0.8587\n",
      "Epoch 14/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0446 - acc: 0.9873 - val_loss: 0.5441 - val_acc: 0.8589\n",
      "Epoch 15/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0367 - acc: 0.9903 - val_loss: 0.5777 - val_acc: 0.8574\n",
      "Epoch 16/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0313 - acc: 0.9922 - val_loss: 0.6377 - val_acc: 0.8555\n",
      "Epoch 17/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0247 - acc: 0.9941 - val_loss: 0.7269 - val_acc: 0.8501\n",
      "Epoch 18/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0203 - acc: 0.9956 - val_loss: 0.6920 - val_acc: 0.8516\n",
      "Epoch 19/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0156 - acc: 0.9970 - val_loss: 0.7689 - val_acc: 0.8425\n",
      "Epoch 20/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0144 - acc: 0.9966 - val_loss: 0.7694 - val_acc: 0.8487\n"
     ]
    }
   ],
   "source": [
    "original_hist = original_model.fit(x_train, y_train,\n",
    "                                   epochs=20,\n",
    "                                   batch_size=512,\n",
    "                                   validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.5737 - acc: 0.8049 - val_loss: 0.4826 - val_acc: 0.8616\n",
      "Epoch 2/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.3973 - acc: 0.8866 - val_loss: 0.3699 - val_acc: 0.8776\n",
      "Epoch 3/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2985 - acc: 0.9054 - val_loss: 0.3140 - val_acc: 0.8860\n",
      "Epoch 4/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2428 - acc: 0.9189 - val_loss: 0.2913 - val_acc: 0.8870\n",
      "Epoch 5/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2085 - acc: 0.9290 - val_loss: 0.2809 - val_acc: 0.8897\n",
      "Epoch 6/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1849 - acc: 0.9360 - val_loss: 0.2772 - val_acc: 0.8899\n",
      "Epoch 7/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1666 - acc: 0.9430 - val_loss: 0.2835 - val_acc: 0.8863\n",
      "Epoch 8/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1515 - acc: 0.9487 - val_loss: 0.2909 - val_acc: 0.8850\n",
      "Epoch 9/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1388 - acc: 0.9526 - val_loss: 0.2984 - val_acc: 0.8842\n",
      "Epoch 10/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1285 - acc: 0.9569 - val_loss: 0.3102 - val_acc: 0.8818\n",
      "Epoch 11/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1194 - acc: 0.9599 - val_loss: 0.3219 - val_acc: 0.8794\n",
      "Epoch 12/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1105 - acc: 0.9648 - val_loss: 0.3379 - val_acc: 0.8774\n",
      "Epoch 13/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1035 - acc: 0.9674 - val_loss: 0.3532 - val_acc: 0.8730\n",
      "Epoch 14/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0963 - acc: 0.9688 - val_loss: 0.3651 - val_acc: 0.8731\n",
      "Epoch 15/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0895 - acc: 0.9724 - val_loss: 0.3858 - val_acc: 0.8703\n",
      "Epoch 16/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0838 - acc: 0.9734 - val_loss: 0.4157 - val_acc: 0.8654\n",
      "Epoch 17/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0785 - acc: 0.9764 - val_loss: 0.4214 - val_acc: 0.8677\n",
      "Epoch 18/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0733 - acc: 0.9784 - val_loss: 0.4390 - val_acc: 0.8644\n",
      "Epoch 19/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0685 - acc: 0.9796 - val_loss: 0.4539 - val_acc: 0.8638\n",
      "Epoch 20/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.0638 - acc: 0.9822 - val_loss: 0.4744 - val_acc: 0.8617\n"
     ]
    }
   ],
   "source": [
    "smaller_model_hist = smaller_model.fit(x_train, y_train,\n",
    "                                       epochs=20,\n",
    "                                       batch_size=512,\n",
    "                                       validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "epochs = range(1, 21)\n",
    "original_val_loss = original_hist.history['val_loss']\n",
    "smaller_model_val_loss = smaller_model_hist.history['val_loss']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz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u7+adJrlBPMez8o3L8R6OLu3zez3wP1BLPBzvVwMkCROKikIBKN\nN/O4LT5NeryXfW16/0wwF9UIoDqcuVMkFkoKItFcmvTztfDxnwlm9QSYBPwpfPxHYCqAmXUys0Oa\ne1EzKwH6uft84EbgEOCA0opIrugvEpF9Pmf7L97+e3dv7JZ6mJmtIPhr//Lw2DXAI2Z2A1AHTAmP\nTwceNLNvEZQIphLM0JlKJ+CxMHEYcI+7b83YbyTSRmpTEGlF2KZQ6e4fxh2LSLap+khERBJUUhAR\nkQSVFEREJEFJQUREEpQUREQkQUlBREQSlBRERCRBSUFERBL+Py+ECKSeVveDAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f43c33d57f0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# b+ is for \"blue cross\"\n",
    "plt.plot(epochs, original_val_loss, 'b+', label='Original model')\n",
    "# \"bo\" is for \"blue dot\"\n",
    "plt.plot(epochs, smaller_model_val_loss, 'bo', label='Smaller model')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Validation loss')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "As you can see, the smaller network starts overfitting later than the reference one (after 6 epochs rather than 4) and its performance \n",
    "degrades much more slowly once it starts overfitting.\n",
    "\n",
    "Now, for kicks, let's add to this benchmark a network that has much more capacity, far more than the problem would warrant:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "bigger_model = models.Sequential()\n",
    "bigger_model.add(layers.Dense(512, activation='relu', input_shape=(10000,)))\n",
    "bigger_model.add(layers.Dense(512, activation='relu'))\n",
    "bigger_model.add(layers.Dense(1, activation='sigmoid'))\n",
    "\n",
    "bigger_model.compile(optimizer='rmsprop',\n",
    "                     loss='binary_crossentropy',\n",
    "                     metrics=['acc'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.4539 - acc: 0.8011 - val_loss: 0.4150 - val_acc: 0.8229\n",
      "Epoch 2/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.2148 - acc: 0.9151 - val_loss: 0.2742 - val_acc: 0.8901\n",
      "Epoch 3/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.1217 - acc: 0.9544 - val_loss: 0.5442 - val_acc: 0.7975\n",
      "Epoch 4/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0552 - acc: 0.9835 - val_loss: 0.4316 - val_acc: 0.8842\n",
      "Epoch 5/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0662 - acc: 0.9888 - val_loss: 0.5098 - val_acc: 0.8822\n",
      "Epoch 6/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0017 - acc: 0.9998 - val_loss: 0.6867 - val_acc: 0.8811\n",
      "Epoch 7/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.1019 - acc: 0.9882 - val_loss: 0.6737 - val_acc: 0.8800\n",
      "Epoch 8/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0735 - acc: 0.9896 - val_loss: 0.6185 - val_acc: 0.8772\n",
      "Epoch 9/20\n",
      "25000/25000 [==============================] - 3s - loss: 3.4759e-04 - acc: 1.0000 - val_loss: 0.7328 - val_acc: 0.8818\n",
      "Epoch 10/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0504 - acc: 0.9912 - val_loss: 0.7092 - val_acc: 0.8791\n",
      "Epoch 11/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0589 - acc: 0.9919 - val_loss: 0.6831 - val_acc: 0.8785\n",
      "Epoch 12/20\n",
      "25000/25000 [==============================] - 3s - loss: 1.9067e-04 - acc: 1.0000 - val_loss: 0.8005 - val_acc: 0.8784\n",
      "Epoch 13/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0623 - acc: 0.9916 - val_loss: 0.7540 - val_acc: 0.8740\n",
      "Epoch 14/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0274 - acc: 0.9954 - val_loss: 0.7806 - val_acc: 0.8670\n",
      "Epoch 15/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0011 - acc: 0.9998 - val_loss: 0.8107 - val_acc: 0.8783\n",
      "Epoch 16/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0445 - acc: 0.9943 - val_loss: 0.8394 - val_acc: 0.8702\n",
      "Epoch 17/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0268 - acc: 0.9959 - val_loss: 0.7708 - val_acc: 0.8745\n",
      "Epoch 18/20\n",
      "25000/25000 [==============================] - 3s - loss: 7.7057e-04 - acc: 1.0000 - val_loss: 0.8885 - val_acc: 0.8738\n",
      "Epoch 19/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0297 - acc: 0.9962 - val_loss: 0.8419 - val_acc: 0.8728\n",
      "Epoch 20/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.0018 - acc: 0.9998 - val_loss: 0.8896 - val_acc: 0.8682\n"
     ]
    }
   ],
   "source": [
    "bigger_model_hist = bigger_model.fit(x_train, y_train,\n",
    "                                     epochs=20,\n",
    "                                     batch_size=512,\n",
    "                                     validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's how the bigger network fares compared to the reference one. The dots are the validation loss values of the bigger network, and the \n",
    "crosses are the initial network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+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7OTqVOncv/99wOwdu1aVq9endF7\nv//++wwZMoTdu3fTmKM5cpMnT2b58uVs3bqVPXv2cN9993HyySdz4oknsnz5crZt28bu3bt54IEH\n4s+ZNm0at912W/x41apVOYlFRPKropPCggW5eR0z49e//jUPPPAAI0aM4Nhjj6VPnz78y7/8S8rn\nXn755bS2tlJbW8s111zD6NGjGTBgQNrvfcMNN3DiiScydepURo4cmc0/I27IkCF8//vf59RTT2Xc\nuHFMmDCBuro6hgwZQkNDA1OmTGHq1KmMGjUq/pxbb72VlStXMnbsWGpra/nZz36Wk1hEJL/Mc9F/\nkkcTJ070zpvsrFu3rsMJqpTs2bOH3bt306dPH1577TU++9nP8uqrr7L//vsXOrTIlfLvTaTUmNnz\n7j4xVb2KG1MoNm1tbZx66qns3r0bd+eOO+6oiIQgIsVJSaHADj74YG0vKiJFo6LHFEREpCMlBRER\niVNSEJGiV8ilq3OhlOLXmIKIFLXY0tWxZb1iS1dDaVxQVmrxq6WQI1VVVRx//PGMGzeO8ePH86c/\n/QlIf5nsYnbQQQflpI5ITxR66epslVr8FZkUomjKHXjggaxatYoXX3yR733ve1x99dUAaS+TnY32\n9vZIX1+kkAq9dHW2Si3+iksK+dhFafv27QwcOBAg7WWy77rrrviS1Jdeeml8Y5qulqRuaGjgoosu\nYurUqVx00UUd3v+pp57i5JNPpq6ujqOPPpp58+bR2NjI5MmTGTNmDK+99lo8ttNOO42xY8dy+umn\nszn8lr7xxhtMmTKFMWPGcM0113R47ZtuuolJkyYxduxYFuTqknCRbhR66epslVz87l5StwkTJnhn\na9eu3aesK9XV7kE66Hirrk77JZLq1auXjxs3zj/xiU94//79feXKle7u/sYbb/jo0aPd3f2mm27y\n2bNnu7v76tWrvaqqypuamnzLli1eXV3t27Zt8127dvmnPvUpv+KKK9zdfebMmf7HP/7R3d03bdrk\nI0eOdHf3BQsW+Pjx472trW2fWJ588kkfMGCAv/XWW75z504/4ogj/Nprr3V395/85Cc+d+5cd3c/\n55xzfMmSJe7uftddd3ld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8/6IaUS/XG/ApgmbcS8Cq\n8HY28C3gW2GdOcAagpkUzwKfzGN8R4fv+2IYw/ywPDE+A24nmPWxGpiY58+wH8FJfkBCWUE/P4IE\n9Tawm6Bf9uvAocDvgQ3A48CgsO5E4OcJz/0asDG8XZKn2DYS9CXHvoM/C+seATzS3Xchj5/fv4Xf\nr5cITnBDOscYHp9NMOPmtahiTBZfWL4k9r1LqJvXz7Cbc0pBvn+6ollEROLUfSQiInFKCiIiEqek\nICIicUoKIiISp6QgIiJxSgoiITPbYx1XcM3Zip1mVpO4QqdIsdqv0AGIFJEd7n58oYMQKSS1FERS\nCNfT/0G4pv6fzezjYXmNmT0RLvj2ezMbFpYfbsEeBy+Gt0+GL1VlZneGa+Y/ZmYHhvX/IVxL/yUz\nW1qgf6YIoKQgkujATt1HX0l47O/uPgb4KfCTsOw24B53H0uwIN2tYfmtwHIPFvQbT3AlLMAI4HZ3\nHw28B1wQls8DTghf51tR/eNE0qErmkVCZvaBux+UpLwZOM3dXw8XLvuLux9qZlsJlm7YHZa/7e6H\nmVkrMNTdP0x4jRqCde9HhMdXAb3d/X+b2W+BDwhWg33Yw8UARQpBLQWR9HgX9zPxYcL9PXw0pvd5\ngrWoxgNN4cqdIgWhpCCSnq8k/HwmvP8nglU9AeqBP4b3fw9cBmBmVWY2oKsXNbNewFHu/iRwFTAA\n2Ke1IpIv+otE5CMHWsfN23/r7rFpqQPN7CWCv/ZnhmXfBu42s38CWoFLwvK5wCIz+zpBi+AyghU6\nk6kC7g0ThwG3uvt7OfsXiWRIYwoiKYRjChPdfWuhYxGJmrqPREQkTi0FERGJU0tBRETilBRERCRO\nSUFEROKUFEREJE5JQURE4pQUREQk7v8DxBgqjYrXWkoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f43c0fbcba8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bigger_model_val_loss = bigger_model_hist.history['val_loss']\n",
    "\n",
    "plt.plot(epochs, original_val_loss, 'b+', label='Original model')\n",
    "plt.plot(epochs, bigger_model_val_loss, 'bo', label='Bigger model')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Validation loss')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The bigger network starts overfitting almost right away, after just one epoch, and overfits much more severely. Its validation loss is also \n",
    "more noisy.\n",
    "\n",
    "Meanwhile, here are the training losses for our two networks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XucFPWZ7/HPw4giiorKy4MiM2gwAnIRBpCg8YLBSyJ4\nSxQ5RtRILnKWNdlEDK6MuuSmG+MFT5asqAkTTdQ18ZwYNd5IMBoZzSBCUEAHBE0EoiI7Erk8+0fV\nlD3jzHRNd1dX98z3/Xr1q7uqf9X9TE1PP/OrX/2eMndHREQEoFvaAYiISOlQUhARkYiSgoiIRJQU\nREQkoqQgIiIRJQUREYkoKYiISERJQUREIkoKIiIS2S3tADrqwAMP9KqqqrTDEBEpKy+88MImd++T\nrV3ZJYWqqirq6urSDkNEpKyY2do47XT4SEREIkoKIiISUVIQEZFI2Y0piEjxbN++nfXr17Nt27a0\nQ5GYevToQb9+/ejevXtO2yspiEib1q9fT69evaiqqsLM0g5HsnB3Nm/ezPr16xkwYEBOr9GlDh/V\n1KQdgUh52bZtGwcccIASQpkwMw444IC8enZdKilce23aEYiUHyWE8pLv76tLJQUREWlfp08KNTVg\nFtzgo8c6lCRSHtavX8/kyZMZOHAghx9+ODNnzuTDDz9ste2bb77Jueeem/U1Tz/9dN59992c4qmp\nqeHGG2/Madu47rrrLmbMmJF3m1x0+qQwcCD07Nl8Xc+ewXo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xEp\nFv1HIvKRPa35xdsfcfem01J7m9lLBP/tTwnX/R/gTjP7JrARuDhcPxOYb2aXEvQIvkpQobM1FcDC\nMHEYcIu7v1uwn0ikgzSmIJJFOKZQ7e6b0o5FJGk6fCQiIhH1FEREJKKegoiIRJQUREQkoqQgIiIR\nJQUREYkoKYiISERJQUREIv8DVI4KezW+r2IAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f43befc12e8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "original_train_loss = original_hist.history['loss']\n",
    "bigger_model_train_loss = bigger_model_hist.history['loss']\n",
    "\n",
    "plt.plot(epochs, original_train_loss, 'b+', label='Original model')\n",
    "plt.plot(epochs, bigger_model_train_loss, 'bo', label='Bigger model')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Training loss')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, the bigger network gets its training loss near zero very quickly. The more capacity the network has, the quicker it will be \n",
    "able to model the training data (resulting in a low training loss), but the more susceptible it is to overfitting (resulting in a large \n",
    "difference between the training and validation loss)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding weight regularization\n",
    "\n",
    "\n",
    "You may be familiar with _Occam's Razor_ principle: given two explanations for something, the explanation most likely to be correct is the \n",
    "\"simplest\" one, the one that makes the least amount of assumptions. This also applies to the models learned by neural networks: given some \n",
    "training data and a network architecture, there are multiple sets of weights values (multiple _models_) that could explain the data, and \n",
    "simpler models are less likely to overfit than complex ones.\n",
    "\n",
    "A \"simple model\" in this context is a model where the distribution of parameter values has less entropy (or a model with fewer \n",
    "parameters altogether, as we saw in the section above). Thus a common way to mitigate overfitting is to put constraints on the complexity \n",
    "of a network by forcing its weights to only take small values, which makes the distribution of weight values more \"regular\". This is called \n",
    "\"weight regularization\", and it is done by adding to the loss function of the network a _cost_ associated with having large weights. This \n",
    "cost comes in two flavors:\n",
    "\n",
    "* L1 regularization, where the cost added is proportional to the _absolute value of the weights coefficients_ (i.e. to what is called the \n",
    "\"L1 norm\" of the weights).\n",
    "* L2 regularization, where the cost added is proportional to the _square of the value of the weights coefficients_ (i.e. to what is called \n",
    "the \"L2 norm\" of the weights). L2 regularization is also called _weight decay_ in the context of neural networks. Don't let the different \n",
    "name confuse you: weight decay is mathematically the exact same as L2 regularization.\n",
    "\n",
    "In Keras, weight regularization is added by passing _weight regularizer instances_ to layers as keyword arguments. Let's add L2 weight \n",
    "regularization to our movie review classification network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras import regularizers\n",
    "\n",
    "l2_model = models.Sequential()\n",
    "l2_model.add(layers.Dense(16, kernel_regularizer=regularizers.l2(0.001),\n",
    "                          activation='relu', input_shape=(10000,)))\n",
    "l2_model.add(layers.Dense(16, kernel_regularizer=regularizers.l2(0.001),\n",
    "                          activation='relu'))\n",
    "l2_model.add(layers.Dense(1, activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l2_model.compile(optimizer='rmsprop',\n",
    "                 loss='binary_crossentropy',\n",
    "                 metrics=['acc'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`l2(0.001)` means that every coefficient in the weight matrix of the layer will add `0.001 * weight_coefficient_value` to the total loss of \n",
    "the network. Note that because this penalty is _only added at training time_, the loss for this network will be much higher at training \n",
    "than at test time.\n",
    "\n",
    "Here's the impact of our L2 regularization penalty:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.4880 - acc: 0.8218 - val_loss: 0.3820 - val_acc: 0.8798\n",
      "Epoch 2/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.3162 - acc: 0.9068 - val_loss: 0.3353 - val_acc: 0.8896\n",
      "Epoch 3/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2742 - acc: 0.9185 - val_loss: 0.3306 - val_acc: 0.8898\n",
      "Epoch 4/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2489 - acc: 0.9288 - val_loss: 0.3363 - val_acc: 0.8866\n",
      "Epoch 5/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2420 - acc: 0.9318 - val_loss: 0.3492 - val_acc: 0.8820\n",
      "Epoch 6/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2322 - acc: 0.9359 - val_loss: 0.3567 - val_acc: 0.8788\n",
      "Epoch 7/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2254 - acc: 0.9385 - val_loss: 0.3632 - val_acc: 0.8787\n",
      "Epoch 8/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2219 - acc: 0.9380 - val_loss: 0.3630 - val_acc: 0.8794\n",
      "Epoch 9/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2162 - acc: 0.9430 - val_loss: 0.3704 - val_acc: 0.8763\n",
      "Epoch 10/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2144 - acc: 0.9428 - val_loss: 0.3876 - val_acc: 0.8727\n",
      "Epoch 11/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2091 - acc: 0.9439 - val_loss: 0.3883 - val_acc: 0.8724\n",
      "Epoch 12/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2061 - acc: 0.9455 - val_loss: 0.3870 - val_acc: 0.8740\n",
      "Epoch 13/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2069 - acc: 0.9445 - val_loss: 0.4073 - val_acc: 0.8714\n",
      "Epoch 14/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2028 - acc: 0.9475 - val_loss: 0.3976 - val_acc: 0.8714\n",
      "Epoch 15/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1998 - acc: 0.9472 - val_loss: 0.4362 - val_acc: 0.8670\n",
      "Epoch 16/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2019 - acc: 0.9462 - val_loss: 0.4088 - val_acc: 0.8711\n",
      "Epoch 17/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1953 - acc: 0.9495 - val_loss: 0.4185 - val_acc: 0.8698\n",
      "Epoch 18/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1945 - acc: 0.9508 - val_loss: 0.4371 - val_acc: 0.8674\n",
      "Epoch 19/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1934 - acc: 0.9486 - val_loss: 0.4136 - val_acc: 0.8699\n",
      "Epoch 20/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1924 - acc: 0.9504 - val_loss: 0.4200 - val_acc: 0.8704\n"
     ]
    }
   ],
   "source": [
    "l2_model_hist = l2_model.fit(x_train, y_train,\n",
    "                             epochs=20,\n",
    "                             batch_size=512,\n",
    "                             validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XuYFPWd7/H3lxGcYPCOBkUcNKgMN5URNMb7BXUNiBqj\nh+esA4lGDYomx0sWN4xxzWM8iW50NQkqITniFaOSxCTGqLAazDKQAbmEiy6YMUQHIiIZWQf4nj+q\npm2Gmema6a6uvnxez9NPd1X/qus7RVPf/l3qV+buiIiIAPRIOgARESkcSgoiIpKipCAiIilKCiIi\nkqKkICIiKUoKIiKSoqQgIiIpSgoiIpKipCAiIim7JR1AV+2///5eVVWVdBgiIkVl4cKFG9y9b6Zy\nRZcUqqqqqK+vTz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REUpQUREQkRUlBRERSlBRERCTl/wPKWI2iT5zC3QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f43bef16c50>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l2_model_val_loss = l2_model_hist.history['val_loss']\n",
    "\n",
    "plt.plot(epochs, original_val_loss, 'b+', label='Original model')\n",
    "plt.plot(epochs, l2_model_val_loss, 'bo', label='L2-regularized model')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Validation loss')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "As you can see, the model with L2 regularization (dots) has become much more resistant to overfitting than the reference model (crosses), \n",
    "even though both models have the same number of parameters.\n",
    "\n",
    "As alternatives to L2 regularization, you could use one of the following Keras weight regularizers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras import regularizers\n",
    "\n",
    "# L1 regularization\n",
    "regularizers.l1(0.001)\n",
    "\n",
    "# L1 and L2 regularization at the same time\n",
    "regularizers.l1_l2(l1=0.001, l2=0.001)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding dropout\n",
    "\n",
    "\n",
    "Dropout is one of the most effective and most commonly used regularization techniques for neural networks, developed by Hinton and his \n",
    "students at the University of Toronto. Dropout, applied to a layer, consists of randomly \"dropping out\" (i.e. setting to zero) a number of \n",
    "output features of the layer during training. Let's say a given layer would normally have returned a vector `[0.2, 0.5, 1.3, 0.8, 1.1]` for a \n",
    "given input sample during training; after applying dropout, this vector will have a few zero entries distributed at random, e.g. `[0, 0.5, \n",
    "1.3, 0, 1.1]`. The \"dropout rate\" is the fraction of the features that are being zeroed-out; it is usually set between 0.2 and 0.5. At test \n",
    "time, no units are dropped out, and instead the layer's output values are scaled down by a factor equal to the dropout rate, so as to \n",
    "balance for the fact that more units are active than at training time.\n",
    "\n",
    "Consider a Numpy matrix containing the output of a layer, `layer_output`, of shape `(batch_size, features)`. At training time, we would be \n",
    "zero-ing out at random a fraction of the values in the matrix:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# At training time: we drop out 50% of the units in the output\n",
    "layer_output *= np.randint(0, high=2, size=layer_output.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "At test time, we would be scaling the output down by the dropout rate. Here we scale by 0.5 (because we were previous dropping half the \n",
    "units):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# At test time:\n",
    "layer_output *= 0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Note that this process can be implemented by doing both operations at training time and leaving the output unchanged at test time, which is \n",
    "often the way it is implemented in practice:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# At training time:\n",
    "layer_output *= np.randint(0, high=2, size=layer_output.shape)\n",
    "# Note that we are scaling *up* rather scaling *down* in this case\n",
    "layer_output /= 0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "This technique may seem strange and arbitrary. Why would this help reduce overfitting? Geoff Hinton has said that he was inspired, among \n",
    "other things, by a fraud prevention mechanism used by banks -- in his own words: _\"I went to my bank. The tellers kept changing and I asked \n",
    "one of them why. He said he didn’t know but they got moved around a lot. I figured it must be because it would require cooperation \n",
    "between employees to successfully defraud the bank. This made me realize that randomly removing a different subset of neurons on each \n",
    "example would prevent conspiracies and thus reduce overfitting\"_.\n",
    "\n",
    "The core idea is that introducing noise in the output values of a layer can break up happenstance patterns that are not significant (what \n",
    "Hinton refers to as \"conspiracies\"), which the network would start memorizing if no noise was present. \n",
    "\n",
    "In Keras you can introduce dropout in a network via the `Dropout` layer, which gets applied to the output of layer right before it, e.g.:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.add(layers.Dropout(0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's add two `Dropout` layers in our IMDB network to see how well they do at reducing overfitting:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dpt_model = models.Sequential()\n",
    "dpt_model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))\n",
    "dpt_model.add(layers.Dropout(0.5))\n",
    "dpt_model.add(layers.Dense(16, activation='relu'))\n",
    "dpt_model.add(layers.Dropout(0.5))\n",
    "dpt_model.add(layers.Dense(1, activation='sigmoid'))\n",
    "\n",
    "dpt_model.compile(optimizer='rmsprop',\n",
    "                  loss='binary_crossentropy',\n",
    "                  metrics=['acc'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/20\n",
      "25000/25000 [==============================] - 3s - loss: 0.6035 - acc: 0.6678 - val_loss: 0.4704 - val_acc: 0.8651\n",
      "Epoch 2/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.4622 - acc: 0.8002 - val_loss: 0.3612 - val_acc: 0.8724\n",
      "Epoch 3/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.3731 - acc: 0.8553 - val_loss: 0.2960 - val_acc: 0.8904\n",
      "Epoch 4/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.3162 - acc: 0.8855 - val_loss: 0.2772 - val_acc: 0.8917\n",
      "Epoch 5/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2762 - acc: 0.9033 - val_loss: 0.2803 - val_acc: 0.8889\n",
      "Epoch 6/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2454 - acc: 0.9172 - val_loss: 0.2823 - val_acc: 0.8892\n",
      "Epoch 7/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.2178 - acc: 0.9281 - val_loss: 0.2982 - val_acc: 0.8877\n",
      "Epoch 8/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1994 - acc: 0.9351 - val_loss: 0.3101 - val_acc: 0.8875\n",
      "Epoch 9/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1832 - acc: 0.9400 - val_loss: 0.3318 - val_acc: 0.8860\n",
      "Epoch 10/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1692 - acc: 0.9434 - val_loss: 0.3534 - val_acc: 0.8841\n",
      "Epoch 11/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1590 - acc: 0.9483 - val_loss: 0.3689 - val_acc: 0.8830\n",
      "Epoch 12/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1499 - acc: 0.9496 - val_loss: 0.4107 - val_acc: 0.8776\n",
      "Epoch 13/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1405 - acc: 0.9539 - val_loss: 0.4114 - val_acc: 0.8782\n",
      "Epoch 14/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1333 - acc: 0.9562 - val_loss: 0.4549 - val_acc: 0.8771\n",
      "Epoch 15/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1267 - acc: 0.9572 - val_loss: 0.4579 - val_acc: 0.8800\n",
      "Epoch 16/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1225 - acc: 0.9580 - val_loss: 0.4843 - val_acc: 0.8772\n",
      "Epoch 17/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1233 - acc: 0.9590 - val_loss: 0.4783 - val_acc: 0.8761\n",
      "Epoch 18/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1212 - acc: 0.9601 - val_loss: 0.5051 - val_acc: 0.8740\n",
      "Epoch 19/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1153 - acc: 0.9618 - val_loss: 0.5451 - val_acc: 0.8747\n",
      "Epoch 20/20\n",
      "25000/25000 [==============================] - 2s - loss: 0.1155 - acc: 0.9621 - val_loss: 0.5358 - val_acc: 0.8738\n"
     ]
    }
   ],
   "source": [
    "dpt_model_hist = dpt_model.fit(x_train, y_train,\n",
    "                               epochs=20,\n",
    "                               batch_size=512,\n",
    "                               validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's plot the results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8VPW9//HXh4CmIgIqWipLgIJsgQhh8eKGe0VAtLVw\nuVb0qqWVSm3rT1t6S6jtvdZa/V3FDa2gLXUBK/Lr1aqtgsWlBbxxAZU10bhGFBCRGsjn98c5GYYw\nSSaZPfN+Ph7nMXPOnJn5ZBjOZ767uTsiIiIAbTIdgIiIZA8lBRERiVBSEBGRCCUFERGJUFIQEZEI\nJQUREYlQUhARkQglBRERiVBSEBGRiLaZDqC5Dj/8cC8qKsp0GCIiOWX16tUfuXuXps7LuaRQVFTE\nqlWrMh2GiEhOMbPKeM5T9ZGIiEQoKYiISISSgoiIRORcm0IsNTU1VFVVsWvXrkyHItKgwsJCunXr\nRrt27TIdikiDWkVSqKqqokOHDhQVFWFmmQ5HZD/uzpYtW6iqqqJXr16ZDkekQa2i+mjXrl0cdthh\nSgiStcyMww47TKXZPFdWltnnx6NVJAVACUGynr6jMmdOZp8fj1aTFEREJHFKCklSVVXFxIkT6du3\nL3369GHmzJl88cUXMc999913+frXv97ka5511lls3bq1RfGUlZVxww03tOi58VqwYAEzZsxI+ByR\n1qysDMyCDfbej7cqKNHnN1deJ4Vkfajuzrnnnss555zD+vXrWbduHTt27GDWrFn7nbt7926+8pWv\nsHjx4iZf97HHHqNTp07JCVJEMqKsDNyDDfbeb05SSOT5zZXXSSFZ9XNPP/00hYWFXHTRRQAUFBRw\n0003cc8997Bz504WLFjAhAkTOPnkkznllFOoqKhg8ODBAOzcuZPzzz+fgQMHMmnSJEaNGhWZxqOo\nqIiPPvqIiooKBgwYwKWXXsqgQYM4/fTT+fzzzwG46667GDFiBEOHDuW8885j586djcY6bdo0vvOd\n7zB69Gh69+7NsmXLuPjiixkwYADTpk2LnHf//fdTXFzM4MGDufrqqyPH58+fT79+/Rg5ciTPPfdc\n5Hh1dTXnnXceI0aMYMSIEfs8JiK5I6+TQrKsWbOG4cOH73PskEMOoUePHmzYsAGAl156icWLF7N8\n+fJ9zrvtttvo3Lkza9eu5dprr2X16tUx32P9+vVcfvnlrFmzhk6dOvHwww8DcO6557Jy5Upefvll\nBgwYwG9/+9sm4/3kk0944YUXuOmmm5gwYQJXXnkla9as4dVXX6W8vJx3332Xq6++mqeffpry8nJW\nrlzJkiVLeO+995g9ezbPPfccK1asYO3atZHXnDlzJldeeSUrV67k4Ycf5pJLLmnWZyiSD2bPzuzz\n49Eqxik0R1nZviWEunq62bNT293rtNNO49BDD93v+IoVK5g5cyYAgwcPZsiQITGf36tXL0pKSgAY\nPnw4FRUVALz22mv89Kc/ZevWrezYsYMzzjijyVjGjx+PmVFcXMyRRx5JcXExAIMGDaKiooLKykpO\nOukkunQJJlScOnUqzz77LMA+x7/5zW+ybt06AP7yl7/skyS2b9/Ojh07moxFJJ/kQpfUvEwKdR+s\n2d56ukQMHDhwvzaC7du389Zbb/HVr36Vl156ifbt2yf0HgceeGDkfkFBQaT6aNq0aSxZsoShQ4ey\nYMECli1bFvdrtWnTZp/XbdOmDbt3727RiNva2lpefPFFCgsLm/1cEckeqj5KglNOOYWdO3dy3333\nAbBnzx5++MMfMm3aNA466KBGnztmzBgeeughANauXcurr77arPf+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98/jWgWEZEIVR+J\niEiEkoKIiEQoKYiISISSgoiIRCgpiIhIhJKCSMjM9ti+M7gmbcZOMyuKnqFTJFu1zXQAIlnkc3cv\nyXQQIpmkkoJIE8L59K8P59T/h5l9NTxeZGZPhxO+/dXMeoTHj7RgjYOXw+1fwpcqMLO7wjnznzSz\nL4XnXxHOpf+KmT2QoT9TBFBSEIn2pXrVR9+MemybuxcDc4H/Gx67BbjX3YcQTEh3c3j8ZmC5BxP6\nDSMYCQvQF7jV3QcBW4HzwuPXAMeErzM9VX+cSDw0olkkZGY73P3gGMcrgJPdfVM4cdn77n6YmX1E\nMHVDTXj8PXc/3MyqgW7u/s+o1ygimPe+b7h/NdDO3X9hZn8GdhDMBrvEw8kARTJBJQWR+HgD95vj\nn1H397C3TW8cwVxUw4CV4cydIhmhpCASn29G3b4Q3n+eYFZPgKnA38L7fwW+A2BmBWbWsaEXNbM2\nQHd3fwa4GugI7FdaEUkX/SIR2etLtu/i7X9297puqZ3N7BWCX/tTwmPfA+ab2VVANXBReHwmMM/M\n/p2gRPAdghk6YykAfh8mDgNudvetSfuLRJpJbQoiTQjbFErd/aNMxyKSaqo+EhGRCJUUREQkQiUF\nERGJUFIQEZEIJQUREYlQUhARkQglBRERiVBSEBGRiP8PEnLZyMua1xUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f43baa91dd8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dpt_model_val_loss = dpt_model_hist.history['val_loss']\n",
    "\n",
    "plt.plot(epochs, original_val_loss, 'b+', label='Original model')\n",
    "plt.plot(epochs, dpt_model_val_loss, 'bo', label='Dropout-regularized model')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Validation loss')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Again, a clear improvement over the reference network.\n",
    "\n",
    "To recap: here the most common ways to prevent overfitting in neural networks:\n",
    "\n",
    "* Getting more training data.\n",
    "* Reducing the capacity of the network.\n",
    "* Adding weight regularization.\n",
    "* Adding dropout."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
