{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"t_label = train['label']\nt_data = train.drop('label',axis = 1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = t_data.iloc[1:2]\nimg = a.values.reshape(28,28)\nplt.imshow(img, cmap = matplotlib.cm.binary,interpolation=\"nearest\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def one_hot(arrays):\n    full = []\n    a = 0\n    for i in arrays:\n        a = a + 1\n        y_t = np.zeros(10,dtype = 'float')\n        for j in range(0,10):\n            if(abs(i - j) < 1e-4):\n                y_t[j] = 1.0\n                \n        full.append(y_t)\n    \n    full = np.asarray(full)\n    return full\n\ny_t = one_hot(t_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def softmax(x):\n    shiftx = x - np.max(x)\n    exps = np.exp(shiftx)\n    return exps / np.sum(exps)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class bp:\n    def __init__(self,i_size,h_size,o_size,lr):\n        self.i_size = i_size\n        self.h_size = h_size\n        self.o_size = o_size\n        self.lr = lr\n        \n        self.w1 = 0.01 * np.random.randn(self.i_size,self.h_size)\n        self.b1 = np.zeros((1,self.h_size))\n        \n        self.w2 = 0.01 * np.random.randn(self.h_size, self.o_size)\n        self.b2 = np.zeros((1, self.o_size))\n        \n    def forword(self,inputs):\n        self.inputs = inputs.reshape(784,1)\n        self.h_layer = np.maximum(0, np.dot(self.inputs.T, self.w1) + self.b1)#relu\n        self.score = np.dot(self.h_layer,self.w2) + self.b2\n        self.probs = softmax(self.score)\n    \n    #cross entry\n    def loss(self,label):\n        label = label.reshape(10,1)\n        i = range(0,self.probs.shape[0])\n        L_i = -np.log(self.probs[i,label.astype(int)[i]])\n        loss = 1 / L_i.shape[0] * np.sum(L_i)\n        return loss\n      \n    def backword(self,label):\n        dscores = self.probs - label.reshape(1,10)\n        \n        dw2 = np.dot(self.h_layer.T, dscores)\n        db2 = np.sum(dscores, axis=0, keepdims=True)\n        \n        dhidden = np.dot(dscores, self.w2.T)\n        dhidden[self.h_layer <= 0] = 0\n        \n        dw1 = np.dot(self.inputs, dhidden)\n        db1 = np.sum(dhidden, axis=0, keepdims=True)\n        \n        self.w1 += -self.lr * dw1\n        self.b1 += -self.lr * db1\n        self.w2 += -self.lr * dw2\n        self.b2 += -self.lr * db2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BP = bp(784,100,10,0.01)\nepoch = 300","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = t_data.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,epoch):\n    shuffle_index = np.random.permutation(60000)\n    x_train,y_train = X[shuffle_index],y_t[shuffle_index]\n    for j in range(0,500):\n        inputs = x_train[j] / 255\n        BP.forword(inputs)\n        BP.backword(y_train[j])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('../input/Kannada-MNIST/test.csv')\nx_test = test.drop('id',axis = 1).values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = []\nfor i in range(0,len(x_test)):\n    BP.forword(x_test[i])\n    result = np.argmax(BP.probs)\n    results.append(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = list(range(1,len(results) + 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub['id'] = ids\nsub['label'] = results","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv('ktzs_nn.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head()","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}