{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom scipy import signal\nimport cv2\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Activation, Flatten\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D\nfrom keras.optimizers import SGD\nfrom keras.utils import np_utils\n\nimport tensorflow as tf\nfrom keras.backend.tensorflow_backend import set_session\ngpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1) \nconfig = tf.ConfigProto(gpu_options=gpu_options)\nset_session(tf.Session(config=config))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/mnist_train.csv')\ntest_df = pd.read_csv('../input/mnist_test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4a96580bd7bef3aebd414d545e2f8fbc2654a89"},"cell_type":"code","source":"print(train_df.shape)\nprint(test_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ba007c52161df01b4a46800dec9527f81902591"},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ddee35f477609a1c4152bd3f4db0f1a63dd5517"},"cell_type":"code","source":"train_df['label'].value_counts().plot(kind='bar')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4e7b4add2c8963ea2d66dd1e80f0441e81f34ff","collapsed":true},"cell_type":"code","source":"feat_cols = train_df.columns.tolist()[1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"97dc77dfb2809ffe4272572c01fb47f434cd4ae6"},"cell_type":"code","source":"X_train = train_df[feat_cols].values.reshape(-1,28,28,1)/255\ny_train = train_df['label'].values\nX_test = test_df[feat_cols].values.reshape(-1,28,28,1)/255\ny_test = test_df['label'].values","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"0f75b4a374165335e32203b5e8ca3c6bb4c5de8b"},"cell_type":"code","source":"plt.imshow(X_test[234][:,:,0], cmap='Greys')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cad980bbc172e110738aab039c5dc68672d3d69","collapsed":true},"cell_type":"code","source":"plt.gray()\nplt.imshow([[1,-1]])\nplt.show()\nplt.imshow([[1],[-1]])\nplt.show()\nplt.imshow(X_train[12345][:,:,0])\nplt.show()\nplt.imshow(signal.convolve(X_test[234][:,:,0],[[1,-1]]))\nplt.show()\nplt.imshow(signal.convolve(X_test[234][:,:,0],[[1],[-1]]))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"273e316672f20de01d74f6878533e9e553dbe4b5"},"cell_type":"markdown","source":"# 1. Simple Deep Neural Network"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"a01b3cadcbdb2c5e7c002fc2a432c1cf4fad5d3a"},"cell_type":"code","source":"print(X_test.dtype, X_test.shape)\nprint(y_test.dtype, y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd5380407068798a8a1766641deeb4f954837a64","collapsed":true},"cell_type":"code","source":"num_pixels = X_test.shape[1] * X_test.shape[2] # 28*28=784\nX_train = X_train.reshape(-1, num_pixels).astype('float32')\nX_test = X_test.reshape(-1, num_pixels).astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d8bf54ca14099a3380f9f1ffeb8a6616e98f404"},"cell_type":"code","source":"print(X_train.dtype, X_train.shape)\nprint(X_test.dtype, X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"379fc7fd93f0af9139c61a14fcfc80a72ad5d07c"},"cell_type":"code","source":"y_train = np_utils.to_categorical(y_train)\ny_test = np_utils.to_categorical(y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb58a80287bbf77250f06568c21c275cda30023a"},"cell_type":"code","source":"print(y_train.dtype)\nprint(y_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e31a06b5658162902da0a3ff6ed7bba51a3443fe"},"cell_type":"code","source":"num_classes = len(set(train_df['label'].values)) # already one-hot encoded for labels, so cant use y_train\nnum_classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0a29321cd090ae28b55f685ffbdc919cc0da6d70","_kg_hide-input":false,"_kg_hide-output":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(num_pixels, input_dim=num_pixels, activation='relu')) # in=out hidde layer\nmodel.add(Dense(num_classes, activation='softmax')) #output layer\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"189395d43236e76b816f4eea9658537c32585c20","scrolled":false},"cell_type":"code","source":"history = model.fit(X_train,y_train, validation_data=(X_test,y_test), batch_size=128, epochs=10, verbose=1)\n# verbos=True = print training process","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7ac18e00982ed30015668a15887cad2d33b82c5"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8356384285762eef5dfa5d60ee4528978733895a"},"cell_type":"markdown","source":"# 2. Convolution Neural Network"},{"metadata":{"trusted":true,"_uuid":"ae6690c0dcb6740a8d64fe2b8d56125a075f6500"},"cell_type":"code","source":"print(X_test.shape, type(X_test), X_test.dtype)\nprint(y_test.shape, type(y_test), y_test.dtype)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b1276b63489f0a3eb44e4282eb698ba804ef2c43"},"cell_type":"markdown","source":"because CNN need to read a image dim, that's why we need to reshape image dataset to (60000,28,28,1)"},{"metadata":{"trusted":true,"_uuid":"48ac29d72a9307d481362c79f7d39a42a3b22ac4"},"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\nX_test = X_test.reshape(-1,28,28,1)\ny_train = y_train.astype('float32')\ny_test = y_test.astype('float32')\nprint(X_test.shape, type(X_test), X_test.dtype)\nprint(y_test.shape, type(y_test), y_test.dtype)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ec0b90992e20e720f6371927ee8d01f0985e5c75"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=32,kernel_size=3,input_shape=(28,28,1), activation='relu'))\nmodel.add(Conv2D(32, 3, activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(64, 3, activation='relu'))\nmodel.add(Conv2D(64, 3, activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5197bff3388b65aa43425cee3f9f6012f1d5dd20"},"cell_type":"code","source":"history = model.fit(X_train,y_train, validation_data=(X_test,y_test), batch_size=128, epochs=10, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03dabc68791a93cbf935242166823911a3a1a32c"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a91f806257f80845fba6c4b94af33ad2cac89c53"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7d76d0d93a18e0a4d4a8b846dbd18c74d03cccf6"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"248064bc57af7fb5386b594855818dde3255845b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5efc12d5eb3dfed82cfa2d07abe943db43fb1d69"},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}