{"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-23T07:03:25.112794Z","iopub.execute_input":"2023-05-23T07:03:25.113481Z","iopub.status.idle":"2023-05-23T07:03:25.161983Z","shell.execute_reply.started":"2023-05-23T07:03:25.113444Z","shell.execute_reply":"2023-05-23T07:03:25.159507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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\nfrom keras.layers import Convolution2D , MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2023-05-23T07:04:13.034035Z","iopub.execute_input":"2023-05-23T07:04:13.034448Z","iopub.status.idle":"2023-05-23T07:04:23.496042Z","shell.execute_reply.started":"2023-05-23T07:04:13.034416Z","shell.execute_reply":"2023-05-23T07:04:23.494676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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-23T07:05:06.095509Z","iopub.execute_input":"2023-05-23T07:05:06.096673Z","iopub.status.idle":"2023-05-23T07:05:11.109425Z","shell.execute_reply.started":"2023-05-23T07:05:06.096604Z","shell.execute_reply":"2023-05-23T07:05:11.108123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Z-score\nmean_px = train_full_X.mean().astype(np.float32)\nstd_px = train_full_X.std().astype(np.float32)\n\ndef standardize(x): \n    return (x-mean_px)/std_px","metadata":{"execution":{"iopub.status.busy":"2023-05-23T07:05:56.367707Z","iopub.execute_input":"2023-05-23T07:05:56.368140Z","iopub.status.idle":"2023-05-23T07:05:56.520775Z","shell.execute_reply.started":"2023-05-23T07:05:56.368103Z","shell.execute_reply":"2023-05-23T07:05:56.519492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    InputLayer(input_shape=(28,28,1)),\n    #Lambda(standardize, input_shape=(28,28,1)),\n    Convolution2D(32,(3,3), activation='relu'),\n    Convolution2D(32,(3,3), activation='relu'),\n    MaxPooling2D(),\n    Convolution2D(64,(3,3), activation='relu'),\n    Convolution2D(64,(3,3), activation='relu'),\n    MaxPooling2D(),\n    Flatten(),\n    Dense(100, activation='relu'),\n    Dense(10, activation='softmax')\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-05-23T07:06:20.101180Z","iopub.execute_input":"2023-05-23T07:06:20.101568Z","iopub.status.idle":"2023-05-23T07:06:20.408929Z","shell.execute_reply.started":"2023-05-23T07:06:20.101539Z","shell.execute_reply":"2023-05-23T07:06:20.407599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-23T07:06:42.366762Z","iopub.execute_input":"2023-05-23T07:06:42.367167Z","iopub.status.idle":"2023-05-23T07:06:42.835326Z","shell.execute_reply.started":"2023-05-23T07:06:42.367136Z","shell.execute_reply":"2023-05-23T07:06:42.834199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-23T07:07:06.647912Z","iopub.execute_input":"2023-05-23T07:07:06.648324Z","iopub.status.idle":"2023-05-23T07:07:07.197718Z","shell.execute_reply.started":"2023-05-23T07:07:06.648292Z","shell.execute_reply":"2023-05-23T07:07:07.196003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-23T07:08:16.472012Z","iopub.execute_input":"2023-05-23T07:08:16.472936Z","iopub.status.idle":"2023-05-23T07:23:09.282045Z","shell.execute_reply.started":"2023-05-23T07:08:16.472900Z","shell.execute_reply":"2023-05-23T07:23:09.281104Z"},"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-23T07:23:25.264667Z","iopub.execute_input":"2023-05-23T07:23:25.265095Z","iopub.status.idle":"2023-05-23T07:23:26.272843Z","shell.execute_reply.started":"2023-05-23T07:23:25.265064Z","shell.execute_reply":"2023-05-23T07:23:26.271737Z"},"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-23T07:23:35.725702Z","iopub.execute_input":"2023-05-23T07:23:35.726090Z","iopub.status.idle":"2023-05-23T07:23:37.952583Z","shell.execute_reply.started":"2023-05-23T07:23:35.726061Z","shell.execute_reply":"2023-05-23T07:23:37.951441Z"},"trusted":true},"execution_count":null,"outputs":[]}]}