{"cells":[{"metadata":{"_uuid":"f15ce6b398727e37f0451497d4c86e217293d679"},"cell_type":"markdown","source":"# Intro\n**This is Lesson 8 in the [Deep Learning](https://www.kaggle.com/learn/deep-learning) track**  \n\nAt the end of this lesson, you will understand and know how to use\n- **Stride lengths** to make your model faster and reduce memory consumption\n- **Dropout** to combat overfitting\n\nBoth of these techniques are especially useful in large models.\n\n# Lesson"},{"metadata":{"_kg_hide-input":true,"collapsed":true,"trusted":false,"_uuid":"66e086318e16c8dbe41d34e6729ac2b4dd3e34ab"},"cell_type":"code","source":"from IPython.display import YouTubeVideo\nYouTubeVideo('fwNLf4t7MR8', width=800, height=450)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"38ca0e1bd2e173e4d3acbac0cc4eb7a0494f7fe9"},"cell_type":"markdown","source":"# Sample Code"},{"metadata":{"trusted":true,"_uuid":"f0d0b2f7340fdaa55afaeeffd2dc9338ff5c9980"},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.python import keras\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout\n\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\ndef data_prep(raw):\n    out_y = keras.utils.to_categorical(raw.label, num_classes)\n    \n    num_images = raw.shape[0]\n    x_as_array = raw.values[:,1:]\n    x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)\n    \n    out_x = x_shaped_array / 255\n    return out_x, out_y\n    \ntrain_size = 30000\ntrain_file = \"../input/digit-recognizer/train.csv\"\nraw_data = pd.read_csv(train_file)\n\nx, y = data_prep(raw_data)\n\nmodel = Sequential()\nmodel.add(Conv2D(30, kernel_size=(3, 3),\n                 strides=2, \n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\n                              \n\nmodel.add(Dropout(0.5))\nmodel.add(Conv2D(30, kernel_size=(3, 3), strides=2, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\nmodel.fit(x, y, \n          batch_size=128,\n          epochs=2,\n          validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"32f6c0bc1dd713af40bfb476a54ecd8d469f9dd9"},"cell_type":"markdown","source":"# Exercise\n**[Apply dropout and strides](https://www.kaggle.com/kernels/fork/663261)** yourself while experimenting with larger models.\n\n---\n**[Deep Learning Course Home Page](https://www.kaggle.com/learn/deep-learning)**\n"},{"metadata":{"trusted":true,"_uuid":"aa981d11c51c5997eb0fe3414b968ff85785a44f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}