{"cells":[{"metadata":{},"cell_type":"markdown","source":"Hi This version will have 0.995 Accuracy.\nIn this version I am using CNN.One of the Simplest model.\nTotal params: 288,618\nTrainable params: 288,170\nNon-trainable params: 448\nIt has 6 Conv2D and with 32 , 64 and 128 Filters.\n\nI used multiple type of batch size line 40,128,256,512,1024 and 4096. \nFor Batch size 4096 i got best input.\n","execution_count":null},{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nimport tensorflow as tf\n\nfrom sklearn.model_selection import train_test_split\nprint(\"We're using TF\", tf.__version__)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.shape,test.shape)\ny = train['label']\ntrain=train.drop(['label'],axis =1)\ntrain = train/255\ntest = test/255\n\ndf_train ,df_val,y_train,y_val = train_test_split(train,y,test_size=0.2, random_state=111)\nprint(df_train.shape,df_val.shape,y_train.shape,y_val.shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\ny_train_oh = keras.utils.to_categorical(y_train, 10)\ny_val_oh = keras.utils.to_categorical(y_val, 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df_train.shape,df_val.shape,y_train_oh.shape,y_val_oh.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dims = df_train.shape\nprint(dims[0],dims)\ndize2  = (dims[0],28,28,1)\ndf_train_fn = df_train.to_numpy()\ndf_train_f = df_train_fn.reshape((dize2))\ndf_val_fn = df_val.to_numpy()\ndf_val_f = df_val_fn.reshape((df_val_fn.shape[0],28,28,1))\ndf_test_fn = test.to_numpy()\ndf_test_f = df_test_fn.reshape((df_test_fn.shape[0],28,28,1))\nprint(df_train_f.shape, df_val_f.shape,df_test_f.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.layers import Dropout, Flatten, Input, Dense\n\ndef create_model():\n    \n    def add_conv_block(model, num_filters):\n        \n        model.add(Conv2D(num_filters, 3, activation='relu', padding='same'))\n        model.add(BatchNormalization())\n        model.add(Conv2D(num_filters, 3, activation='relu', padding='valid'))\n        model.add(MaxPooling2D(pool_size=2))\n        model.add(Dropout(0.2))\n\n        return model\n    \n    model = tf.keras.models.Sequential()\n    model.add(Input(shape=(28, 28, 1)))\n    \n    model = add_conv_block(model, 32)\n    model = add_conv_block(model, 64)\n    model = add_conv_block(model, 128)\n\n    model.add(Flatten())\n    model.add(Dense(10, activation='softmax'))\n\n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n    return model\n\nmodel = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"h = model.fit(\n    df_train_f, y_train_oh,\n    validation_data=(df_val_f, y_val_oh),\n    epochs=20, batch_size=4096\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.predict(df_test_f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(preds.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred1 = np.argmax(preds, axis=1)\nprint(pred1.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(pred1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final = pd.DataFrame(columns = ['ImageId','Label'])\ndf_final['Label'] = pred1\ndf_final['ImageId'] = df_final.index + 1\nnp.savetxt('final_1.csv',pred1)\ndf_final.to_csv(\"final_2.csv\")","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}