{"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":"code","source":"import pandas as pd\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:17:59.136903Z","iopub.execute_input":"2021-07-26T21:17:59.137250Z","iopub.status.idle":"2021-07-26T21:17:59.143624Z","shell.execute_reply.started":"2021-07-26T21:17:59.137222Z","shell.execute_reply":"2021-07-26T21:17:59.142273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\r\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:00.988344Z","iopub.execute_input":"2021-07-26T21:18:00.988741Z","iopub.status.idle":"2021-07-26T21:18:06.531555Z","shell.execute_reply.started":"2021-07-26T21:18:00.988710Z","shell.execute_reply":"2021-07-26T21:18:06.530286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:12.094786Z","iopub.execute_input":"2021-07-26T21:18:12.095206Z","iopub.status.idle":"2021-07-26T21:18:12.121246Z","shell.execute_reply.started":"2021-07-26T21:18:12.095174Z","shell.execute_reply":"2021-07-26T21:18:12.120017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_numpy().shape","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:13.806163Z","iopub.execute_input":"2021-07-26T21:18:13.806521Z","iopub.status.idle":"2021-07-26T21:18:13.816666Z","shell.execute_reply.started":"2021-07-26T21:18:13.806491Z","shell.execute_reply":"2021-07-26T21:18:13.815434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.to_numpy().shape","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:15.288297Z","iopub.execute_input":"2021-07-26T21:18:15.288848Z","iopub.status.idle":"2021-07-26T21:18:15.298217Z","shell.execute_reply.started":"2021-07-26T21:18:15.288811Z","shell.execute_reply":"2021-07-26T21:18:15.296766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(train.to_numpy()[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:16.292821Z","iopub.execute_input":"2021-07-26T21:18:16.293181Z","iopub.status.idle":"2021-07-26T21:18:16.302607Z","shell.execute_reply.started":"2021-07-26T21:18:16.293150Z","shell.execute_reply":"2021-07-26T21:18:16.300992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train.to_numpy()[0:,1:].reshape(42000,28,28,1)\r\ntest_image = test.to_numpy().reshape(28000,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:17.694675Z","iopub.execute_input":"2021-07-26T21:18:17.695045Z","iopub.status.idle":"2021-07-26T21:18:17.702985Z","shell.execute_reply.started":"2021-07-26T21:18:17.695016Z","shell.execute_reply":"2021-07-26T21:18:17.701650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train_image.astype(float) / 255.0\r\ntest_image = test_image.astype(float) / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:18.743422Z","iopub.execute_input":"2021-07-26T21:18:18.743842Z","iopub.status.idle":"2021-07-26T21:18:19.002374Z","shell.execute_reply.started":"2021-07-26T21:18:18.743810Z","shell.execute_reply":"2021-07-26T21:18:19.001272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = train_image[0]\r\nprint(img.shape)\r\nplt.imshow(img.squeeze())","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:19.677852Z","iopub.execute_input":"2021-07-26T21:18:19.678255Z","iopub.status.idle":"2021-07-26T21:18:19.859739Z","shell.execute_reply.started":"2021-07-26T21:18:19.678223Z","shell.execute_reply":"2021-07-26T21:18:19.858385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\r\nfrom tensorflow import keras\r\nfrom keras.layers import InputLayer, Conv2D, LeakyReLU, Conv2DTranspose\r\nfrom keras.losses import MeanSquaredError\r\nfrom keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:22.861051Z","iopub.execute_input":"2021-07-26T21:18:22.861381Z","iopub.status.idle":"2021-07-26T21:18:22.866592Z","shell.execute_reply.started":"2021-07-26T21:18:22.861333Z","shell.execute_reply":"2021-07-26T21:18:22.865364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AutoEncoder(keras.Model):\r\n    def __init__(self,input_shape):\r\n        super(AutoEncoder,self).__init__()\r\n        #encoder\r\n        self.encoder = keras.Sequential([\r\n            InputLayer(input_shape=input_shape),\r\n            Conv2D(64,kernel_size=3,strides=2,padding='same'),\r\n            LeakyReLU(0.001),\r\n            Conv2D(64,kernel_size=3,strides=2,padding='same'),\r\n            LeakyReLU(0.001)\r\n        ])\r\n\r\n        #decoder\r\n        self.decoder = keras.Sequential([\r\n            Conv2DTranspose(64,kernel_size=3,strides=2,padding='same'),\r\n            LeakyReLU(0.001),\r\n            Conv2DTranspose(64,kernel_size=3,strides=2,padding='same'),\r\n            LeakyReLU(0.001),\r\n            Conv2D(input_shape[2],kernel_size=3,padding='same'),\r\n            LeakyReLU(0.001)\r\n        ])\r\n\r\n    def call(self,x):\r\n        encoded = self.encoder(x)\r\n        decoded = self.decoder(encoded)\r\n        return decoded","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:24.340760Z","iopub.execute_input":"2021-07-26T21:18:24.341259Z","iopub.status.idle":"2021-07-26T21:18:24.350061Z","shell.execute_reply.started":"2021-07-26T21:18:24.341229Z","shell.execute_reply":"2021-07-26T21:18:24.348708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae = AutoEncoder((28,28,1))\r\n","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:26.385860Z","iopub.execute_input":"2021-07-26T21:18:26.386238Z","iopub.status.idle":"2021-07-26T21:18:26.442049Z","shell.execute_reply.started":"2021-07-26T21:18:26.386205Z","shell.execute_reply":"2021-07-26T21:18:26.440840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = Adam(0.0001)\r\nloss = MeanSquaredError()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:27.784430Z","iopub.execute_input":"2021-07-26T21:18:27.784810Z","iopub.status.idle":"2021-07-26T21:18:27.793060Z","shell.execute_reply.started":"2021-07-26T21:18:27.784778Z","shell.execute_reply":"2021-07-26T21:18:27.791915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae.compile(optimizer = opt,loss = loss)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:18:29.754665Z","iopub.execute_input":"2021-07-26T21:18:29.755039Z","iopub.status.idle":"2021-07-26T21:18:29.769538Z","shell.execute_reply.started":"2021-07-26T21:18:29.755009Z","shell.execute_reply":"2021-07-26T21:18:29.768188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = ae.fit(train_image,train_image,batch_size=128,epochs=10,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:19:15.898213Z","iopub.execute_input":"2021-07-26T21:19:15.898682Z","iopub.status.idle":"2021-07-26T21:19:55.079704Z","shell.execute_reply.started":"2021-07-26T21:19:15.898650Z","shell.execute_reply":"2021-07-26T21:19:55.078486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:19:57.348623Z","iopub.execute_input":"2021-07-26T21:19:57.349056Z","iopub.status.idle":"2021-07-26T21:19:57.363923Z","shell.execute_reply.started":"2021-07-26T21:19:57.349023Z","shell.execute_reply":"2021-07-26T21:19:57.362560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\r\nn = 10\r\nplt.figure(figsize=(20, 4))\r\nfor i in range(n):\r\n    encoded = ae.encoder(tf.expand_dims(test_image[i],axis=0))\r\n    cleaned = ae.decoder(encoded)\r\n    ax = plt.subplot(2, n, i + 1)\r\n    plt.title(\"original\")\r\n    plt.imshow(tf.squeeze(test_image[i]))\r\n    plt.gray()\r\n    ax.get_xaxis().set_visible(False)\r\n    ax.get_yaxis().set_visible(False)\r\n    bx = plt.subplot(2, n, i + n + 1)\r\n    plt.title(\"reconstructed\")\r\n    plt.imshow(tf.squeeze(cleaned))\r\n    plt.gray()\r\n    bx.get_xaxis().set_visible(False)\r\n    bx.get_yaxis().set_visible(False)\r\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:20:02.006774Z","iopub.execute_input":"2021-07-26T21:20:02.007176Z","iopub.status.idle":"2021-07-26T21:20:03.443624Z","shell.execute_reply.started":"2021-07-26T21:20:02.007141Z","shell.execute_reply":"2021-07-26T21:20:03.442507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\r\nplt.xlabel('Epoch')\r\nplt.ylabel('Loss')\r\nplt.ylim([0, 1])\r\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2021-07-26T21:20:06.297736Z","iopub.execute_input":"2021-07-26T21:20:06.298234Z","iopub.status.idle":"2021-07-26T21:20:06.574658Z","shell.execute_reply.started":"2021-07-26T21:20:06.298186Z","shell.execute_reply":"2021-07-26T21:20:06.573621Z"},"trusted":true},"execution_count":null,"outputs":[]}]}