{"cells":[{"metadata":{"_uuid":"ed1526f66603d2f952a046591a6f19f83bc14e7d"},"cell_type":"markdown","source":"# Introduction\n\nYou've built a model to identify clothing types in the **MNIST for Fashion** dataset.  Now you will make your model bigger, specify larger stride lengths and apply dropout. These changes will make your model faster and more accurate.\n\nThis is a last step in the **[Deep Learning Track](https://www.kaggle.com/learn/deep-learning)**.\n\n\n## Data Preparation\n**Run this cell of code.**\n"},{"metadata":{"_uuid":"14e9e6b7049da2e39d822c881d6e4e75e60e2d4e","trusted":true},"cell_type":"code","source":"import numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\n\n# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.deep_learning.exercise_8 import *\nprint(\"Setup Complete\")\n\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\ndef prep_data(raw):\n    y = raw[:, 0]\n    out_y = keras.utils.to_categorical(y, num_classes)\n    \n    x = raw[:,1:]\n    num_images = raw.shape[0]\n    out_x = x.reshape(num_images, img_rows, img_cols, 1)\n    out_x = out_x / 255\n    return out_x, out_y\n\nfashion_file = \"../input/fashionmnist/fashion-mnist_train.csv\"\nfashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',')\nx, y = prep_data(fashion_data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"73543252c64b03313353fdb8695140ba389ca76c"},"cell_type":"markdown","source":"# 1) Increasing Stride Size in A Layer\n\nBelow is a model without strides (or more accurately, with a stride length of 1)\n\nRun it. Notice it's accuracy and how long it takes per epoch. Then you will change the stride length in one of the layers."},{"metadata":{"_uuid":"3b815a86e5ac72bd940a0dfdbbfc1f3c7daa2a21","trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout\n\nbatch_size = 16\n\nfashion_model = Sequential()\nfashion_model.add(Conv2D(16, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\nfashion_model.add(Conv2D(16, (3, 3), activation='relu'))\nfashion_model.add(Flatten())\nfashion_model.add(Dense(128, activation='relu'))\nfashion_model.add(Dense(num_classes, activation='softmax'))\n\nfashion_model.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\nfashion_model.fit(x, y,\n          batch_size=batch_size,\n          epochs=3,\n          validation_split = 0.2)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"107a563d819f22d32d65e77d468af2f74838a1eb"},"cell_type":"markdown","source":"You have the same code in the cell below, but the model is now called `fashion_model_1`.  Change the specification of `fashion_model_1` so the second convolutional layer has a stride length of 2.\n\nRun the cell after you have done that. How does the speed and accuracy change compared to the first model you ran above?"},{"metadata":{"_uuid":"98fb93e9b71436420052de256a1f50ccf68948b5","trusted":true},"cell_type":"code","source":"fashion_model_1 = Sequential()\nfashion_model_1.add(Conv2D(16, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\nfashion_model_1.add(Conv2D(16, (3, 3), activation='relu', strides=2))\nfashion_model_1.add(Flatten())\nfashion_model_1.add(Dense(128, activation='relu'))\nfashion_model_1.add(Dense(num_classes, activation='softmax'))\n\nfashion_model_1.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\nfashion_model_1.fit(x, y,\n          batch_size=batch_size,\n          epochs=3,\n          validation_split = 0.2)\nq_1.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"48eca159f8b2395a493fed29cb5aa4c2b9b91bfe"},"cell_type":"markdown","source":"For the solution, uncomment and run the cell below:"},{"metadata":{"_uuid":"54508467e65c599f002058a5303a5b7f7dafeffd","trusted":false},"cell_type":"code","source":"#q_1.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4bb08e1108d64361ef49a05a4bfa810dc19061cf"},"cell_type":"markdown","source":"You should notice that your model training ran about twice as fast, but the accuracy change was trivial.  \n\nIn addition to being faster to train, this model is also faster at making predictions. This is very important in many scenarios. In practice, you'll need to decide whether that type of speed is important in the applications where you eventually apply deep learning models.\n\nYou could experiment with more layers or more convolutions in each layer. With some fine-tuning, you can build a model that is both faster and more accurate than the original model."},{"metadata":{"_uuid":"ae9a51f640be15e13d265695fb9845bd7987d81e"},"cell_type":"markdown","source":"# Congrats\nYou've finished the Deep Learning course.  You have the tools to create and tune computer vision models.  \n\nIf you feel like playing more with this dataset, you can open up a new code cell to experiment with different models (adding dropout, adding layers, etc.)  Or pick a new project and try out your skills.  \n\nA few fun datasets you might try include:\n- [Written letter recognition](https://www.kaggle.com/olgabelitskaya/classification-of-handwritten-letters)\n- [Flower Identification](https://www.kaggle.com/alxmamaev/flowers-recognition)\n- [Cats vs Dogs](https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition)\n- [10 Monkeys](https://www.kaggle.com/slothkong/10-monkey-species)\n- [Predict Bone Age from X-Rays](https://www.kaggle.com/kmader/rsna-bone-age)\n\nYou have learned a lot, and you'll learn it as you practice. Have fun with it!"}],"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.6.5"}},"nbformat":4,"nbformat_minor":1}