{"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)\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"dftrain=pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ndftest=pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ndfdig=pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')\n\nX_train=dftrain.values\nX_test=dftest.values\ny_train=X_train[:,0]\nX_train=X_train[:,1:]\nlabel_test=X_test[:,0]\nX_test=X_test[:,1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_dig=dfdig.values\ny_dig=X_dig[:,0]\nX_dig=X_dig[:,1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\ni=25\nprint(y_train[i])\nplt.imshow(X_train[i,:].reshape(28,28),cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization,GaussianNoise,Activation\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.models import Sequential\nfrom keras.utils import to_categorical\nfrom keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Dig for Validation\n\nX_train = X_train.reshape((-1, 28, 28, 1))\nX_test = X_test.reshape((-1, 28, 28, 1))\nX_dig = X_dig.reshape((-1, 28, 28, 1))\n\ny_train = to_categorical(y_train,10)\n#y_test = to_categorical(y_test, 10)\ny_dig = to_categorical(y_dig, 10)\ny_dig.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n#model.add(GaussianNoise(0.1, input_shape=(28,28,1)))\n#model.add(Conv2D(32, kernel_size=(7,7), padding='same', activation='relu'))\n#model.add(MaxPooling2D((2,2), padding='same'))\n#model.add(Conv2D(32, kernel_size=(7,7), padding='same', activation='relu'))\n#model.add(MaxPooling2D((2,2), padding='same'))\n#model.add(Conv2D(64, kernel_size=(5,5), padding='same', activation='relu'))\n#model.add(MaxPooling2D((2,2), padding='same'))\n#model.add(Conv2D(32, kernel_size=(5,5), padding='same', activation='relu'))\n\n#model.add(MaxPooling2D((2,2), padding='same'))\n\nmodel.add(Conv2D(16, kernel_size=(7,7), padding='same',  input_shape=(28,28,1),activation='relu'))\nmodel.add(MaxPooling2D((2,2), padding='same'))\nmodel.add(Conv2D(16, kernel_size=(7,7), padding='same', activation='relu'))\nmodel.add(MaxPooling2D((2,2), padding='same'))\n\n#model.add(BatchNormalization(momentum=0.15))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(32, activation='relu'))\n#model.add(GaussianNoise(0.1))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation='softmax'))\nmodel.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])#Adam(lr=0.001,beta_1=0.9,beta_2=0.999)\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nes=keras.callbacks.EarlyStopping(monitor='val_loss', verbose=0, mode='auto', restore_best_weights=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x=X_train,y=y_train,batch_size=512,epochs=20,verbose=1,validation_data=(X_dig, y_dig), callbacks=[es])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''i=20\nhist=[]\nfor _ in range(i):\n    model.fit(x=X_train,y=y_train,batch_size=512,epochs=20,verbose=1,validation_data=(X_dig, y_dig), callbacks=[es])\n    loss,acc=model.evaluate(X_dig, y_dig)\n    \n    hist.append(acc)'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred=model.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_pred = np.argmax(y_pred, axis = 1)\nsample_sub=pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = sample_sub['id'].to_list()\nfile = open(\"submission.csv\", \"w\")\nfile.write(\"id,label\\n\")\nfor id_, pred in zip(ids, label_pred):\n    file.write(\"{},{}\\n\".format(id_, pred))\nfile.close()","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":1}