{"cells":[{"metadata":{},"cell_type":"markdown","source":"**[Deep Learning Home Page](https://www.kaggle.com/learn/deep-learning)**\n\n---\n","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Introduction\n\nYou've seen how to build a model from scratch to identify handwritten digits.  You'll now build a model to identify different types of clothing.  To make models that train quickly, we'll work with very small (low-resolution) images. \n\nAs an example, your model will take an images like this and identify it as a shoe:\n\n![Imgur](https://i.imgur.com/GyXOnSB.png)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Data Preparation\nThis code is supplied, and you don't need to change it. Just run the cell below.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\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)\n\n# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.deep_learning.exercise_7 import *\nprint(\"Setup Complete\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 1) Start the model\nCreate a `Sequential` model called `fashion_model`. Don't add layers yet.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D\n\nfashion_model = Sequential()\n____\n\n# Check your answer\nq_1.check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"q_1.solution()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 2) Add the first layer\n\nAdd the first `Conv2D` layer to `fashion_model`. It should have 12 filters, a kernel_size of 3 and the `relu` activation function. The first layer always requires that you specify the `input_shape`.  We have saved the number of rows and columns to the variables `img_rows` and `img_cols` respectively, so the input shape in this case is `(img_rows, img_cols, 1)`.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fashion_model.add(Conv2D(12,\n                         activation='relu',\n                         kernel_size=3,\n                         input_shape = (img_rows, img_cols, 1)))\n____\n\n# Check your answer\nq_2.check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" q_2.hint()\nq_2.solution()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 3) Add the remaining layers\n\n1. Add 2 more convolutional (`Conv2D layers`) with 20 filters each, 'relu' activation, and a kernel size of 3. Follow that with a `Flatten` layer, and then a `Dense` layer with 100 neurons. \n2. Add your prediction layer to `fashion_model`.  This is a `Dense` layer.  We alrady have a variable called `num_classes`.  Use this variable when specifying the number of nodes in this layer. The activation should be `softmax` (or you will have problems later).","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fashion_model.add(Conv2D(20, activation='relu', kernel_size=3))\nfashion_model.add(Conv2D(20, activation='relu', kernel_size=3))\nfashion_model.add(Flatten())\nfashion_model.add(Dense(100, activation='relu'))\nfashion_model.add(Dense(10, activation='softmax'))\n____\n\n# Check your answer\nq_3.check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" q_3.solution()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 4) Compile Your Model\nCompile fashion_model with the `compile` method.  Specify the following arguments:\n1. `loss = \"categorical_crossentropy\"`\n2. `optimizer = 'adam'`\n3. `metrics = ['accuracy']`","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fashion_model.compile(loss='categorical_crossentropy',\n                      optimizer='adam',\n                      metrics=['accuracy'])\n____\n\n# Check your answer\nq_4.check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"q_4.solution()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 5) Fit The Model\nRun the command `fashion_model.fit`. The arguments you will use are\n1. The data used to fit the model. First comes the data holding the images, and second is the data with the class labels to be predicted. Look at the first code cell (which was supplied to you) where we called `prep_data` to find the variable names for these.\n2. `batch_size = 100`\n3. `epochs = 4`\n4. `validation_split = 0.2`\n\nWhen you run this command, you can watch your model start improving.  You will see validation accuracies after each epoch.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Your code to fit the model here\n\nfashion_model.fit(x, y, batch_size=100, epochs=4, validation_split=0.2)\n____\n\n# Check your answer\nq_5.check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"q_5.solution()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 6) Create A New Model\n\nCreate a new model called `second_fashion_model` in the cell below.  Make some changes so it is different than `fashion_model` that you've trained above. The change could be using a different number of layers, different number of convolutions in the layers, etc.\n\nDefine the model, compile it and fit it in the cell below.  See how its validation score compares to that of the original model.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Your code below\n\nsecond_fashion_model = Sequential()\nsecond_fashion_model.add(Conv2D(12,\n                         activation='relu',\n                         kernel_size=3,\n                         input_shape = (img_rows, img_cols, 1)))\n# Changed kernel sizes to be 2\nsecond_fashion_model.add(Conv2D(20, activation='relu', kernel_size=2))\nsecond_fashion_model.add(Conv2D(20, activation='relu', kernel_size=2))\n# added an addition Conv2D layer\nsecond_fashion_model.add(Conv2D(20, activation='relu', kernel_size=2))\nsecond_fashion_model.add(Flatten())\nsecond_fashion_model.add(Dense(100, activation='relu'))\n# It is important not to change the last layer. First argument matches number of classes. Softmax guarantees we get reasonable probabilities\nsecond_fashion_model.add(Dense(10, activation='softmax'))\n\nsecond_fashion_model.compile(loss='categorical_crossentropy',\n                             optimizer='adam',\n                             metrics=['accuracy'])\n\nsecond_fashion_model.fit(x, y, batch_size=100, epochs=4, validation_split=0.2)\n\n\n____\n\n\n# Don't remove this line (ensures comptibility with tensorflow 2.0)\nsecond_fashion_model.history.history['val_acc'] = second_fashion_model.history.history['val_accuracy']\n# Check your answer\nq_6.check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"q_6.solution()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Keep Going\nYou are ready to learn about **[strides and dropout](https://www.kaggle.com/dansbecker/dropout-and-strides-for-larger-models)**, which become important as you start using bigger and more powerful models.\n","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"---\n**[Deep Learning Home Page](https://www.kaggle.com/learn/deep-learning)**\n\n\n\n\n\n*Have questions or comments? Visit the [Learn Discussion forum](https://www.kaggle.com/learn-forum) to chat with other Learners.*","execution_count":null}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}