{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analyze (F)MNIST with `tensorflow` / `keras`\n",
    "\n",
    "Careful: do **not** hit 'Kernel' > 'Restart & Run All', since some of the cells below take a long time to execute if you are not running the code on a GPU, so we already executed them for you.\n",
    "\n",
    "In this notebook we compare different types of neural network architectures on the MNIST and Fashion MNIST datasets, to see how the performance improves when using a more complicated architecture. Additionally, we compare the networks to a simple logistic regression classifier from `sklearn`, which should have approximately the same accuracy as a linear FFNN (= a FFNN with only one layer mapping from the input directly to the output and no hidden layers, i.e., that has the same number of trainable parameters as the logistic regression model)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:18:20.020015Z",
     "start_time": "2019-06-28T18:17:33.894451Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import gzip\n",
    "import random\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score\n",
    "# neural network libraries\n",
    "import tensorflow as tf\n",
    "# set random seeds before importing keras to get (at least more or less) reproducable results\n",
    "random.seed(28)\n",
    "np.random.seed(28)\n",
    "tf.random.set_seed(28)\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras.datasets import mnist, fashion_mnist\n",
    "from tensorflow.keras import Sequential\n",
    "from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\n",
    "from tensorflow.keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fashion_mnist_load_local_data():\n",
    "    from tensorflow.python.keras.utils.data_utils import get_file\n",
    "    base = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/'\n",
    "    dirname = os.path.abspath(\"../data/fashion-mnist\") \n",
    "    files = [\n",
    "        'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',\n",
    "        't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'\n",
    "    ]\n",
    "    paths = []\n",
    "    for fname in files:\n",
    "        paths.append(get_file(fname, origin=base + fname, cache_subdir=dirname))\n",
    "\n",
    "    with gzip.open(paths[0], 'rb') as lbpath:\n",
    "        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)\n",
    "    with gzip.open(paths[1], 'rb') as imgpath:\n",
    "        x_train = np.frombuffer(\n",
    "            imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)\n",
    "    with gzip.open(paths[2], 'rb') as lbpath:\n",
    "        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)\n",
    "    with gzip.open(paths[3], 'rb') as imgpath:\n",
    "        x_test = np.frombuffer(\n",
    "            imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)\n",
    "    return (x_train, y_train), (x_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load and look at the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:18:20.049855Z",
     "start_time": "2019-06-28T18:18:20.021568Z"
    }
   },
   "outputs": [],
   "source": [
    "# input image dimensions\n",
    "img_rows, img_cols = 28, 28\n",
    "n_features = img_rows*img_cols\n",
    "\n",
    "def load_data(use_fashion=False, reshape=False):\n",
    "    # the data, split between train and test sets (load from local data folder, not ~/.keras)\n",
    "    if use_fashion:\n",
    "        (x_train, y_train), (x_test, y_test) = fashion_mnist_load_local_data()  # fashion_mnist.load_data()\n",
    "    else:\n",
    "        # might need to use mnist.load_data(path=\"mnist.npz\") when executing on Google Colab\n",
    "        (x_train, y_train), (x_test, y_test) = mnist.load_data(path=os.path.join(os.path.abspath(\"../data/\"), \"mnist.npz\"))\n",
    "\n",
    "    if K.image_data_format() == 'channels_first':\n",
    "        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\n",
    "        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\n",
    "        input_shape = (1, img_rows, img_cols)\n",
    "    else:\n",
    "        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n",
    "        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n",
    "        input_shape = (img_rows, img_cols, 1)\n",
    "\n",
    "    # normalize (data is ints from 0 to 255, get them to [0, 1])\n",
    "    x_train = x_train.astype('float32')\n",
    "    x_test = x_test.astype('float32')\n",
    "    x_train /= 255.\n",
    "    x_test /= 255.\n",
    "    \n",
    "    if reshape:\n",
    "        # transform images into regular feature vectors\n",
    "        x_train = x_train.reshape(x_train.shape[0], n_features)\n",
    "        x_test = x_test.reshape(x_test.shape[0], n_features)\n",
    "\n",
    "    return x_train, x_test, y_train, y_test\n",
    "\n",
    "def convert_cat(y_train, y_test, num_classes=10):\n",
    "    # convert class vectors to binary class matrices\n",
    "    y_train_cat = keras.utils.to_categorical(y_train, num_classes)\n",
    "    y_test_cat = keras.utils.to_categorical(y_test, num_classes)\n",
    "    return y_train_cat, y_test_cat\n",
    "\n",
    "def plot_images(x):\n",
    "    n = 10\n",
    "    plt.figure(figsize=(20, 4))\n",
    "    for i in range(1, n+1):\n",
    "        # display original\n",
    "        ax = plt.subplot(2, n, i)\n",
    "        plt.imshow(x[i].reshape(28, 28))\n",
    "        plt.gray()\n",
    "        ax.get_xaxis().set_visible(False)\n",
    "        ax.get_yaxis().set_visible(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:18:22.818959Z",
     "start_time": "2019-06-28T18:18:20.051562Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9188 characters]\n",
      "text/plain": [
       "<Figure size 1440x288 with 10 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 17936 characters]\n",
      "text/plain": [
       "<Figure size 1440x288 with 10 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# MNIST\n",
    "x_train, x_test, y_train, y_test = load_data()\n",
    "plot_images(x_train)\n",
    "# fashion MNIST\n",
    "x_train, x_test, y_train, y_test = load_data(True)\n",
    "plot_images(x_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train a \"regular\" linear model on the original MNIST dataset\n",
    "\n",
    "As you see below, the simple logistic regression classifier is already very good on this easy task, with a test accuracy of over 92.6%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:20:11.767300Z",
     "start_time": "2019-06-28T18:18:31.492075Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/franzi/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py:762: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9262"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load data and reshape images to regular feature vectors\n",
    "x_train, x_test, y_train, y_test = load_data(reshape=True)\n",
    "# train LogReg classifier\n",
    "clf = LogisticRegression(class_weight='balanced', random_state=1, fit_intercept=True)\n",
    "clf.fit(x_train, y_train)\n",
    "# accuracy on test dataset\n",
    "clf.score(x_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train different NN architectures on MNIST\n",
    "\n",
    "In the code below we define 3 different neural network architectures: a linear FFNN, a FFNN with multiple hidden layers, and a CNN, which is an architecture particularly well suited for image classification tasks.\n",
    "\n",
    "You will see that the more complex architectures use an additional operation between layers called `Dropout`. This is a regularization technique used for training neural networks, where a certain percentage of the values in the hidden layer representation of a data point are randomly set to zero. You can think of this as the network suffering from a temporary stroke, which forces the neurons learn redundant representations (i.e., such that one neuron can take over for another neuron that was knocked out), which improves generalization.\n",
    "\n",
    "The linear FFNN has almost the same accuracy (90.5%) as the LogReg model (please note the NNs were only trained for a single epoch!) and the multi-layer FFNN is already better than the LogReg model (95.8%), while the CNN beats them all (98.3%), which is expected since this architecture is designed for the image classification task."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:20:11.798437Z",
     "start_time": "2019-06-28T18:20:11.769464Z"
    }
   },
   "outputs": [],
   "source": [
    "def train_linnn(x_train, y_train_cat, num_classes=10, epochs=1):\n",
    "    model = Sequential()\n",
    "    model.add(Dense(num_classes, activation='softmax', input_shape=(784,)))\n",
    "\n",
    "    model.compile(loss=keras.losses.categorical_crossentropy,\n",
    "                  optimizer=keras.optimizers.Adadelta(learning_rate=1.),\n",
    "                  metrics=['accuracy'])\n",
    "\n",
    "    model.fit(x_train, y_train_cat, epochs=epochs, batch_size=128)\n",
    "    return model\n",
    "\n",
    "def train_ffnn(x_train, y_train_cat, num_classes=10, epochs=1):\n",
    "    model = Sequential()\n",
    "    model.add(Dense(512, activation='relu', input_shape=(784,)))\n",
    "    model.add(Dropout(0.2))\n",
    "    model.add(Dense(512, activation='relu'))\n",
    "    model.add(Dropout(0.2))\n",
    "    model.add(Dense(num_classes, activation='softmax'))\n",
    "\n",
    "    model.compile(loss=keras.losses.categorical_crossentropy,\n",
    "                  optimizer=keras.optimizers.Adadelta(learning_rate=1.),\n",
    "                  metrics=['accuracy'])\n",
    "\n",
    "    model.fit(x_train, y_train_cat, epochs=epochs, batch_size=128)\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:20:24.912378Z",
     "start_time": "2019-06-28T18:20:11.799908Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "469/469 [==============================] - 0s 810us/step - loss: 0.6451 - accuracy: 0.8401\n",
      "Test accuracy: 0.9046000242233276\n",
      "0.9046\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2850 - accuracy: 0.9138\n",
      "Test accuracy: 0.9577999711036682\n"
     ]
    }
   ],
   "source": [
    "# load data and reshape \n",
    "x_train, x_test, y_train, y_test = load_data(reshape=True)\n",
    "y_train_cat, y_test_cat = convert_cat(y_train, y_test)\n",
    "# train simple linear model\n",
    "model = train_linnn(x_train, y_train_cat)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy:', score[1])\n",
    "# equivalent evaluation:\n",
    "# get predictions (as probabilities)\n",
    "y_pred = model.predict(x_test)\n",
    "# convert predictions to classes\n",
    "y_pred_classes = np.argmax(y_pred, axis=1)\n",
    "print(accuracy_score(y_test, y_pred_classes), \"\\n\")\n",
    "# train multi-layer FFNN\n",
    "model = train_ffnn(x_train, y_train_cat)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy:', score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:20:24.943000Z",
     "start_time": "2019-06-28T18:20:24.913859Z"
    }
   },
   "outputs": [],
   "source": [
    "# CNN classifier\n",
    "def train_cnn(x_train, y_train_cat, input_shape=(28, 28, 1), num_classes=10, epochs=1):\n",
    "    # setup CNN\n",
    "    model = Sequential()\n",
    "    model.add(Conv2D(32, kernel_size=(3, 3),\n",
    "                     activation='relu',\n",
    "                     input_shape=input_shape))\n",
    "    model.add(Conv2D(64, (3, 3), activation='relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "    model.add(Dropout(0.25))\n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(128, activation='relu'))\n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Dense(num_classes, activation='softmax'))\n",
    "\n",
    "    model.compile(loss=keras.losses.categorical_crossentropy,\n",
    "                  optimizer=keras.optimizers.Adadelta(learning_rate=1.),\n",
    "                  metrics=['accuracy'])\n",
    "    # train\n",
    "    model.fit(x_train, y_train_cat, epochs=epochs, batch_size=128)\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:21:21.387931Z",
     "start_time": "2019-06-28T18:20:24.944280Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "469/469 [==============================] - 32s 68ms/step - loss: 0.2628 - accuracy: 0.9199\n",
      "Test accuracy: 0.9827\n"
     ]
    }
   ],
   "source": [
    "x_train, x_test, y_train, y_test = load_data()\n",
    "y_train_cat, y_test_cat = convert_cat(y_train, y_test)\n",
    "# train cnn\n",
    "model = train_cnn(x_train, y_train_cat)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy:', score[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test all models on the Fashion MNIST dataset\n",
    "\n",
    "On the more difficult FMNIST task, the LogReg model has a much lower accuracy of 84.4% compared to the 92.6% achieved on the original MNIST dataset. When trained for only a single epoch, both the linear and multi-layer FFNNs have a lower accuracy than the LogReg model (80.5 and 81.9% respectively) and only the CNN does a bit better (86.5%). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:25:38.101988Z",
     "start_time": "2019-06-28T18:21:21.389691Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/franzi/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py:762: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy LogReg: 0.8438\n",
      "469/469 [==============================] - 0s 781us/step - loss: 0.7530 - accuracy: 0.7527\n",
      "Test accuracy Linear NN: 0.8045\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.5820 - accuracy: 0.7895\n",
      "Test accuracy FFNN: 0.8194\n",
      "469/469 [==============================] - 34s 73ms/step - loss: 0.5575 - accuracy: 0.8024\n",
      "Test accuracy CNN: 0.8652\n"
     ]
    }
   ],
   "source": [
    "# load data and reshape images to regular feature vectors\n",
    "x_train, x_test, y_train, y_test = load_data(True, reshape=True)\n",
    "y_train_cat, y_test_cat = convert_cat(y_train, y_test)\n",
    "# train LogReg classifier\n",
    "clf = LogisticRegression(class_weight='balanced', random_state=1, fit_intercept=True)\n",
    "clf.fit(x_train, y_train)\n",
    "print('Test accuracy LogReg:', clf.score(x_test, y_test), \"\\n\")\n",
    "# train simple linear model\n",
    "model = train_linnn(x_train, y_train_cat)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy Linear NN:', score[1], \"\\n\")\n",
    "# train multi-layer FFNN\n",
    "model = train_ffnn(x_train, y_train_cat)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy FFNN:', score[1], \"\\n\")\n",
    "# load data again (not reshaped)\n",
    "x_train, x_test, y_train, y_test = load_data(True)\n",
    "y_train_cat, y_test_cat = convert_cat(y_train, y_test)\n",
    "# train cnn\n",
    "model = train_cnn(x_train, y_train_cat)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy CNN:', score[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, when trained for more epochs, the performance of all models improves, with the accuracy of the linear FFNN now being very close to that of the LogReg model (84.3%), while the multi-layer FFNN is better (89.3%) and the CNN can now solve the task quite well with an accuracy of 92.4%.\n",
    "\n",
    "(See how the loss decreases over time - observing how this metric develops can help you judge whether you've set your learning rate correctly.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-28T18:42:43.369976Z",
     "start_time": "2019-06-28T18:25:38.103589Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/15\n",
      "469/469 [==============================] - 0s 785us/step - loss: 0.7222 - accuracy: 0.7677\n",
      "Epoch 2/15\n",
      "469/469 [==============================] - 0s 766us/step - loss: 0.5074 - accuracy: 0.8297\n",
      "Epoch 3/15\n",
      "469/469 [==============================] - 0s 759us/step - loss: 0.4694 - accuracy: 0.8403\n",
      "Epoch 4/15\n",
      "469/469 [==============================] - 0s 832us/step - loss: 0.4507 - accuracy: 0.8480\n",
      "Epoch 5/15\n",
      "469/469 [==============================] - 0s 776us/step - loss: 0.4388 - accuracy: 0.8495\n",
      "Epoch 6/15\n",
      "469/469 [==============================] - 0s 755us/step - loss: 0.4306 - accuracy: 0.8535\n",
      "Epoch 7/15\n",
      "469/469 [==============================] - 0s 772us/step - loss: 0.4239 - accuracy: 0.8544\n",
      "Epoch 8/15\n",
      "469/469 [==============================] - 0s 757us/step - loss: 0.4183 - accuracy: 0.8567\n",
      "Epoch 9/15\n",
      "469/469 [==============================] - 0s 767us/step - loss: 0.4139 - accuracy: 0.8577\n",
      "Epoch 10/15\n",
      "469/469 [==============================] - 0s 750us/step - loss: 0.4109 - accuracy: 0.8590\n",
      "Epoch 11/15\n",
      "469/469 [==============================] - 0s 765us/step - loss: 0.4079 - accuracy: 0.8602\n",
      "Epoch 12/15\n",
      "469/469 [==============================] - 0s 809us/step - loss: 0.4045 - accuracy: 0.8616\n",
      "Epoch 13/15\n",
      "469/469 [==============================] - 0s 778us/step - loss: 0.4021 - accuracy: 0.8621\n",
      "Epoch 14/15\n",
      "469/469 [==============================] - 0s 759us/step - loss: 0.4000 - accuracy: 0.8633\n",
      "Epoch 15/15\n",
      "469/469 [==============================] - 0s 766us/step - loss: 0.3988 - accuracy: 0.8627\n",
      "Test accuracy Linear NN: 0.8432\n",
      "Epoch 1/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.5830 - accuracy: 0.7891\n",
      "Epoch 2/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.4076 - accuracy: 0.8494\n",
      "Epoch 3/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.3657 - accuracy: 0.8644\n",
      "Epoch 4/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.3385 - accuracy: 0.8757\n",
      "Epoch 5/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.3202 - accuracy: 0.8808\n",
      "Epoch 6/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.3069 - accuracy: 0.8859\n",
      "Epoch 7/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2943 - accuracy: 0.8898\n",
      "Epoch 8/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2826 - accuracy: 0.8946\n",
      "Epoch 9/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2743 - accuracy: 0.8973\n",
      "Epoch 10/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2646 - accuracy: 0.8999\n",
      "Epoch 11/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2590 - accuracy: 0.9021\n",
      "Epoch 12/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2510 - accuracy: 0.9057\n",
      "Epoch 13/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2423 - accuracy: 0.9092\n",
      "Epoch 14/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2381 - accuracy: 0.9096\n",
      "Epoch 15/15\n",
      "469/469 [==============================] - 2s 4ms/step - loss: 0.2322 - accuracy: 0.9119\n",
      "Test accuracy FFNN: 0.8934\n",
      "Epoch 1/15\n",
      "469/469 [==============================] - 34s 73ms/step - loss: 0.5775 - accuracy: 0.7941\n",
      "Epoch 2/15\n",
      "469/469 [==============================] - 34s 72ms/step - loss: 0.3679 - accuracy: 0.8689\n",
      "Epoch 3/15\n",
      "469/469 [==============================] - 34s 73ms/step - loss: 0.3161 - accuracy: 0.8875\n",
      "Epoch 4/15\n",
      "469/469 [==============================] - 35s 74ms/step - loss: 0.2846 - accuracy: 0.8981\n",
      "Epoch 5/15\n",
      "469/469 [==============================] - 36s 78ms/step - loss: 0.2604 - accuracy: 0.9073\n",
      "Epoch 6/15\n",
      "469/469 [==============================] - 36s 76ms/step - loss: 0.2418 - accuracy: 0.9125\n",
      "Epoch 7/15\n",
      "469/469 [==============================] - 36s 76ms/step - loss: 0.2272 - accuracy: 0.9189\n",
      "Epoch 8/15\n",
      "469/469 [==============================] - 36s 77ms/step - loss: 0.2101 - accuracy: 0.9240\n",
      "Epoch 9/15\n",
      "469/469 [==============================] - 36s 76ms/step - loss: 0.2001 - accuracy: 0.9274\n",
      "Epoch 10/15\n",
      "469/469 [==============================] - 37s 79ms/step - loss: 0.1880 - accuracy: 0.9316\n",
      "Epoch 11/15\n",
      "469/469 [==============================] - 37s 78ms/step - loss: 0.1810 - accuracy: 0.9345\n",
      "Epoch 12/15\n",
      "469/469 [==============================] - 37s 78ms/step - loss: 0.1714 - accuracy: 0.9383\n",
      "Epoch 13/15\n",
      "469/469 [==============================] - 36s 77ms/step - loss: 0.1633 - accuracy: 0.9411\n",
      "Epoch 14/15\n",
      "469/469 [==============================] - 40s 84ms/step - loss: 0.1547 - accuracy: 0.9439\n",
      "Epoch 15/15\n",
      "469/469 [==============================] - 36s 76ms/step - loss: 0.1484 - accuracy: 0.9453\n",
      "Test accuracy CNN: 0.9239\n"
     ]
    }
   ],
   "source": [
    "x_train, x_test, y_train, y_test = load_data(True, reshape=True)\n",
    "y_train_cat, y_test_cat = convert_cat(y_train, y_test)\n",
    "# train simple linear model\n",
    "model = train_linnn(x_train, y_train_cat, epochs=15)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy Linear NN:', score[1], \"\\n\")\n",
    "# train multi-layer FFNN\n",
    "model = train_ffnn(x_train, y_train_cat, epochs=15)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy FFNN:', score[1], \"\\n\")\n",
    "# load data again (not reshaped)\n",
    "x_train, x_test, y_train, y_test = load_data(True)\n",
    "y_train_cat, y_test_cat = convert_cat(y_train, y_test)\n",
    "# train cnn\n",
    "model = train_cnn(x_train, y_train_cat, epochs=15)\n",
    "score = model.evaluate(x_test, y_test_cat, verbose=0)\n",
    "print('Test accuracy CNN:', score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
