{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"validation = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')\nvalidation.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validation.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# the data seems sequential, lets do some shuffling\nnp.random.shuffle(train.values)\nnp.random.shuffle(validation.values)\n\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# spilt into x_train and y_train\nx_train = train.iloc[:,1:].values\ny_train = train.iloc[:,0].values\n\nx_val = validation.iloc[:,1:].values\ny_val = validation.iloc[:,0].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# build some neural network on keras :)\n# do we need convolution???\n# idk man, lets try some dense layers and check the score first\n\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom shutil import copyfile\n\n# class myCallback(tf.keras.callbacks.Callback):\n#       def on_epoch_end(self, epoch, logs={}):\n#         if(logs.get('acc')>0.90):\n#             print(\"\\nReached 90% accuracy so cancelling training!\")\n#             self.model.stop_training = True\n# callbacks = myCallback()\n\nmodel = tf.keras.models.Sequential([\n                        tf.keras.layers.Flatten(input_shape=(784,)), \n                        tf.keras.layers.Dense(256, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(128, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(64, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(32, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n                        ])\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nhistory = model.fit(x_train, y_train, epochs =16, validation_data=(x_val, y_val)\n                    #, callbacks=[callbacks]\n                   )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Models tried\n\n### Simple model\nmodel = tf.keras.models.Sequential([\n                        tf.keras.layers.Flatten(input_shape=(784,)), \n                        tf.keras.layers.Dense(128, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n                        ])\n                        \nval_accuracy 0.6455\n\n### Slightly complex model\nmodel = tf.keras.models.Sequential([\n                        tf.keras.layers.Flatten(input_shape=(784,)), \n                        tf.keras.layers.Dense(128, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(64, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(32, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n                        ])\n                        \nval_accuracy 0.6590\n\n### More more complex model\nmodel = tf.keras.models.Sequential([\n                        tf.keras.layers.Flatten(input_shape=(784,)), \n                        tf.keras.layers.Dense(256, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(128, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(64, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(32, activation=tf.nn.relu), \n                        tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n                        ])\n                        \nval_accuracy 0.6808****"},{"metadata":{"trusted":true},"cell_type":"code","source":"%matplotlib inline\n\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\n\n\nacc=history.history['accuracy']\nval_acc=history.history['val_accuracy']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\n\nepochs=range(len(acc)) \nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.figure()\n\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\nplt.figure()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# score looks pretty well, but that's probably over-fitting a bit too much\n# shall cross check validation data next time\n# time to do some prediction!\ntest_predict = model.predict(test.iloc[:,1:].values)\n\nprediction = test.iloc[:,:1]\nprediction['label'] = np.argmax(test_predict, axis=1)\nprediction.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction.to_csv('submission.csv', index=False)","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":1}