{"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import keras\nimport tensorflow","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1af9a0194de4ff5076388a559cd6c9dd777be566"},"cell_type":"code","source":"from keras import layers\nfrom keras import models","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3325de48b7801209c7d3ea6f8b3f916bdc75f66"},"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b8cd1f87022c0ea4be6eff9d2d2238d0ee9f47a"},"cell_type":"code","source":"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10, activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b19f9abbb31037b0418fd13f96f6ebb7629bbdc"},"cell_type":"code","source":"import os, shutil\noriginal_dataset_dir = '../input/train/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1a0f5dcb28a0efb40d23f42a5f65a2da3dd2d33"},"cell_type":"code","source":"base_dir = '../input/train/'\nos.mkdir(base_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8429f927196eaac1524dc6551bbd67131a6cbda2"},"cell_type":"code","source":"from keras.datasets import mnist\nfrom keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"6a8542560cd219ac512d83174b13ab499fa742cc"},"cell_type":"code","source":"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\ntrain_images = train_images.reshape((60000, 28, 28, 1))\ntrain_images = train_images.astype('float32') / 255\ntest_images = test_images.reshape((10000, 28, 28, 1))\ntest_images = test_images.astype('float32') / 255\ntrain_labels = to_categorical(train_labels)\ntest_labels = to_categorical(test_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08feae6ad9d6971d5ffb0ac509a1bf8ed9830240"},"cell_type":"code","source":"model.compile(optimizer='rmsprop', \n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\nhistory = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecb3643fa50423d77b03d5803330292c74263175"},"cell_type":"code","source":"test_loss, test_acc = model.evaluate(test_images, test_labels)\ntest_acc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55ccdde5b1f430fd635712ab0c57063b70d32257"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","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}