{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install mitdeeplearning","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#%tensorflow_version 2.x\nimport tensorflow as tf\nimport mitdeeplearning as mdl\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport random\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## MNIST dataset\nmnist = tf.keras.datasets.mnist\n(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\ntrain_images = (np.expand_dims(train_images, axis = -1)/255.).astype(np.float32)\ntrain_labels = (train_labels).astype(np.int64)\ntest_images = (np.expand_dims(test_images, axis = -1)/255.).astype(np.float32)\ntest_labels = (test_labels).astype(np.int64)\nlen(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(1,20,5):\n    plt.imshow(np.squeeze(train_images[i]), cmap = plt.cm.binary)\n    plt.show()\n    print(\"label = \" + str(train_labels[i]))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport keras\nimport tensorflow as tf\n\nimport keras.layers as layers\nfrom keras import regularizers\nimport numpy as np \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Train the model \nBatch_size = 256\nEpochs = 700","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def build_cnn_model():\n    \n    cnn_model = tf.keras.Sequential([\n        \n        tf.keras.layers.Conv2D(filters = 30, kernel_size = (3,3), activation = tf.nn.relu),\n        tf.keras.layers.MaxPool2D(pool_size = (2,2)),\n        tf.keras.layers.Conv2D(filters = 40, kernel_size = (3,3), activation = tf.nn.relu),\n        tf.keras.layers.MaxPool2D(pool_size = (2,2)),\n        tf.keras.layers.Conv2D(filters = 50, kernel_size = (3,3), activation = tf.nn.relu),\n        tf.keras.layers.MaxPool2D(pool_size = (2,2)),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(128, activation = tf.nn.relu),\n        tf.keras.layers.Dense(10, activation = \"softmax\")\n    \n    ])\n    \n    return cnn_model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model = build_cnn_model()\ncnn_model.predict(train_images[[0]])\nprint(cnn_model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=.001), loss=\"sparse_categorical_crossentropy\",metrics = [\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model.fit(train_images, train_labels,batch_size = Batch_size, epochs = Epochs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = cnn_model.predict(test_images)\nprediction = np.argmax(predictions[0])\ntest_images[0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Label of this digit is \", test_labels[0])\nplt.imshow(test_images[0,:,:,0], cmap = plt.cm.binary)\n#plt.imshow(np.squeeze(test_images[0]), cmap = plt.cm.binary)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_index = 79 #@param {type:\"slider\", min:0, max:100, step:1}\nplt.subplot(1,2,1)\nmdl.lab2.plot_image_prediction(image_index, predictions, test_labels, test_images)\nplt.subplot(1,2,2)\nmdl.lab2.plot_value_prediction(image_index, predictions,  test_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plots the first X test images, their predicted label, and the true label\n# Color correct predictions in blue, incorrect predictions in red\nnum_rows = 5\nnum_cols = 4\nnum_images = num_rows*num_cols\nplt.figure(figsize=(2*num_cols, 2*num_rows))\nfor i in range(num_images):\n    plt.subplot(num_rows, 2*num_cols, 2*i+1)\n    mdl.lab2.plot_image_prediction(i, predictions, test_labels, test_images)\n    plt.subplot(num_rows, 2*num_cols, 2*i+2)\n    mdl.lab2.plot_value_prediction(i, predictions, test_labels)","execution_count":null,"outputs":[]}],"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}