{"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","execution":{"iopub.status.busy":"2023-06-17T20:01:05.120737Z","iopub.execute_input":"2023-06-17T20:01:05.121010Z","iopub.status.idle":"2023-06-17T20:01:05.136099Z","shell.execute_reply.started":"2023-06-17T20:01:05.120985Z","shell.execute_reply":"2023-06-17T20:01:05.135109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers.convolutional import Conv2D,MaxPooling2D\nfrom keras.layers.core import Activation,Flatten,Dense\nfrom keras import backend as K\nimport pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:01:05.137985Z","iopub.execute_input":"2023-06-17T20:01:05.138395Z","iopub.status.idle":"2023-06-17T20:01:14.571425Z","shell.execute_reply.started":"2023-06-17T20:01:05.138364Z","shell.execute_reply":"2023-06-17T20:01:14.570480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preparing training data\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ny_train = np.array(data['label'])\nprint(f'y_train shape :{y_train.shape}')\nx_train = np.array(data.drop('label',axis=1)).reshape(42000,28,-1)\nprint(f'x_train shape :{x_train.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:30:57.323757Z","iopub.execute_input":"2023-06-17T20:30:57.324147Z","iopub.status.idle":"2023-06-17T20:31:00.636540Z","shell.execute_reply.started":"2023-06-17T20:30:57.324117Z","shell.execute_reply":"2023-06-17T20:31:00.635411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalizing values and adding depth dimension\nX_train = x_train/255\nX_train = np.expand_dims(X_train,1).reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:01:18.249771Z","iopub.execute_input":"2023-06-17T20:01:18.250181Z","iopub.status.idle":"2023-06-17T20:01:18.358010Z","shell.execute_reply.started":"2023-06-17T20:01:18.250146Z","shell.execute_reply":"2023-06-17T20:01:18.356984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LeNet:\n\n    def build(width,height,depth,classes):\n\n        #initializing model:\n        model = Sequential()\n        input_shape = (height,width,depth)\n\n        #if we are using \"channels first\", update the input shape\n        if K.image_data_format() == 'channels_first':\n            input_shape = (depth,height,width)\n\n        # first set of CONV => RELU => POOL layers:\n        model.add(Conv2D(20,(5,5),padding='same',input_shape=input_shape))\n        model.add(Activation('relu'))\n        model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n        # second set of CONV => RELU => POOL layers:\n        model.add(Conv2D(50,(5,5),padding='same'))\n        model.add(Activation('relu'))\n        model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n        # first (and only) set of FC => RELU layers:\n        model.add(Flatten())\n        model.add(Dense(500))\n        model.add(Activation('relu'))\n\n        # Softmax Classifier:\n        model.add(Dense(classes))\n        model.add(Activation('softmax'))\n\n        # return the constructed network architecture\n        return model","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:01:18.368690Z","iopub.execute_input":"2023-06-17T20:01:18.369225Z","iopub.status.idle":"2023-06-17T20:01:18.381851Z","shell.execute_reply.started":"2023-06-17T20:01:18.369191Z","shell.execute_reply":"2023-06-17T20:01:18.380730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import SGD\nfrom sklearn.preprocessing import LabelBinarizer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom keras.datasets import mnist\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:01:18.383529Z","iopub.execute_input":"2023-06-17T20:01:18.383939Z","iopub.status.idle":"2023-06-17T20:01:19.012599Z","shell.execute_reply.started":"2023-06-17T20:01:18.383905Z","shell.execute_reply":"2023-06-17T20:01:19.011222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the labels from integers to vectors\nle = LabelBinarizer()\nY_train = le.fit_transform(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:02:05.151794Z","iopub.execute_input":"2023-06-17T20:02:05.152215Z","iopub.status.idle":"2023-06-17T20:02:05.167091Z","shell.execute_reply.started":"2023-06-17T20:02:05.152178Z","shell.execute_reply":"2023-06-17T20:02:05.166206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# initialize the optimizer and model\nprint('[INFO] compiling model ...')\nopt = SGD(learning_rate=0.01)\nmodel = LeNet.build(28,28,1,10)\nmodel.compile(loss='categorical_crossentropy',optimizer=opt,\n              metrics=['accuracy'])\n\n# train the network\nH = model.fit(X_train,Y_train,batch_size=128,\n              verbose=1,epochs=20)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:01:19.038628Z","iopub.execute_input":"2023-06-17T20:01:19.038881Z","iopub.status.idle":"2023-06-17T20:02:05.149679Z","shell.execute_reply.started":"2023-06-17T20:01:19.038859Z","shell.execute_reply":"2023-06-17T20:02:05.148692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the training loss and accuracy\nplt.style.use(\"ggplot\")\nplt.figure()\nplt.plot(np.arange(0, 20), H.history[\"loss\"], label=\"train_loss\")\nplt.plot(np.arange(0, 20), H.history[\"accuracy\"], label=\"train_acc\")\nplt.title(\"Training Loss and Accuracy\")\nplt.xlabel(\"Epoch #\")\nplt.ylabel(\"Loss/Accuracy\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:35:19.511946Z","iopub.execute_input":"2023-06-17T20:35:19.512593Z","iopub.status.idle":"2023-06-17T20:35:19.871160Z","shell.execute_reply.started":"2023-06-17T20:35:19.512563Z","shell.execute_reply":"2023-06-17T20:35:19.870268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preparing test data\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nx_test = np.array(test_data)\nprint(x_test.shape)\nX_test = x_test/255\nX_test = np.expand_dims(X_test,1).reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:41:39.226262Z","iopub.execute_input":"2023-06-17T20:41:39.226901Z","iopub.status.idle":"2023-06-17T20:41:41.085955Z","shell.execute_reply.started":"2023-06-17T20:41:39.226866Z","shell.execute_reply":"2023-06-17T20:41:41.084845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:41:41.088276Z","iopub.execute_input":"2023-06-17T20:41:41.088665Z","iopub.status.idle":"2023-06-17T20:41:43.902308Z","shell.execute_reply.started":"2023-06-17T20:41:41.088625Z","shell.execute_reply":"2023-06-17T20:41:43.901299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:41:43.904103Z","iopub.execute_input":"2023-06-17T20:41:43.904488Z","iopub.status.idle":"2023-06-17T20:41:43.914249Z","shell.execute_reply.started":"2023-06-17T20:41:43.904454Z","shell.execute_reply":"2023-06-17T20:41:43.913104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nresult[\"Label\"] = [np.argmax(result) for result in predictions]\nresult.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T20:41:44.981943Z","iopub.execute_input":"2023-06-17T20:41:44.984336Z","iopub.status.idle":"2023-06-17T20:41:45.270879Z","shell.execute_reply.started":"2023-06-17T20:41:44.984291Z","shell.execute_reply":"2023-06-17T20:41:45.269799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}