{"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)\nfrom sklearn.model_selection import train_test_split\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":"2021-12-06T15:02:44.735945Z","iopub.execute_input":"2021-12-06T15:02:44.736616Z","iopub.status.idle":"2021-12-06T15:02:44.746487Z","shell.execute_reply.started":"2021-12-06T15:02:44.736577Z","shell.execute_reply":"2021-12-06T15:02:44.745659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Importing required Packages**","metadata":{}},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # This will ignore information messages from Tensorflow\n\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n#Plots\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\n#Tensorflow\nimport tensorflow as tf\nfrom tensorflow import keras # Highlevel API for tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import optimizers\n\n# To run on GPU\nphysical_devices = tf.config.list_physical_devices('GPU')\ntf.config.experimental.set_memory_growth(physical_devices[0],True)","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:45.635446Z","iopub.execute_input":"2021-12-06T15:02:45.635883Z","iopub.status.idle":"2021-12-06T15:02:45.649402Z","shell.execute_reply.started":"2021-12-06T15:02:45.635845Z","shell.execute_reply":"2021-12-06T15:02:45.648441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Reading the data**","metadata":{}},{"cell_type":"code","source":"# read training data from CSV file \ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nprediction_data =  pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nsample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:46.258812Z","iopub.execute_input":"2021-12-06T15:02:46.259578Z","iopub.status.idle":"2021-12-06T15:02:49.588664Z","shell.execute_reply.started":"2021-12-06T15:02:46.259529Z","shell.execute_reply":"2021-12-06T15:02:49.587831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y,X seperation\nx_train = data.drop(['label'],axis = 1)\ny_train = data[['label']]\n\n#Train, Test Split\nx_train, x_test, y_train, y_test = train_test_split(x_train,y_train, \n                                        train_size=0.99,\n                                        test_size=0.01, \n                                         random_state=42, \n                                         shuffle=True)\nprint('x_train ->' , x_train.shape)\nprint('y_train ->' , y_train.shape)\nprint('x_test ->' , x_test.shape)\nprint('y_test ->' , y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:49.591327Z","iopub.execute_input":"2021-12-06T15:02:49.591750Z","iopub.status.idle":"2021-12-06T15:02:49.841570Z","shell.execute_reply.started":"2021-12-06T15:02:49.591712Z","shell.execute_reply":"2021-12-06T15:02:49.840781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train).astype(\"float32\")/255.0 # To make the input between 0 & 1\nx_test = np.array(x_test).astype(\"float32\")/255.0 # To make the input between 0 & 1","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:49.842947Z","iopub.execute_input":"2021-12-06T15:02:49.843302Z","iopub.status.idle":"2021-12-06T15:02:50.000395Z","shell.execute_reply.started":"2021-12-06T15:02:49.843261Z","shell.execute_reply":"2021-12-06T15:02:49.999527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Modelling**","metadata":{}},{"cell_type":"code","source":"# Sequential Model\n# 1st way of Defining a model\nmodel1 = keras.Sequential(\n    [   \n        keras.Input(shape = (x_train.shape[1])), #Input\n        layers.Dense(512, activation = 'relu'), #1st hidden layer\n        layers.Dense(256, activation = 'relu'), #2nd hidden layer\n        layers.Dense(10), # Number of labels we have 1 -10\n    \n    ]\n)\n# Model Configuration\nmodel1.compile (\n    loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer = optimizers.Adam(learning_rate = 0.001),\n    metrics = [\"accuracy\"],\n)\nprint('Method 1')\nprint(model1.summary())","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:50.002663Z","iopub.execute_input":"2021-12-06T15:02:50.003028Z","iopub.status.idle":"2021-12-06T15:02:50.047548Z","shell.execute_reply.started":"2021-12-06T15:02:50.002986Z","shell.execute_reply":"2021-12-06T15:02:50.046750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2nd way of Defining a model\n# Whats the advantage of defining the model this way?\n# we can use print(model.summary()) inbetween anywhere easy for debugging\nmodel2 = keras.Sequential()\nmodel2.add(keras.Input(shape = (x_train.shape[1])))\nmodel2.add(layers.Dense(512,activation = 'relu'))\n#print(model.summary())\nmodel2.add(layers.Dense(256,activation = 'relu'))\nmodel2.add(layers.Dense(10))\n\n# Model compile\nmodel2.compile (\n    loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer = optimizers.Adam(learning_rate = 0.001),\n    metrics = [\"accuracy\"],\n)\n\nprint('Method 2')\nprint(model2.summary())","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:50.048915Z","iopub.execute_input":"2021-12-06T15:02:50.049231Z","iopub.status.idle":"2021-12-06T15:02:50.091554Z","shell.execute_reply.started":"2021-12-06T15:02:50.049190Z","shell.execute_reply":"2021-12-06T15:02:50.090644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#3rd way of Defining a model\n# Whats the advantage of defining the model this way?\n# This is called function API method. The above two methods can handel only one set of input & output\n# This method can have multiple inputs and outputs\n# Most flexible option for creating tensorflow arch\n\ninputs = keras.Input(shape = (x_train.shape[1])) # Defining the input\nx = layers.Dense(512,activation = 'relu')(inputs) # Passing the inputs\nx = layers.Dense(512,activation = 'relu')(x)\nx = layers.Dense(512,activation = 'relu')(x)\nx = layers.Dense(256,activation = 'relu')(x)\nx = layers.Dense(256,activation = 'relu')(x) # Passing the previous output (x)\noutputs = layers.Dense(10,activation = 'softmax')(x) # Passing the previous output (x)\n\nmodel3 = keras.Model(inputs=inputs, outputs=outputs, name=\"Model-3\")\n\n\n# Model compile\nmodel3.compile (\n    loss = keras.losses.SparseCategoricalCrossentropy(from_logits=False), # since we have declared activation function 'sigmod'\n    optimizer = optimizers.Adam(learning_rate = 0.001),\n    metrics = [\"accuracy\"],\n)\n\nprint('Method 3')\nprint(model3.summary())","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:50.093224Z","iopub.execute_input":"2021-12-06T15:02:50.093508Z","iopub.status.idle":"2021-12-06T15:02:50.153711Z","shell.execute_reply.started":"2021-12-06T15:02:50.093475Z","shell.execute_reply":"2021-12-06T15:02:50.152779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print('Model-1')\n# model1.fit(x_train, y_train,batch_size=32, epochs=5,verbose=2)\n# print(\"Evaluation on Test set\")\n# model1.evaluate(x_test,y_test, batch_size=32,verbose = 2)\n# print('\\n')\n# print('\\n')\n# print('Model-2')\n# model2.fit(x_train, y_train,batch_size=32, epochs=5,verbose=2)\n# print(\"Evaluation on Test set\")\n# model2.evaluate(x_test,y_test, batch_size=32,verbose = 2)\n# print('\\n')\n# print('\\n')\nprint('Model-3')\nmodel3.fit(x_train, y_train,batch_size=32, epochs=5,verbose=2)\nprint(\"Evaluation on Test set\")\nmodel3.evaluate(x_test,y_test, batch_size=32,verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:02:50.155327Z","iopub.execute_input":"2021-12-06T15:02:50.155597Z","iopub.status.idle":"2021-12-06T15:03:03.590506Z","shell.execute_reply.started":"2021-12-06T15:02:50.155559Z","shell.execute_reply":"2021-12-06T15:03:03.589797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_result = np.array(prediction_data)","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:03:03.597518Z","iopub.execute_input":"2021-12-06T15:03:03.597747Z","iopub.status.idle":"2021-12-06T15:03:03.667314Z","shell.execute_reply.started":"2021-12-06T15:03:03.597721Z","shell.execute_reply":"2021-12-06T15:03:03.666500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_results = []\nfor array in (prediction_result):\n    pred_results.append(np.argmax(array))","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:03:03.668751Z","iopub.execute_input":"2021-12-06T15:03:03.668991Z","iopub.status.idle":"2021-12-06T15:03:03.837827Z","shell.execute_reply.started":"2021-12-06T15:03:03.668958Z","shell.execute_reply":"2021-12-06T15:03:03.837158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred = pd.DataFrame(pred_results, columns= ['Label'])\n# #ImageId\tLabel\n# sample_submission = sample_submission.drop('Label', axis = 1)\n# final_submission = pd.concat([sample_submission, pred],axis =1)\nfinal_submission.to_csv('final_submission1.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-12-06T15:03:28.078859Z","iopub.execute_input":"2021-12-06T15:03:28.079238Z","iopub.status.idle":"2021-12-06T15:03:28.135836Z","shell.execute_reply.started":"2021-12-06T15:03:28.079206Z","shell.execute_reply":"2021-12-06T15:03:28.135158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-12-06T14:52:19.067762Z","iopub.execute_input":"2021-12-06T14:52:19.068486Z","iopub.status.idle":"2021-12-06T14:52:19.074593Z","shell.execute_reply.started":"2021-12-06T14:52:19.068446Z","shell.execute_reply":"2021-12-06T14:52:19.073867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-12-06T14:52:41.077772Z","iopub.execute_input":"2021-12-06T14:52:41.078252Z","iopub.status.idle":"2021-12-06T14:52:41.167677Z","shell.execute_reply.started":"2021-12-06T14:52:41.078189Z","shell.execute_reply":"2021-12-06T14:52:41.166879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle competitions submit -c digit-recognizer -f final_submission.csv -m \"First Submission\"","metadata":{"execution":{"iopub.status.busy":"2021-12-06T14:54:06.016083Z","iopub.execute_input":"2021-12-06T14:54:06.017148Z","iopub.status.idle":"2021-12-06T14:54:07.023376Z","shell.execute_reply.started":"2021-12-06T14:54:06.017074Z","shell.execute_reply":"2021-12-06T14:54:07.022467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}