{"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 numpy as np \nimport pandas as pd\nimport tensorflow as tf\n# from tensorflow import keras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-09T18:14:44.942555Z","iopub.execute_input":"2022-05-09T18:14:44.943398Z","iopub.status.idle":"2022-05-09T18:14:44.948526Z","shell.execute_reply.started":"2022-05-09T18:14:44.943340Z","shell.execute_reply":"2022-05-09T18:14:44.947612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:45.002397Z","iopub.execute_input":"2022-05-09T18:14:45.002974Z","iopub.status.idle":"2022-05-09T18:14:49.486474Z","shell.execute_reply.started":"2022-05-09T18:14:45.002933Z","shell.execute_reply":"2022-05-09T18:14:49.485624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_df.drop(columns=['label'])\ny_train = train_df['label']\nX_test = test_df","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.487988Z","iopub.execute_input":"2022-05-09T18:14:49.488213Z","iopub.status.idle":"2022-05-09T18:14:49.578566Z","shell.execute_reply.started":"2022-05-09T18:14:49.488184Z","shell.execute_reply":"2022-05-09T18:14:49.577720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.579877Z","iopub.execute_input":"2022-05-09T18:14:49.580229Z","iopub.status.idle":"2022-05-09T18:14:49.596235Z","shell.execute_reply.started":"2022-05-09T18:14:49.580199Z","shell.execute_reply":"2022-05-09T18:14:49.595339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\nX_train = X_train.reshape(X_train.shape[0],28,28,1)\n\nX_test = np.array(X_test)\nX_test = X_test.reshape(X_test.shape[0],28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.598534Z","iopub.execute_input":"2022-05-09T18:14:49.599572Z","iopub.status.idle":"2022-05-09T18:14:49.785460Z","shell.execute_reply.started":"2022-05-09T18:14:49.599327Z","shell.execute_reply":"2022-05-09T18:14:49.784443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.astype('float32') / 255.\nX_test = X_test.astype('float32') / 255.","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.786941Z","iopub.execute_input":"2022-05-09T18:14:49.787319Z","iopub.status.idle":"2022-05-09T18:14:49.925986Z","shell.execute_reply.started":"2022-05-09T18:14:49.787276Z","shell.execute_reply":"2022-05-09T18:14:49.925022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nn_classes = 10\ny_train = to_categorical(y_train, n_classes)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.927370Z","iopub.execute_input":"2022-05-09T18:14:49.927739Z","iopub.status.idle":"2022-05-09T18:14:49.934478Z","shell.execute_reply.started":"2022-05-09T18:14:49.927697Z","shell.execute_reply":"2022-05-09T18:14:49.933116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.935880Z","iopub.execute_input":"2022-05-09T18:14:49.936151Z","iopub.status.idle":"2022-05-09T18:14:49.946727Z","shell.execute_reply.started":"2022-05-09T18:14:49.936118Z","shell.execute_reply":"2022-05-09T18:14:49.945899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.reshape(X_train, (42000,784,))\nX_test = np.reshape(X_test, (28000,784,))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.948193Z","iopub.execute_input":"2022-05-09T18:14:49.948685Z","iopub.status.idle":"2022-05-09T18:14:49.959124Z","shell.execute_reply.started":"2022-05-09T18:14:49.948649Z","shell.execute_reply":"2022-05-09T18:14:49.958314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.960442Z","iopub.execute_input":"2022-05-09T18:14:49.961206Z","iopub.status.idle":"2022-05-09T18:14:49.974177Z","shell.execute_reply.started":"2022-05-09T18:14:49.961168Z","shell.execute_reply":"2022-05-09T18:14:49.973041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten\nfrom keras.models import Model\n\nx1 = Input(shape=(784,))\nencoder1 = Dense(512, activation='relu')(x1)\ndecoder1 = Dense(784, activation='sigmoid')(encoder1)\nautoencoder1 = Model(x1, decoder1)\nautoencoder1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:49.976323Z","iopub.execute_input":"2022-05-09T18:14:49.976683Z","iopub.status.idle":"2022-05-09T18:14:50.017781Z","shell.execute_reply.started":"2022-05-09T18:14:49.976642Z","shell.execute_reply":"2022-05-09T18:14:50.016843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder1.compile(optimizer='adam', loss='mse')\nautoencoder1.fit(X_train, X_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:14:50.019132Z","iopub.execute_input":"2022-05-09T18:14:50.019377Z","iopub.status.idle":"2022-05-09T18:15:59.507854Z","shell.execute_reply.started":"2022-05-09T18:14:50.019346Z","shell.execute_reply":"2022-05-09T18:15:59.507042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input_layer = Input(shape=(784,))\n# hidden1 = Dense(units = 512, activation='sigmoid')(input_layer)\n# hidden2 = Dense(units = 350, activation='sigmoid')(hidden1)\n# hidden3 = Dense(units = 256, activation='sigmoid')(hidden2)\n# hidden4 = Dense(units = 128, activation='sigmoid')(hidden3)\n# output1 = Dense(units=784, activation='sigmoid')(hidden4)\n# autoencoder1 = Model(inputs = input_layer, outputs = output1)\n# autoencoder1.summary()\n# autoencoder1.compile(loss = 'mse', optimizer = 'adam', metrics = ['accuracy'])\n# autoencoder1.fit(X_train , X_train , epochs = 15)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:15:59.509372Z","iopub.execute_input":"2022-05-09T18:15:59.509649Z","iopub.status.idle":"2022-05-09T18:15:59.513637Z","shell.execute_reply.started":"2022-05-09T18:15:59.509619Z","shell.execute_reply":"2022-05-09T18:15:59.512734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder1_hidden_output = autoencoder1.layers[1].output\ntrimmed_autoencoder1 = Model(x1,autoencoder1_hidden_output)\nX_train1 = trimmed_autoencoder1.predict(X_train)\nX_test1 = trimmed_autoencoder1.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:15:59.514916Z","iopub.execute_input":"2022-05-09T18:15:59.515137Z","iopub.status.idle":"2022-05-09T18:16:02.892820Z","shell.execute_reply.started":"2022-05-09T18:15:59.515110Z","shell.execute_reply":"2022-05-09T18:16:02.891992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x2 = Input(shape=(512,))\nencoder2 = Dense(256, activation='relu')(x2)\ndecoder2 = Dense(512, activation='sigmoid')(encoder2)\nautoencoder2 = Model(x2, decoder2)\nautoencoder2.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:16:02.894076Z","iopub.execute_input":"2022-05-09T18:16:02.894921Z","iopub.status.idle":"2022-05-09T18:16:02.930213Z","shell.execute_reply.started":"2022-05-09T18:16:02.894874Z","shell.execute_reply":"2022-05-09T18:16:02.929183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder2.compile(optimizer='adam', loss='mse')\nautoencoder2.fit(X_train1, X_train1, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:16:02.931760Z","iopub.execute_input":"2022-05-09T18:16:02.931991Z","iopub.status.idle":"2022-05-09T18:19:58.841461Z","shell.execute_reply.started":"2022-05-09T18:16:02.931963Z","shell.execute_reply":"2022-05-09T18:19:58.840535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder2_hidden_output = autoencoder2.layers[1].output\ntrimmed_autoencoder2=Model(x2,autoencoder2_hidden_output)\nX_train2 = trimmed_autoencoder2.predict(X_train1)\nX_test2 = trimmed_autoencoder2.predict(X_test1)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:19:58.843103Z","iopub.execute_input":"2022-05-09T18:19:58.843432Z","iopub.status.idle":"2022-05-09T18:20:02.820626Z","shell.execute_reply.started":"2022-05-09T18:19:58.843388Z","shell.execute_reply":"2022-05-09T18:20:02.819825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x3 = Input(shape=(256,))\noutput = Dense(10, activation='softmax')(x3)\nautoencoder3 = Model(x3,output)\nautoencoder3.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:20:13.291529Z","iopub.execute_input":"2022-05-09T18:20:13.292132Z","iopub.status.idle":"2022-05-09T18:20:13.315350Z","shell.execute_reply.started":"2022-05-09T18:20:13.292069Z","shell.execute_reply":"2022-05-09T18:20:13.314383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder3.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nautoencoder3.fit(X_train2, y_train, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:20:15.949907Z","iopub.execute_input":"2022-05-09T18:20:15.950441Z","iopub.status.idle":"2022-05-09T18:22:11.984849Z","shell.execute_reply.started":"2022-05-09T18:20:15.950389Z","shell.execute_reply":"2022-05-09T18:22:11.983862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = autoencoder3.predict(X_test2)\nfinal_predictions=np.argmax(predictions, axis=1)\nprint(final_predictions)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:22:20.318576Z","iopub.execute_input":"2022-05-09T18:22:20.318846Z","iopub.status.idle":"2022-05-09T18:22:21.270688Z","shell.execute_reply.started":"2022-05-09T18:22:20.318816Z","shell.execute_reply":"2022-05-09T18:22:21.269767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission['label'] = final_predictions\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:39:03.224708Z","iopub.execute_input":"2022-05-09T18:39:03.225262Z","iopub.status.idle":"2022-05-09T18:39:03.296535Z","shell.execute_reply.started":"2022-05-09T18:39:03.225211Z","shell.execute_reply":"2022-05-09T18:39:03.295799Z"},"trusted":true},"execution_count":null,"outputs":[]}]}