{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30407,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-19T20:25:09.111671Z","iopub.execute_input":"2024-02-19T20:25:09.112407Z","iopub.status.idle":"2024-02-19T20:25:09.151572Z","shell.execute_reply.started":"2024-02-19T20:25:09.112376Z","shell.execute_reply":"2024-02-19T20:25:09.150561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:09.153130Z","iopub.execute_input":"2024-02-19T20:25:09.153421Z","iopub.status.idle":"2024-02-19T20:25:09.158069Z","shell.execute_reply.started":"2024-02-19T20:25:09.153393Z","shell.execute_reply":"2024-02-19T20:25:09.157158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:09.159392Z","iopub.execute_input":"2024-02-19T20:25:09.159711Z","iopub.status.idle":"2024-02-19T20:25:12.949697Z","shell.execute_reply.started":"2024-02-19T20:25:09.159683Z","shell.execute_reply":"2024-02-19T20:25:12.948501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:12.952278Z","iopub.execute_input":"2024-02-19T20:25:12.952612Z","iopub.status.idle":"2024-02-19T20:25:12.979681Z","shell.execute_reply.started":"2024-02-19T20:25:12.952582Z","shell.execute_reply":"2024-02-19T20:25:12.978516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:12.981054Z","iopub.execute_input":"2024-02-19T20:25:12.981460Z","iopub.status.idle":"2024-02-19T20:25:12.988676Z","shell.execute_reply.started":"2024-02-19T20:25:12.981419Z","shell.execute_reply":"2024-02-19T20:25:12.987601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().any().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:12.990258Z","iopub.execute_input":"2024-02-19T20:25:12.991198Z","iopub.status.idle":"2024-02-19T20:25:13.009154Z","shell.execute_reply.started":"2024-02-19T20:25:12.991168Z","shell.execute_reply":"2024-02-19T20:25:13.008158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_y = df[[\"label\"]]\ndf_train = df.drop(\"label\",axis = 1)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:13.010515Z","iopub.execute_input":"2024-02-19T20:25:13.010974Z","iopub.status.idle":"2024-02-19T20:25:13.115697Z","shell.execute_reply.started":"2024-02-19T20:25:13.010936Z","shell.execute_reply":"2024-02-19T20:25:13.114634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_y.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:13.117137Z","iopub.execute_input":"2024-02-19T20:25:13.117457Z","iopub.status.idle":"2024-02-19T20:25:13.126291Z","shell.execute_reply.started":"2024-02-19T20:25:13.117426Z","shell.execute_reply":"2024-02-19T20:25:13.125042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m, n = df_train.shape\n\n\"\"\"fig, axes = plt.subplots(8,8, figsize=(8,8))\nfig.tight_layout(pad=0.1)\nprint(axes.shape)\nx = df_train.to_numpy()\ny = df_y.to_numpy()\nfor i,ax in enumerate(axes.flat):\n    # Select random indices\n    rand_index = np.random.randint(m)\n    \n    # Select rows corresponding to the random indices and\n    # reshape the image\n    x_rand_reshaped = x[rand_index].reshape((28,28)).T\n    \n    # Display the image\n    ax.imshow(x_rand_reshaped, cmap='gray')\n    \n    # Display the label above the image\n    ax.set_title(y[rand_index,0])\n    ax.set_axis_off()\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:13.128160Z","iopub.execute_input":"2024-02-19T20:25:13.128470Z","iopub.status.idle":"2024-02-19T20:25:13.135885Z","shell.execute_reply.started":"2024-02-19T20:25:13.128440Z","shell.execute_reply":"2024-02-19T20:25:13.134845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.activations import linear, relu, sigmoid","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:13.138974Z","iopub.execute_input":"2024-02-19T20:25:13.139348Z","iopub.status.idle":"2024-02-19T20:25:22.445724Z","shell.execute_reply.started":"2024-02-19T20:25:13.139318Z","shell.execute_reply":"2024-02-19T20:25:22.444712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model = Sequential(\n[\n    tf.keras.Input(shape =(784)),\n    Dense(25,activation = 'relu'),\n    Dense(15,activation = 'relu'),\n    Dense(10, activation = 'linear')\n], name = \"my_model\"\n)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:28.971725Z","iopub.execute_input":"2023-03-04T14:13:28.972617Z","iopub.status.idle":"2023-03-04T14:13:28.980478Z","shell.execute_reply.started":"2023-03-04T14:13:28.972560Z","shell.execute_reply":"2023-03-04T14:13:28.979315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-26T10:59:04.669557Z","iopub.execute_input":"2023-02-26T10:59:04.669983Z","iopub.status.idle":"2023-02-26T10:59:04.695968Z","shell.execute_reply.started":"2023-02-26T10:59:04.669939Z","shell.execute_reply":"2023-02-26T10:59:04.694727Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.compile(\n    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    \n    optimizer = tf.keras.optimizers.Adam(learning_rate = 0.001),\n)\nmodel.fit(\n    df_train,df_y,\n    epochs = 50\n)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:28.982347Z","iopub.execute_input":"2023-03-04T14:13:28.982981Z","iopub.status.idle":"2023-03-04T14:13:28.997938Z","shell.execute_reply.started":"2023-03-04T14:13:28.982938Z","shell.execute_reply":"2023-03-04T14:13:28.996727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"m,n = df_train.shape\nfig, axes = plt.subplots(8,8, figsize=(6,6))\nfig.tight_layout(pad=0.1)\nx = df_train.to_numpy()\ny = df_y.to_numpy()\nfor i,ax in enumerate(axes.flat):\n    # Select random indices\n    rand_index = np.random.randint(m)\n    \n    # Select rows corresponding to the random indices and\n    # reshape the image\n    x_rand_reshaped = x[rand_index].reshape((28,28)).T\n    \n    # Display the image\n    ax.imshow(x_rand_reshaped, cmap='gray')\n    #predections\n    prediction = model.predict(x[rand_index].reshape(1,784))\n    predict_n = tf.nn.softmax(prediction)\n    y_pred = np.argmax(predict_n)\n    ax.set_title(f\"{y[rand_index,0]},{y_pred}\",fontsize=10)\n    ax.set_axis_off()\nfig.suptitle(\"Label, yhat\", fontsize=14)\nplt.show()\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:28.999570Z","iopub.execute_input":"2023-03-04T14:13:29.000298Z","iopub.status.idle":"2023-03-04T14:13:29.010839Z","shell.execute_reply.started":"2023-03-04T14:13:29.000259Z","shell.execute_reply":"2023-03-04T14:13:29.009729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ndf_sample.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:29.012627Z","iopub.execute_input":"2023-03-04T14:13:29.013145Z","iopub.status.idle":"2023-03-04T14:13:29.043272Z","shell.execute_reply.started":"2023-03-04T14:13:29.013104Z","shell.execute_reply":"2023-03-04T14:13:29.042125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:29.044712Z","iopub.execute_input":"2023-03-04T14:13:29.045054Z","iopub.status.idle":"2023-03-04T14:13:30.908824Z","shell.execute_reply.started":"2023-03-04T14:13:29.045021Z","shell.execute_reply":"2023-03-04T14:13:30.907653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.910439Z","iopub.execute_input":"2023-03-04T14:13:30.913671Z","iopub.status.idle":"2023-03-04T14:13:30.919959Z","shell.execute_reply.started":"2023-03-04T14:13:30.913629Z","shell.execute_reply":"2023-03-04T14:13:30.918823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"results = model.predict(df_test)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.921682Z","iopub.execute_input":"2023-03-04T14:13:30.922139Z","iopub.status.idle":"2023-03-04T14:13:30.932798Z","shell.execute_reply.started":"2023-03-04T14:13:30.922093Z","shell.execute_reply":"2023-03-04T14:13:30.931509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#results.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.934322Z","iopub.execute_input":"2023-03-04T14:13:30.936555Z","iopub.status.idle":"2023-03-04T14:13:30.941646Z","shell.execute_reply.started":"2023-03-04T14:13:30.936484Z","shell.execute_reply":"2023-03-04T14:13:30.940705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#results = np.argmax(results,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.943489Z","iopub.execute_input":"2023-03-04T14:13:30.943997Z","iopub.status.idle":"2023-03-04T14:13:30.954172Z","shell.execute_reply.started":"2023-03-04T14:13:30.943962Z","shell.execute_reply":"2023-03-04T14:13:30.953177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#results.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.955714Z","iopub.execute_input":"2023-03-04T14:13:30.956114Z","iopub.status.idle":"2023-03-04T14:13:30.964575Z","shell.execute_reply.started":"2023-03-04T14:13:30.956079Z","shell.execute_reply":"2023-03-04T14:13:30.963630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#results = pd.Series(results,name=\"Label\")\n#results","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.966122Z","iopub.execute_input":"2023-03-04T14:13:30.966531Z","iopub.status.idle":"2023-03-04T14:13:30.976886Z","shell.execute_reply.started":"2023-03-04T14:13:30.966497Z","shell.execute_reply":"2023-03-04T14:13:30.975890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n#submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.978437Z","iopub.execute_input":"2023-03-04T14:13:30.978864Z","iopub.status.idle":"2023-03-04T14:13:30.988715Z","shell.execute_reply.started":"2023-03-04T14:13:30.978830Z","shell.execute_reply":"2023-03-04T14:13:30.987440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:25:26.395714Z","iopub.execute_input":"2024-02-19T20:25:26.397032Z","iopub.status.idle":"2024-02-19T20:25:30.994918Z","shell.execute_reply.started":"2024-02-19T20:25:26.396988Z","shell.execute_reply":"2024-02-19T20:25:30.993752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train.to_numpy()[0:,1:].reshape(42000,28,28,1)\ntest_image = test.to_numpy().reshape(28000,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:32.933422Z","iopub.execute_input":"2024-02-19T20:27:32.934548Z","iopub.status.idle":"2024-02-19T20:27:32.941215Z","shell.execute_reply.started":"2024-02-19T20:27:32.934493Z","shell.execute_reply":"2024-02-19T20:27:32.939909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train_image.astype(float) / 255.0\ntest_image = test_image.astype(float) / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:33.209889Z","iopub.execute_input":"2024-02-19T20:27:33.210881Z","iopub.status.idle":"2024-02-19T20:27:33.395785Z","shell.execute_reply.started":"2024-02-19T20:27:33.210840Z","shell.execute_reply":"2024-02-19T20:27:33.394561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = train_image[0]\nprint(img.shape)\nplt.imshow(img.squeeze())","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:33.434414Z","iopub.execute_input":"2024-02-19T20:27:33.434794Z","iopub.status.idle":"2024-02-19T20:27:33.732920Z","shell.execute_reply.started":"2024-02-19T20:27:33.434763Z","shell.execute_reply":"2024-02-19T20:27:33.731697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras.layers import InputLayer, Conv2D, LeakyReLU, Conv2DTranspose\nfrom keras.losses import MeanSquaredError\nfrom keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:33.734935Z","iopub.execute_input":"2024-02-19T20:27:33.735437Z","iopub.status.idle":"2024-02-19T20:27:33.741916Z","shell.execute_reply.started":"2024-02-19T20:27:33.735394Z","shell.execute_reply":"2024-02-19T20:27:33.740521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AutoEncoder(keras.Model):\n    def __init__(self,input_shape):\n        super(AutoEncoder,self).__init__()\n        #encoder\n        self.encoder = keras.Sequential([\n            InputLayer(input_shape=input_shape),\n            Conv2D(64,kernel_size=3,strides=2,padding='same'),\n            LeakyReLU(0.001),\n            Conv2D(64,kernel_size=3,strides=2,padding='same'),\n            LeakyReLU(0.001)\n        ])\n\n        #decoder\n        self.decoder = keras.Sequential([\n            Conv2DTranspose(64,kernel_size=3,strides=2,padding='same'),\n            LeakyReLU(0.001),\n            Conv2DTranspose(64,kernel_size=3,strides=2,padding='same'),\n            LeakyReLU(0.001),\n            Conv2D(input_shape[2],kernel_size=3,padding='same'),\n            LeakyReLU(0.001)\n        ])\n\n    def call(self,x):\n        encoded = self.encoder(x)\n        decoded = self.decoder(encoded)\n        return decoded","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:33.910313Z","iopub.execute_input":"2024-02-19T20:27:33.910624Z","iopub.status.idle":"2024-02-19T20:27:33.921345Z","shell.execute_reply.started":"2024-02-19T20:27:33.910595Z","shell.execute_reply":"2024-02-19T20:27:33.920323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae = AutoEncoder((28,28,1))","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:34.184689Z","iopub.execute_input":"2024-02-19T20:27:34.185686Z","iopub.status.idle":"2024-02-19T20:27:37.639302Z","shell.execute_reply.started":"2024-02-19T20:27:34.185642Z","shell.execute_reply":"2024-02-19T20:27:37.638413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = Adam(0.0001)\nloss = MeanSquaredError()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:37.641320Z","iopub.execute_input":"2024-02-19T20:27:37.641697Z","iopub.status.idle":"2024-02-19T20:27:37.646350Z","shell.execute_reply.started":"2024-02-19T20:27:37.641620Z","shell.execute_reply":"2024-02-19T20:27:37.645150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae.compile(optimizer = opt,loss = loss)","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:27:37.647713Z","iopub.execute_input":"2024-02-19T20:27:37.648098Z","iopub.status.idle":"2024-02-19T20:27:37.667322Z","shell.execute_reply.started":"2024-02-19T20:27:37.648055Z","shell.execute_reply":"2024-02-19T20:27:37.666364Z"},"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":"2024-02-19T20:27:37.669292Z","iopub.execute_input":"2024-02-19T20:27:37.669569Z","iopub.status.idle":"2024-02-19T20:28:20.121994Z","shell.execute_reply.started":"2024-02-19T20:27:37.669542Z","shell.execute_reply":"2024-02-19T20:28:20.120784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:28:20.123230Z","iopub.execute_input":"2024-02-19T20:28:20.123560Z","iopub.status.idle":"2024-02-19T20:28:20.142713Z","shell.execute_reply.started":"2024-02-19T20:28:20.123529Z","shell.execute_reply":"2024-02-19T20:28:20.141675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 10\nplt.figure(figsize=(20, 4))\nfor i in range(n):\n    encoded = ae.encoder(tf.expand_dims(test_image[i],axis=0))\n    cleaned = ae.decoder(encoded)\n    ax = plt.subplot(2, n, i + 1)\n    plt.title(\"original\")\n    plt.imshow(tf.squeeze(test_image[i]))\n    plt.gray()\n    ax.get_xaxis().set_visible(False)\n    ax.get_yaxis().set_visible(False)\n    bx = plt.subplot(2, n, i + n + 1)\n    plt.title(\"reconstructed\")\n    plt.imshow(tf.squeeze(cleaned))\n    plt.gray()\n    bx.get_xaxis().set_visible(False)\n    bx.get_yaxis().set_visible(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:28:20.143934Z","iopub.execute_input":"2024-02-19T20:28:20.144203Z","iopub.status.idle":"2024-02-19T20:28:21.011406Z","shell.execute_reply.started":"2024-02-19T20:28:20.144178Z","shell.execute_reply":"2024-02-19T20:28:21.010363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.ylim([0, 1])\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2024-02-19T20:28:21.012776Z","iopub.execute_input":"2024-02-19T20:28:21.013106Z","iopub.status.idle":"2024-02-19T20:28:21.179281Z","shell.execute_reply.started":"2024-02-19T20:28:21.013075Z","shell.execute_reply":"2024-02-19T20:28:21.178009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Autoencoder","metadata":{}},{"cell_type":"code","source":"fig,axes = plt.subplots(1,8, figsize=(8,8))\ntrain = df_train.to_numpy()\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(train[i].reshape(28,28),cmap = \"gray\")\n    ax.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:30.991506Z","iopub.execute_input":"2023-03-04T14:13:30.992457Z","iopub.status.idle":"2023-03-04T14:13:31.265270Z","shell.execute_reply.started":"2023-03-04T14:13:30.992416Z","shell.execute_reply":"2023-03-04T14:13:31.264265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras import layers","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:31.267014Z","iopub.execute_input":"2023-03-04T14:13:31.267751Z","iopub.status.idle":"2023-03-04T14:13:31.272863Z","shell.execute_reply.started":"2023-03-04T14:13:31.267712Z","shell.execute_reply":"2023-03-04T14:13:31.271400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#auto encoder\nencoding_dim = 32\n\ninput_img = keras.Input(shape = (784,))\n\nencoded = layers.Dense(encoding_dim, activation = 'relu')(input_img)\n\ndecoded = layers.Dense(784,activation = 'sigmoid')(encoded)\n\nautoencode = keras.Model(input_img,decoded)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:31.275028Z","iopub.execute_input":"2023-03-04T14:13:31.275805Z","iopub.status.idle":"2023-03-04T14:13:35.196380Z","shell.execute_reply.started":"2023-03-04T14:13:31.275769Z","shell.execute_reply":"2023-03-04T14:13:35.195331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#encoding layer\nencoder = keras.Model(input_img,encoded)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:35.198061Z","iopub.execute_input":"2023-03-04T14:13:35.198410Z","iopub.status.idle":"2023-03-04T14:13:35.209446Z","shell.execute_reply.started":"2023-03-04T14:13:35.198374Z","shell.execute_reply":"2023-03-04T14:13:35.208437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_input = keras.Input(shape=(encoding_dim,))\n\ndecoder_layer = autoencode.layers[-1]\n\ndecoder = keras.Model(encoded_input, decoder_layer(encoded_input))","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:35.211059Z","iopub.execute_input":"2023-03-04T14:13:35.211425Z","iopub.status.idle":"2023-03-04T14:13:35.238600Z","shell.execute_reply.started":"2023-03-04T14:13:35.211389Z","shell.execute_reply":"2023-03-04T14:13:35.237586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencode.compile(optimizer='adam', loss='binary_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:35.244898Z","iopub.execute_input":"2023-03-04T14:13:35.245167Z","iopub.status.idle":"2023-03-04T14:13:35.262572Z","shell.execute_reply.started":"2023-03-04T14:13:35.245142Z","shell.execute_reply":"2023-03-04T14:13:35.261639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train/255\nprint(train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:14:32.511117Z","iopub.execute_input":"2023-03-04T14:14:32.511504Z","iopub.status.idle":"2023-03-04T14:14:32.623960Z","shell.execute_reply.started":"2023-03-04T14:14:32.511470Z","shell.execute_reply":"2023-03-04T14:14:32.622750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencode.fit(train, train,\n                epochs=50,\n                batch_size=256,\n                shuffle=True,\n                )","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:13:35.380029Z","iopub.execute_input":"2023-03-04T14:13:35.380470Z","iopub.status.idle":"2023-03-04T14:14:18.087856Z","shell.execute_reply.started":"2023-03-04T14:13:35.380429Z","shell.execute_reply":"2023-03-04T14:14:18.086586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_imgs = encoder.predict(train)\ndecoded_imgs = decoder.predict(encoded_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:14:18.089784Z","iopub.execute_input":"2023-03-04T14:14:18.090459Z","iopub.status.idle":"2023-03-04T14:14:25.941948Z","shell.execute_reply.started":"2023-03-04T14:14:18.090415Z","shell.execute_reply":"2023-03-04T14:14:25.940843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axes = plt.subplots(1,8, figsize=(8,8))\ntrain = df_train.to_numpy()\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(train[i].reshape(28,28),cmap = \"gray\")\n    ax.axis('off')\n\nfig,axes = plt.subplots(1,8, figsize=(8,8))\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(encoded_imgs[i].reshape(4,8),cmap = \"gray\")\n    ax.axis('off')\nfig,axes = plt.subplots(1,8, figsize=(8,8))\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(decoded_imgs[i].reshape(28,28),cmap = \"gray\")\n    ax.axis('off')\n","metadata":{"execution":{"iopub.status.busy":"2023-03-04T14:14:25.943934Z","iopub.execute_input":"2023-03-04T14:14:25.944310Z","iopub.status.idle":"2023-03-04T14:14:26.732128Z","shell.execute_reply.started":"2023-03-04T14:14:25.944272Z","shell.execute_reply":"2023-03-04T14:14:26.730819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the loss in data is high as we can see through the basic approach","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}