{"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-05-04T16:16:24.500227Z","iopub.execute_input":"2023-05-04T16:16:24.500654Z","iopub.status.idle":"2023-05-04T16:16:24.510241Z","shell.execute_reply.started":"2023-05-04T16:16:24.500612Z","shell.execute_reply":"2023-05-04T16:16:24.509133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import all required libraries","metadata":{}},{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom keras.models import Sequential\nfrom keras.layers import InputLayer, Dense, Flatten, Lambda\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom keras.callbacks import EarlyStopping\n","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:24.512435Z","iopub.execute_input":"2023-05-04T16:16:24.513105Z","iopub.status.idle":"2023-05-04T16:16:24.525640Z","shell.execute_reply.started":"2023-05-04T16:16:24.513072Z","shell.execute_reply":"2023-05-04T16:16:24.524602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load train and test data","metadata":{}},{"cell_type":"code","source":"# create the training & test sets, skipping the header row with [1:]\ntrain = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\n\n# Create numpy input data\ntrain_full_X = (train.iloc[:,1:].values).astype('float32') # all pixel values\ntrain_full_y = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\ntest_X = (test.iloc[:,1:].values).astype('float32')\n\n# Reshape\ntrain_full_X = train_full_X.reshape(train_full_X.shape[0], 28, 28,1)\ntest_X = test_X.reshape(test_X.shape[0], 28, 28,1)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:24.526940Z","iopub.execute_input":"2023-05-04T16:16:24.528103Z","iopub.status.idle":"2023-05-04T16:16:28.301374Z","shell.execute_reply.started":"2023-05-04T16:16:24.528069Z","shell.execute_reply":"2023-05-04T16:16:28.299917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Designing Convolutional Neural Network Architecture","metadata":{}},{"cell_type":"code","source":"model = Sequential([  \n    Flatten(input_shape=(28,28)),\n    Dense(100, activation='relu'),\n    Dense(10, activation='softmax')\n    ])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:28.302838Z","iopub.execute_input":"2023-05-04T16:16:28.303191Z","iopub.status.idle":"2023-05-04T16:16:28.340833Z","shell.execute_reply.started":"2023-05-04T16:16:28.303161Z","shell.execute_reply":"2023-05-04T16:16:28.339722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compile the network","metadata":{}},{"cell_type":"code","source":"optimizer = keras.optimizers.Adam()\nmodel.compile(optimizer=optimizer,\n loss='sparse_categorical_crossentropy',\n metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:28.343672Z","iopub.execute_input":"2023-05-04T16:16:28.344015Z","iopub.status.idle":"2023-05-04T16:16:28.357524Z","shell.execute_reply.started":"2023-05-04T16:16:28.343986Z","shell.execute_reply":"2023-05-04T16:16:28.356115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split the data","metadata":{}},{"cell_type":"code","source":"train_X, val_X, train_y, val_y = train_test_split(train_full_X, train_full_y, test_size=0.10, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:28.359539Z","iopub.execute_input":"2023-05-04T16:16:28.359975Z","iopub.status.idle":"2023-05-04T16:16:28.872093Z","shell.execute_reply.started":"2023-05-04T16:16:28.359932Z","shell.execute_reply":"2023-05-04T16:16:28.870814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Run the model","metadata":{}},{"cell_type":"code","source":"early_stopping = EarlyStopping(\n    patience=5,\n    min_delta=0.001,\n    restore_best_weights=True,\n)\nhistory = model.fit(\n    train_X, train_y,\n    validation_data=(val_X, val_y),\n    batch_size=512,\n    epochs=200,\n    callbacks=[early_stopping],verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:28.873488Z","iopub.execute_input":"2023-05-04T16:16:28.873808Z","iopub.status.idle":"2023-05-04T16:16:53.504068Z","shell.execute_reply.started":"2023-05-04T16:16:28.873782Z","shell.execute_reply":"2023-05-04T16:16:53.503002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot(title=\"Cross-entropy\")\nhistory_df.loc[:, ['accuracy', 'val_accuracy']].plot(title=\"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:16:53.505487Z","iopub.execute_input":"2023-05-04T16:16:53.505820Z","iopub.status.idle":"2023-05-04T16:17:11.176303Z","shell.execute_reply.started":"2023-05-04T16:16:53.505792Z","shell.execute_reply":"2023-05-04T16:17:11.175145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_X)\n\noutput = pd.DataFrame({'id': test.id, 'label': np.argmax(predictions, axis=1)})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:17:11.177855Z","iopub.execute_input":"2023-05-04T16:17:11.178715Z","iopub.status.idle":"2023-05-04T16:17:11.651224Z","shell.execute_reply.started":"2023-05-04T16:17:11.178676Z","shell.execute_reply":"2023-05-04T16:17:11.649779Z"},"trusted":true},"execution_count":null,"outputs":[]}]}