{"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":"# 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd #for reading csv files\nimport numpy as np \nimport tensorflow as tf \n\nfrom tensorflow import keras #we are going to code using keras\nfrom keras.models import Sequential, Model #we can use Sequential or Model to define model\nfrom keras.layers import Dense, BatchNormalization, Activation, Input #Importing layers which we will use in the model\nfrom keras.utils.np_utils import to_categorical #for encoding target labels\nfrom keras.callbacks import ReduceLROnPlateau #this will allow learning rate to be changed during training","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:20:26.522073Z","iopub.execute_input":"2022-01-31T08:20:26.522424Z","iopub.status.idle":"2022-01-31T08:20:32.560441Z","shell.execute_reply.started":"2022-01-31T08:20:26.522338Z","shell.execute_reply":"2022-01-31T08:20:32.559516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv', header='infer')\ntest = pd.read_csv('../input/digit-recognizer/test.csv', header='infer')\n\ny_train=train[\"label\"].values\nX_train=train.iloc[:,1:].values\n\nX_test=test.values\n\n# del train, test\nprint(type(X_train), X_train.shape) \nprint(type(X_test), X_test.shape) \nprint(type(y_train), y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:20:55.058567Z","iopub.execute_input":"2022-01-31T08:20:55.058837Z","iopub.status.idle":"2022-01-31T08:20:58.576865Z","shell.execute_reply.started":"2022-01-31T08:20:55.058807Z","shell.execute_reply":"2022-01-31T08:20:58.576136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=(X_train-127.5)/127.5\nX_test=(X_test-127.5)/127.5\n\ny_train=to_categorical(y_train)\nprint(type(y_train), y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:21:40.190286Z","iopub.execute_input":"2022-01-31T08:21:40.190579Z","iopub.status.idle":"2022-01-31T08:21:40.428145Z","shell.execute_reply.started":"2022-01-31T08:21:40.190546Z","shell.execute_reply":"2022-01-31T08:21:40.426592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reduceLROnPlateau=ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:21:53.079905Z","iopub.execute_input":"2022-01-31T08:21:53.080258Z","iopub.status.idle":"2022-01-31T08:21:53.084895Z","shell.execute_reply.started":"2022-01-31T08:21:53.080222Z","shell.execute_reply":"2022-01-31T08:21:53.083849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Dense(units=512, input_shape=(784,))) \nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(units=256))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(units=128))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(units=64))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(units=32))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(units=10))\nmodel.add(BatchNormalization())\nmodel.add(Activation('softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# model.summary()\n# tf.keras.utils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:21:56.322476Z","iopub.execute_input":"2022-01-31T08:21:56.323318Z","iopub.status.idle":"2022-01-31T08:21:56.57073Z","shell.execute_reply.started":"2022-01-31T08:21:56.323276Z","shell.execute_reply":"2022-01-31T08:21:56.569821Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x=X_train, y=y_train, epochs=150, batch_size=120, validation_split=0.2)#, callbacks=[reduceLROnPlateau])","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:22:20.123603Z","iopub.execute_input":"2022-01-31T08:22:20.12433Z","iopub.status.idle":"2022-01-31T08:30:00.008367Z","shell.execute_reply.started":"2022-01-31T08:22:20.124281Z","shell.execute_reply":"2022-01-31T08:30:00.007552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_test, verbose=1)\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:30:41.746884Z","iopub.execute_input":"2022-01-31T08:30:41.747226Z","iopub.status.idle":"2022-01-31T08:30:43.096654Z","shell.execute_reply.started":"2022-01-31T08:30:41.747189Z","shell.execute_reply":"2022-01-31T08:30:43.0957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hist.history[\"acc\"])\nplt.plot(hist.history['val_acc'])\nplt.plot(hist.history['loss'])\nplt.plot(hist.history['val_loss'])\nplt.title(\"model accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.legend([\"Accuracy\",\"Validation Accuracy\",\"loss\",\"Validation Loss\"])\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=model.predict(X_test) #make predictions on test images\nprint(predictions.shape) #This will be 28K x 10. For each test image, probability of 10 classes\n\n#Decide class label for each test image based on max probability.\n#This will be vector of size 28K. \npredictions=np.argmax(predictions,axis=1) \nprint(predictions.shape) \n\n\n\nsub=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv', header='infer')\nsub[\"Label\"]=predictions\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T08:31:08.697837Z","iopub.execute_input":"2022-01-31T08:31:08.698154Z","iopub.status.idle":"2022-01-31T08:31:11.267673Z","shell.execute_reply.started":"2022-01-31T08:31:08.698121Z","shell.execute_reply":"2022-01-31T08:31:11.267045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}