{"cells":[{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import np_utils\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten\nfrom keras.optimizers import Adam\n\n# Input data files are available in the \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nsubm = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train , X_test , y_train , y_test = train_test_split(train.iloc[: , 1:].values , train.iloc[:,0].values , test_size = 0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = X_train.reshape(-1, 1, 28, 28)/255.\nX_test = X_test.reshape(-1, 1, 28, 28)/255.\ny_train = np_utils.to_categorical(y_train, num_classes=10)\ny_test = np_utils.to_categorical(y_test, num_classes=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Another way to build your CNN\nmodel = Sequential()\n\n# Conv layer 1 output shape (32, 28, 28)\nmodel.add(Convolution2D(\n    batch_input_shape=(None, 1, 28, 28),\n    filters=32,\n    kernel_size=5,\n    strides=1,\n    padding='same',     # Padding method\n    data_format='channels_first',\n))\nmodel.add(Activation('relu'))\n\n# Pooling layer 1 (max pooling) output shape (32, 14, 14)\nmodel.add(MaxPooling2D(\n    pool_size=2,\n    strides=2,\n    padding='same',    # Padding method\n    data_format='channels_first',\n))\n\n# Conv layer 2 output shape (64, 14, 14)\nmodel.add(Convolution2D(64, 5, strides=1, padding='same', data_format='channels_first'))\nmodel.add(Activation('relu'))\n\n# Pooling layer 2 (max pooling) output shape (64, 7, 7)\nmodel.add(MaxPooling2D(2, 2, 'same', data_format='channels_first'))\n\n# Fully connected layer 1 input shape (64 * 7 * 7) = (3136), output shape (1024)\nmodel.add(Flatten())\nmodel.add(Dense(1024))\nmodel.add(Activation('relu'))\n\n# Fully connected layer 2 to shape (10) for 10 classes\nmodel.add(Dense(10))\nmodel.add(Activation('softmax'))\n\n# Another way to define your optimizer\nadam = Adam(lr=1e-4)\n\n# We add metrics to get more results you want to see\nmodel.compile(optimizer=adam,\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Training ------------')\n# Another way to train the model\nmodel.fit(X_train, y_train, epochs=1, batch_size=64,)\n\nprint('\\nTesting ------------')\n# Evaluate the model with the metrics we defined earlier\nloss, accuracy = model.evaluate(X_test, y_test)\n\nprint('\\ntest loss: ', loss)\nprint('\\ntest accuracy: ', accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = test.iloc[:,1:].values \ntest = test.reshape(-1 , 1 , 28 , 28)/255\npre = model.predict(test, batch_size=64)\npre = np.argmax(pre , axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"subm['label'] = pre\nsubm.to_csv('submit.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}