{"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 tensorflow as tf\nimport pandas as pd\nimport numpy as np\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-30T16:20:33.607995Z","iopub.execute_input":"2022-10-30T16:20:33.608357Z","iopub.status.idle":"2022-10-30T16:20:33.613790Z","shell.execute_reply.started":"2022-10-30T16:20:33.608326Z","shell.execute_reply":"2022-10-30T16:20:33.612738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:33.620218Z","iopub.execute_input":"2022-10-30T16:20:33.620492Z","iopub.status.idle":"2022-10-30T16:20:36.787255Z","shell.execute_reply.started":"2022-10-30T16:20:33.620468Z","shell.execute_reply":"2022-10-30T16:20:36.786279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = train.copy()\npd.set_option('display.max_columns' , 784)\n#df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:36.789203Z","iopub.execute_input":"2022-10-30T16:20:36.789592Z","iopub.status.idle":"2022-10-30T16:20:36.893112Z","shell.execute_reply.started":"2022-10-30T16:20:36.789557Z","shell.execute_reply":"2022-10-30T16:20:36.892159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1 = df_train.drop('label',axis=1)\ny1 = df_train['label']\n\nprint(X1.shape , y1.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:36.894710Z","iopub.execute_input":"2022-10-30T16:20:36.895084Z","iopub.status.idle":"2022-10-30T16:20:36.978301Z","shell.execute_reply.started":"2022-10-30T16:20:36.895051Z","shell.execute_reply":"2022-10-30T16:20:36.977209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1 = (X1.values.reshape(-1, 28, 28, 1)) / 225\nprint(X1.shape , y1.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:36.981236Z","iopub.execute_input":"2022-10-30T16:20:36.982342Z","iopub.status.idle":"2022-10-30T16:20:37.086599Z","shell.execute_reply.started":"2022-10-30T16:20:36.982302Z","shell.execute_reply":"2022-10-30T16:20:37.085547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X2, y2), (x_test, y_test) = keras.datasets.mnist.load_data()\n\nX2 = (np.reshape(X2 ,(-1, 28, 28, 1))) / 255\n\nprint(X2.shape , y2.shape)\nprint(X1.shape, y1.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.088211Z","iopub.execute_input":"2022-10-30T16:20:37.088929Z","iopub.status.idle":"2022-10-30T16:20:37.448603Z","shell.execute_reply.started":"2022-10-30T16:20:37.088889Z","shell.execute_reply":"2022-10-30T16:20:37.447566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.concatenate([X1 , X2] , axis =0)\ny = np.concatenate([y1 , y2] , axis =0)\nprint(X.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.450131Z","iopub.execute_input":"2022-10-30T16:20:37.450525Z","iopub.status.idle":"2022-10-30T16:20:37.705867Z","shell.execute_reply.started":"2022-10-30T16:20:37.450472Z","shell.execute_reply":"2022-10-30T16:20:37.704927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X,y,test_size =0.1)\n\nprint('training features shape ',X_train.shape)\nprint('training labels shape ',y_train.shape)\nprint('test features shape ',X_test.shape)\nprint('test labels shape ',y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.708123Z","iopub.execute_input":"2022-10-30T16:20:37.709070Z","iopub.status.idle":"2022-10-30T16:20:37.908484Z","shell.execute_reply.started":"2022-10-30T16:20:37.709030Z","shell.execute_reply":"2022-10-30T16:20:37.907364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resblock(x , filters , kernelsize):\n    fx = layers.Conv2D(filters , kernelsize , activation = 'relu' , padding = 'same')(x)\n    fx = layers.BatchNormalization()(fx)\n    fx = layers.Conv2D(filters , kernelsize , activation = 'relu' , padding = 'same')(fx)\n    out = layers.Add()([fx , x])\n    out = layers.ReLU()(out)\n    out = layers.BatchNormalization()(out)\n\n    return out\n","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.909866Z","iopub.execute_input":"2022-10-30T16:20:37.910440Z","iopub.status.idle":"2022-10-30T16:20:37.916895Z","shell.execute_reply.started":"2022-10-30T16:20:37.910402Z","shell.execute_reply":"2022-10-30T16:20:37.915877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = keras.Input(shape=(28, 28,1))\ninputs.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.918301Z","iopub.execute_input":"2022-10-30T16:20:37.918897Z","iopub.status.idle":"2022-10-30T16:20:37.931467Z","shell.execute_reply.started":"2022-10-30T16:20:37.918861Z","shell.execute_reply":"2022-10-30T16:20:37.930545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model(input):\n\n    resblock1 = resblock(input , 32, 3)\n    conv1 = layers.Conv2D(64 , 3)(resblock1)\n    resblock2 = resblock(conv1 , 64 , 3)\n    conv2 = layers.Conv2D(128 , 3)(resblock2)\n    resblock3 = resblock(conv2 , 128 , 3)\n    conv3 = layers.Conv2D(256 , 3)(resblock3)\n    resblock4 = resblock(conv3 , 256 , 3)\n    flatten = layers.Flatten()(resblock4)\n    dense1 = layers.Dense(256 , activation = 'relu')(flatten)\n    out = layers.Dense(10 , activation ='softmax')(dense1)\n    \n    return out\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.936178Z","iopub.execute_input":"2022-10-30T16:20:37.936538Z","iopub.status.idle":"2022-10-30T16:20:37.944051Z","shell.execute_reply.started":"2022-10-30T16:20:37.936469Z","shell.execute_reply":"2022-10-30T16:20:37.943009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Model(inputs=inputs, outputs=model(inputs), name=\"mnist_model\")","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:37.945403Z","iopub.execute_input":"2022-10-30T16:20:37.945821Z","iopub.status.idle":"2022-10-30T16:20:38.114669Z","shell.execute_reply.started":"2022-10-30T16:20:37.945787Z","shell.execute_reply":"2022-10-30T16:20:38.113801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer=keras.optimizers.RMSprop(),\n    metrics=[\"accuracy\"],)\n\nhistory = model.fit(X_train, y_train, batch_size=64, epochs=11, validation_data=(X_test,y_test) , verbose =1)\n\ntest_scores = model.evaluate(X_test, y_test, verbose=1)\nprint(\"Test loss:\", test_scores[0])\nprint(\"Test accuracy:\", test_scores[1])\n","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:20:38.116055Z","iopub.execute_input":"2022-10-30T16:20:38.116445Z","iopub.status.idle":"2022-10-30T16:32:36.971716Z","shell.execute_reply.started":"2022-10-30T16:20:38.116409Z","shell.execute_reply":"2022-10-30T16:32:36.970564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test.copy()\nX_test = X_test/255.0\nX_test = X_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:32:36.973031Z","iopub.execute_input":"2022-10-30T16:32:36.973992Z","iopub.status.idle":"2022-10-30T16:32:37.134906Z","shell.execute_reply.started":"2022-10-30T16:32:36.973953Z","shell.execute_reply":"2022-10-30T16:32:37.133576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\ny_pred = np.argmax(y_pred, axis=1)\ny_pred[:10]","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:32:37.138652Z","iopub.execute_input":"2022-10-30T16:32:37.139042Z","iopub.status.idle":"2022-10-30T16:32:48.038235Z","shell.execute_reply.started":"2022-10-30T16:32:37.139006Z","shell.execute_reply":"2022-10-30T16:32:48.036664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name = \"mnist.csv\"\ny_pred = pd.Series(y_pred, name='Label')\nsub = pd.concat([pd.Series(range(1, 28001), name=\"ImageId\"), y_pred], axis=1)\nsub.to_csv(file_name, index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T16:32:48.039449Z","iopub.execute_input":"2022-10-30T16:32:48.039909Z","iopub.status.idle":"2022-10-30T16:32:48.076264Z","shell.execute_reply.started":"2022-10-30T16:32:48.039872Z","shell.execute_reply":"2022-10-30T16:32:48.075332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}