{"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":"# Autoencoder ","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport PIL\nimport matplotlib.pyplot as plt\n\nfrom keras.datasets import mnist\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras import Model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-28T12:24:17.267811Z","iopub.execute_input":"2023-08-28T12:24:17.268267Z","iopub.status.idle":"2023-08-28T12:24:17.274750Z","shell.execute_reply.started":"2023-08-28T12:24:17.268232Z","shell.execute_reply":"2023-08-28T12:24:17.273497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading MNIST Dataset","metadata":{}},{"cell_type":"code","source":"(x_train, _), (x_test, _) = mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-08-28T12:03:38.063030Z","iopub.execute_input":"2023-08-28T12:03:38.063449Z","iopub.status.idle":"2023-08-28T12:03:39.801225Z","shell.execute_reply.started":"2023-08-28T12:03:38.063417Z","shell.execute_reply":"2023-08-28T12:03:39.800264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reshaping and Normalizing","metadata":{}},{"cell_type":"code","source":"x_train = x_train.astype('float32') / 255.0\nx_test = x_test.astype('float32') / 255.0\nx_train = x_train.reshape(len(x_train),np.prod(x_train.shape[1:]))\nx_test = x_test.reshape(len(x_test),np.prod(x_test.shape[1:]))","metadata":{"execution":{"iopub.status.busy":"2023-08-28T12:03:40.412377Z","iopub.execute_input":"2023-08-28T12:03:40.412780Z","iopub.status.idle":"2023-08-28T12:03:40.529931Z","shell.execute_reply.started":"2023-08-28T12:03:40.412746Z","shell.execute_reply":"2023-08-28T12:03:40.528669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Autoencoder Model","metadata":{}},{"cell_type":"code","source":"class MNISTAutoEncoder(Model):\n    def __init__(self,input_shape, encoding_dims):\n        super().__init__()\n        self.latent_space = encoding_dims\n        self.encoder = Dense(self.latent_space, activation=\"relu\")\n        self.decoder = Dense(input_shape, activation=\"sigmoid\")\n    \n    def call(self, x):\n        encoded = self.encoder(x)\n        decoded = self.decoder(encoded)\n        return decoded\n    \nLATENT_SPACE = 20\nINPUT_SHAPE = x_train.shape[1]\n\nautoencoder = MNISTAutoEncoder(INPUT_SHAPE, LATENT_SPACE)\nautoencoder.compile(optimizer=\"adam\",loss=\"binary_crossentropy\")\nhistory = autoencoder.fit(x_train, x_train, epochs=20, validation_split=0.1, shuffle=True,batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T12:29:26.679948Z","iopub.execute_input":"2023-08-28T12:29:26.680663Z","iopub.status.idle":"2023-08-28T12:30:50.695077Z","shell.execute_reply.started":"2023-08-28T12:29:26.680625Z","shell.execute_reply":"2023-08-28T12:30:50.694051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### History Plot","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['loss'], label=\"loss\")\nplt.plot(history.history['val_loss'], label=\"validation_loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-08-28T12:30:54.484544Z","iopub.execute_input":"2023-08-28T12:30:54.484982Z","iopub.status.idle":"2023-08-28T12:30:54.848958Z","shell.execute_reply.started":"2023-08-28T12:30:54.484949Z","shell.execute_reply":"2023-08-28T12:30:54.847719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing","metadata":{}},{"cell_type":"code","source":"encoded = autoencoder.encoder(x_test)\ndecoded = autoencoder.decoder(encoded)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T12:30:58.138732Z","iopub.execute_input":"2023-08-28T12:30:58.139128Z","iopub.status.idle":"2023-08-28T12:30:58.198691Z","shell.execute_reply.started":"2023-08-28T12:30:58.139099Z","shell.execute_reply":"2023-08-28T12:30:58.197744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sample_index = 10\n\nplt.figure(figsize=(10, 5))\n\nplt.subplot(1, 2, 1)  \nplt.imshow(x_test[test_sample_index].reshape(28,28), cmap='gray')\nplt.title('Real Image')\n\nplt.subplot(1, 2, 2)\nplt.imshow(decoded[test_sample_index].numpy().reshape(28,28), cmap='gray')\nplt.title('Autoencoder Reconstruction')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-28T12:30:58.850151Z","iopub.execute_input":"2023-08-28T12:30:58.850569Z","iopub.status.idle":"2023-08-28T12:30:59.533194Z","shell.execute_reply.started":"2023-08-28T12:30:58.850540Z","shell.execute_reply":"2023-08-28T12:30:59.531807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Thank You!","metadata":{}}]}