{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n#Version of tensorflow\nprint(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"file_name = \"/kaggle/input/digit-recognizer/train.csv\"\ndata = pd.read_csv(file_name)\ntarget = pd.DataFrame(data['label'])\nfeatures = data.drop('label',axis=1)\nprint(data.shape)\nprint(data.describe)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n#Building a neural network\nmodel = tf.keras.models.Sequential()\n\nmodel.add(tf.keras.layers.Dense(128,input_dim=784,activation=tf.nn.relu))\n#model.add(tf.keras.layers.Dropout(0.3))\nmodel.add(tf.keras.layers.Dense(21,activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_23 = tf.keras.models.Sequential([\n                            tf.keras.layers.Dense(132,input_dim=784,activation = tf.nn.relu),\n                            tf.keras.layers.Dense(40,activation=tf.nn.relu),\n                            tf.keras.layers.Dense(10,activation=tf.nn.sigmoid)\n    \n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_23.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Compliing the model\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":" taking another model but using the Stochastic greadient descent algorithm as an optimizer","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model_23.compile(optimizer=\"sgd\",loss=\"sparse_categorical_crossentropy\",metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Fit the model i.e find the patterns in the training data\nmodel.fit(features,target,epochs=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_23.fit(features,target,epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scores = model.evaluate(features,target)\nprint(scores)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scores_23 = model_23.evaluate(features,target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#testing data\ntest_data=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_target = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ns = model.evaluate(test_data,test_target['Label'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features , test_data = np.array(features),np.array(test_data)\nfeatures,test_data = features.reshape(42000,28,28,1),test_data.reshape(28000,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Building a convolutional layer \nmodel_update = tf.keras.models.Sequential([tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu,input_shape=(28,28,1)),\n                                           tf.keras.layers.MaxPooling2D(2,2),\n                                           tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu,input_shape=(28,28,1)),\n                                           tf.keras.layers.MaxPooling2D(2,2),\n                                           tf.keras.layers.Flatten(),\n                                           tf.keras.layers.Dense(512,activation=tf.nn.relu),\n                                           tf.keras.layers.Dense(10,activation=tf.nn.softmax)\n                                            ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_update.summary()\n# Explains how the convolutions are acting on the images by concentrating on the main features\n# and pooling helps in compressing the image but keeping the features intact","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Compiling the Convolutional model\nmodel_update.compile(optimizer = \"adam\", loss = \"sparse_categorical_crossentropy\", metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_update.fit(features,target,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Evaluating the test data with a convolutional model, I hope there is an improvement\nmodel_update.evaluate(test_data,np.array(test_target['Label']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Not much of an improvement ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets try normalizing the features and test_data which contain the pixel values \nof the pictures being feeded into the Convolutional Neural Network\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#Normalizing\nfeatures , test_data = features , test_data\n#Converting features and test_data into numpy arrays\nfeatures , test_data = np.array(features),np.array(test_data)\n#Reshaping them\nfeatures , test_data = features.reshape(42000,28,28,1),test_data.reshape(28000,28,28,1)\n\"\"\"\nfeatures , test_data = np.array(features),np.array(test_data)\nfeatures,test_data = features.reshape(42000,28,28,1),test_data.reshape(28000,28,28,1)\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Building a Convolutional Neural Network with 3 Convolutional layers with their respective pooling\nmodel_3 = tf.keras.models.Sequential([\n                                tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu,input_shape=(28,28,1)),\n                                tf.keras.layers.MaxPooling2D(2,2),\n                                tf.keras.layers.Conv2D(32,(3,3),activation=tf.nn.relu),\n                                tf.keras.layers.MaxPooling2D(2,2),\n                                tf.keras.layers.Flatten(),\n                                tf.keras.layers.Dense(1028,activation=tf.nn.relu),\n                                tf.keras.layers.Dense(10,activation=tf.nn.softmax)\n                                     ])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Compiling the model\nmodel_3.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit the model to find the patterns\nmodel_3.fit(features,target,epochs=10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Evaluating our model on the training data (which is actually not a good idea as our model has learnt the features to look out from the training data)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model_3.evaluate(features,target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_3.evaluate(test_data,np.array(test_target['Label']))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets try another Convolutional Neural Network having 3 Convolutional layers along with Max Pooling layers","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model_4 = tf.keras.models.Sequential([\n                                tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu,input_shape=(28,28,1)),\n                                tf.keras.layers.MaxPooling2D(2,2),\n                                tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu),\n                                tf.keras.layers.MaxPooling2D(2,2),\n                                tf.keras.layers.Conv2D(32,(3,3),activation=tf.nn.relu),\n                                tf.keras.layers.MaxPooling2D(2,2),\n                                tf.keras.layers.Flatten(),\n                                tf.keras.layers.Dense(512,activation = tf.nn.relu),\n                                tf.keras.layers.Dense(10,activation = tf.nn.softmax)\n                                ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Helping us how the Convolutional layers and MaxPooling layers reduce the parameters by concentrating\n#on the main features and copressing the image by keeping the features  intact\nmodel_4.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model compiling bysetting up the optimizer(which tries to minimize the error by making sure that the weights and baises \n#helps in reaching the global minima or  good enough local minima)\n\nmodel_4.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Find the main features by update the weights and biases of the network\nmodel_4.fit(features,target,batch_size=32,epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_4.evaluate(test_data,test_target['Label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}