{"cells":[{"metadata":{"colab_type":"code","id":"_0HuCHRE7ctb","outputId":"4a44040a-7616-4c5b-8468-b2e55748ef80","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"import tensorflow\ntensorflow.__version__\n\nimport tensorflow.keras as keras\nkeras.__version__","execution_count":null,"outputs":[]},{"metadata":{"id":"D02D3dtFBz4J","colab_type":"code","outputId":"399fd074-e8c1-4423-bc21-7d8a79698a8e","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom keras.layers import *\nfrom keras.models import *\nfrom keras.callbacks import CSVLogger, ModelCheckpoint\nfrom keras import losses\nfrom keras import optimizers\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"id":"ZfdWKv7ECl0e","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\nDig_MNIST = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"-q92nWJ8y6IU","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":773},"outputId":"7eaa58cd-bb67-44f3-d41f-0abb4d7cecb6","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef vizualize_data():\n    _train = train.ix[:,1:].values.astype(float)\n    y_train = train.ix[:,0].values\n\n    X_test = test.values.astype(float)\n\n    fig = plt.figure()\n\n    Nsample = 4\n    labels = list(range(10))\n    for y in labels:\n        d = train.ix[train['label']==y, 1:].sample(n=Nsample).values\n        for idx in range(Nsample):\n            a = fig.add_subplot(Nsample, len(labels), idx * len(labels) + y + 1)\n            plt.imshow(d[idx].reshape((28,28)))\n            plt.axis('off')\n\n    plt.show()\n    \nvizualize_data()","execution_count":null,"outputs":[]},{"metadata":{"id":"S1hJ2zneD83X","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import numpy as np\n\nx = train.iloc[:, 1:].values\n\ny = []\nfor label in train.iloc[:, :1].values:\n  temp = []\n  for i in range(10):\n    if i == label:\n      temp.append(1)\n    else:\n      temp.append(0)\n  y.append(temp)\ny = np.array(y)\nx_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size=10000, random_state=42)\n\nx_input = Input(shape=(784,))\ny_prediction = BatchNormalization()(x_input)\ny_prediction = Dense(20, activation=None)(y_prediction)\ny_prediction = Activation('elu')(y_prediction)\ny_prediction = Dropout(rate=0.3)(y_prediction)\ny_prediction = Dense(20, activation=None)(y_prediction)\ny_prediction = Activation('elu')(y_prediction)\nprediction = Dense(10, activation='softmax')(y_prediction)\n\nmodel = Model(inputs=[x_input], output=[prediction])\nmodel.compile('RMSprop', losses.categorical_crossentropy, metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"gg1kZd4WM7hf","colab_type":"code","outputId":"bb9aa984-5363-4585-874c-0a6d87deb14c","colab":{"base_uri":"https://localhost:8080/","height":734},"trusted":true},"cell_type":"code","source":"model.fit(\n    x_train, y_train,\n    batch_size=16,\n    epochs=20,\n    verbose=1,\n    validation_data=(x_valid, y_valid),\n    callbacks=[\n        CSVLogger('log.csv'),\n        ModelCheckpoint('model.h5', save_best_only=True),\n    ]\n)","execution_count":null,"outputs":[]},{"metadata":{"id":"dpP70UCQsPCb","colab_type":"code","outputId":"42d304bb-526a-4d23-f107-be8db1a096fc","colab":{"base_uri":"https://localhost:8080/","height":204},"trusted":true},"cell_type":"code","source":"x_test=test.drop('id', axis=1).iloc[:,:].values\npred_probas = model.predict(x_test, batch_size=16)\nprediction = pred_probas.argmax(axis=1)\n\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = prediction\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.tail()","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"nn_1_zhidkov.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"TPU"},"nbformat":4,"nbformat_minor":1}