{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 📚 **Import Libraries**","metadata":{}},{"cell_type":"code","source":"import os\nimport warnings\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nwarnings.filterwarnings(\"ignore\")\nimport tensorflow_datasets as tfds\nfrom matplotlib.pyplot import figure","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Using tensorflow datasets as my inputs instead of the input csv files (just for fun)**","metadata":{}},{"cell_type":"markdown","source":"Define my training circuits","metadata":{}},{"cell_type":"code","source":"(training_images, training_labels) = tfds.as_numpy(tfds.load(\n    'mnist',\n    split='train[30%:]',\n    batch_size=-1,\n    as_supervised=True,\n    shuffle_files=True,\n))\n\ntraining_images = training_images / 255.0\n\n# Looking at shape of my training data.\nprint(training_images.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Take a look at a set of training_images**","metadata":{}},{"cell_type":"code","source":"figure(figsize=(30, 10), dpi=60)\n\nx, y = 10, 5\nfor i in range(50):  \n    plt.subplot(y, x, i+1)\n    plt.imshow(training_images[i].reshape((28,28)),aspect='auto',interpolation='none', cmap='binary')\n    plt.axis('off')\n    plt.tight_layout(pad=0.0)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Closer look at a training_images image**","metadata":{}},{"cell_type":"code","source":"img = training_images[0].reshape(1,28,28,1)\nfig = plt.figure(figsize=(8,8))\nplt.imshow(img[0,:,:,0], interpolation='none', cmap='binary')\nplt.axis('off')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define my test circuits","metadata":{}},{"cell_type":"code","source":"(test_images, test_labels) = tfds.as_numpy(tfds.load(\n    'mnist',\n    split='train[70%:]+test',\n    batch_size=-1,\n    as_supervised=True,\n    shuffle_files=True,\n))\n\ntest_images = test_images/255.0\n\n# Looking at shape of my testing data.\nprint(test_images.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Take a look at a set of test_images**","metadata":{}},{"cell_type":"code","source":"figure(figsize=(30, 10), dpi=60)\n\nx, y = 10, 5\nfor i in range(50):  \n    plt.subplot(y, x, i+1)\n    plt.imshow(test_images[i].reshape((28,28)),aspect='auto',interpolation='none', cmap='gray')\n    plt.axis('off')\n    plt.tight_layout(pad=0.0)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Closer look at a training_images image**","metadata":{}},{"cell_type":"code","source":"img = test_images[0].reshape(1,28,28,1)\nfig = plt.figure(figsize=(8,8))\nplt.imshow(img[0,:,:,0], interpolation='none', cmap='gray')\nplt.axis('off')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***Defining the model.***","metadata":{}},{"cell_type":"code","source":"callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=20, verbose=1, restore_best_weights=True)\n\nmodel = tf.keras.models.Sequential([\n  tf.keras.layers.Conv2D(20, (2,2), activation='relu', input_shape=(28, 28, 1)),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (2,2), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (2,2), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (2,2), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (3,2), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (2,3), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (3,3), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (5,2), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (2,5), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (5,5), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (3,5), activation='relu'),\n  tf.keras.layers.Dropout(0.1),\n  tf.keras.layers.Conv2D(20, (5,3), activation='relu'),\n  tf.keras.layers.MaxPooling2D(2,2),\n  tf.keras.layers.Flatten(),\n  tf.keras.layers.Dense(200, activation='relu'),\n  tf.keras.layers.Dropout(0.3),\n  tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **ALL THE CONVOLUTIONS!!**","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Listing out the model layer_names to look at the convolutions**","metadata":{}},{"cell_type":"code","source":"layer_names = [layer.name for layer in model.layers]\nlayer_names","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Take a look at any layer in the model by changing layer_name below.**","metadata":{}},{"cell_type":"code","source":"#layer_name = 'conv2d_3'\n#layer_dict = {layer.name : layer for layer in model.layers}\n#modelslice = tf.keras.Model(model.inputs, layer_dict[layer_name].output)\n#image = training_images[20] \n#image = np.expand_dims(image, axis=0)\n#feature_maps = modelslice.predict(image)\n#plt.figure(figsize=(20, 10))\n\n#for i in range(16): # This is itterating through 16 filters of the convolution!\n    #plt.subplot(4,8,i+1)\n    #plt.axis('off')\n    #plt.imshow(feature_maps[0, :, :, i-1], cmap='inferno')\n    #plt.tight_layout(pad=0.0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Compile, Fit, and Evaluate**\n\n","metadata":{}},{"cell_type":"markdown","source":"The callback will stop the training when there is no improvement in the\nloss for 10 consecutive epochs.\n\nThe restore_best_weights=True will take the model back to its best fit. ","metadata":{}},{"cell_type":"code","source":"optimizer = 'adamax'\n\nmodel.compile(optimizer=optimizer,\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nhistory = model.fit(training_images, \n                    training_labels,\n                    epochs=500,\n                    verbose=1,\n                    callbacks=[callback])\n\ntest_loss, test_acc = model.evaluate(test_images, test_labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Plotting my accuracy and loss**","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(20,12))\n\nplt.subplot(211)\nplt.style.use('dark_background')\nplt.plot(history.history['accuracy'], color='c', label=\"Training accuracy\")\nplt.ylabel('Accuracy')\nplt.xlabel('Epochs')\nplt.legend(loc='best')\n\nplt.subplot(212)\nplt.style.use('dark_background')\nplt.plot(history.history['loss'], color='m', label=\"Training loss\")\nplt.ylabel('Loss')\nplt.legend(loc='best')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prepare and send the submission to the output directory**","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv('../input/digit-recognizer/test.csv')\ntest_data = np.array(test_data, dtype=np.float32)/255\ntest_data = test_data.reshape(-1,28,28,1)\nprediction = model.predict(test_data)\npredict = np.array(np.round(prediction), dtype = np.int32)\npredict = np.argmax(predict , axis=1).reshape(-1, 1)\nout = [{'ImageId': i+1, 'Label': predict[i][0]} for i in range(len(predict))]\npd.DataFrame(out).to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **This is to remove a submission from the output directory**","metadata":{}},{"cell_type":"code","source":"#os.remove(\"/kaggle/working/submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}