{"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_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport scipy\n\n\n# Input data files are available in the read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n%config Completer.use_jedi = False","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-15T11:55:42.714162Z","iopub.execute_input":"2023-10-15T11:55:42.715518Z","iopub.status.idle":"2023-10-15T11:55:43.076235Z","shell.execute_reply.started":"2023-10-15T11:55:42.715450Z","shell.execute_reply":"2023-10-15T11:55:43.075184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%config Completer.use_jedi = False #For Autocomplete\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T11:55:43.077940Z","iopub.execute_input":"2023-10-15T11:55:43.078331Z","iopub.status.idle":"2023-10-15T11:55:43.084849Z","shell.execute_reply.started":"2023-10-15T11:55:43.078305Z","shell.execute_reply":"2023-10-15T11:55:43.083760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainingData = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n# testData = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T11:55:43.086300Z","iopub.execute_input":"2023-10-15T11:55:43.086707Z","iopub.status.idle":"2023-10-15T11:55:46.277633Z","shell.execute_reply.started":"2023-10-15T11:55:43.086671Z","shell.execute_reply":"2023-10-15T11:55:46.276543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrainingData = (np.array(trainingData))\n# testData = (np.array(testData))\n\nnp.random.shuffle(trainingData)\n# np.random.shuffle(testData)\n\ntrainingData = trainingData.T\n# testData = testData.T\n\ntestData = trainingData[: , :1000]\ntrainingData = trainingData[: , 1000:]\n\n\nn, m = trainingData.shape\n\ntrainingTargets = trainingData[0,:]\ntrainingPredictors = trainingData[1:,:]\ntrainingPredictors = trainingPredictors/255\n\ntestTargets = testData[0,:]\ntestPredictors = testData[1:,:]\ntestPredictors = testPredictors/255\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T11:55:46.279250Z","iopub.execute_input":"2023-10-15T11:55:46.280076Z","iopub.status.idle":"2023-10-15T11:55:47.026066Z","shell.execute_reply.started":"2023-10-15T11:55:46.280035Z","shell.execute_reply":"2023-10-15T11:55:47.024962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class NeuralNetwork:\n    def __init__(self, predictors, target, hiddenLayerNodes):\n        self.predictors = predictors\n        self.target = target\n        self.n0, self.m = self.predictors.shape\n        self.n1 = hiddenLayerNodes\n        self.n2 = 10\n        self.W1, self.B1, self.W2, self.B2 = [None] * 4 \n        self.A1, self.Z1, self.A2, self.Z2 = [None] * 4\n    \n    def gradientDescent(self, iterations, alpha):\n        self.initialiseWeightsAndBiases()\n        \n        for iteration in range(iterations):\n            self.updateWeightsAndBiases(alpha)\n            if(iteration%10 == 0):\n#                 print(\"W1 = \",self.W1)\n#                 print(\"W2 = \",self.W2)\n#                 print(\"B1 = \",self.B1)\n#                 print(\"B2 = \",self.B2)\n                \n                print(str(iteration) + \" \" + str(self.getAccuracy()))\n            \n    def initialiseWeightsAndBiases(self):\n        self.W1 = np.random.rand(10, 784)-0.5\n        self.W2 = np.random.rand(10, 10)-0.5\n        self.B1 = np.zeros((10,1))-0.5\n        self.B2 = np.zeros((10,1))-0.5\n    \n    def updateWeightsAndBiases(self, alpha):\n        self.forwardPropagation()\n        Y = self.getOneHot()\n        \n        DZ2 = self.A2 - Y\n        DW2 = (1/self.m)*DZ2.dot(self.A1.T)\n        DB2 = (1/self.m)*np.sum(DZ2,1).reshape(-1, 1)\n        \n        DZ1 = self.W2.T.dot(DZ2)*self.derivativeOfA1()\n        DW1 = (1/self.m)*DZ1.dot(self.predictors.T)\n        DB1 = (1/self.m)*np.sum(DZ1,1).reshape(-1, 1)\n        \n        self.W2 = self.W2 - alpha*DW2\n        self.W1 = self.W1 - alpha*DW1\n        self.B2 = self.B2 - alpha*DB2\n        self.B1 = self.B1 - alpha*DB1\n    \n    def forwardPropagation(self):\n        self.Z1 = self.W1.dot(self.predictors) + self.B1\n        self.A1 = self.ReLU(self.sigmoid(self.Z1))\n        self.Z2 = self.W2.dot(self.A1) + self.B2\n        self.A2 = self.softmax(self.Z2 )\n        \n    def ReLU(self,Z):\n        return np.maximum(Z, 0)\n    \n    def softmax(self,Z):\n        Z = Z- np.max(Z,axis=0)\n        return np.exp(Z)/np.sum(np.exp(Z), axis = 0)\n    \n    def sigmoid(self, x):\n        return 1 / (1 + np.exp(-x))\n    \n    def getOneHot(self):\n        Y = np.zeros((10, self.m))\n        Y[self.target, np.arange(self.m)] = 1\n        return Y\n    \n    def derivativeOfA1(self):\n        return self.sigmoid(self.Z1)>0\n    \n    def getPredictions(self,Y):\n        return np.argmax(Y, 0)\n    \n    def getAccuracy(self):\n        print(\"predictions\" ,self.getPredictions(self.A2))\n        return np.sum(self.getPredictions(self.A2)==self.target)/(self.target.size)\n    \n    def calculateTestAccuracy(self, testPredictors, testTarget):\n        self.predictors = testPredictors\n        self.target = testTarget\n        \n        self.forwardPropagation()\n        \n        print(\"Accuracy: \" ,self.getAccuracy())\n        \n    def predictRandomExample(self):\n        randomIndex = np.random.randint(0, self.predictors.shape[1])\n        example = self.predictors[:, randomIndex].reshape(28, 28)\n        \n        plt.gray()\n        plt.imshow(example)\n        \n        print(\"Target: \", self.target[randomIndex], \" \")\n        print(\"Prediction: \", self.getPredictions( self.A2[:, randomIndex]), \" \")\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T12:11:22.135853Z","iopub.execute_input":"2023-10-15T12:11:22.136202Z","iopub.status.idle":"2023-10-15T12:11:22.154213Z","shell.execute_reply.started":"2023-10-15T12:11:22.136176Z","shell.execute_reply":"2023-10-15T12:11:22.152969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn =  NeuralNetwork(trainingPredictors, trainingTargets, 10 )\nnn.gradientDescent(500, 1.8)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T12:17:25.627675Z","iopub.execute_input":"2023-10-15T12:17:25.628085Z","iopub.status.idle":"2023-10-15T12:18:54.535945Z","shell.execute_reply.started":"2023-10-15T12:17:25.628054Z","shell.execute_reply":"2023-10-15T12:18:54.534463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn.calculateTestAccuracy( testPredictors, testTargets)\nnn.predictRandomExample()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T12:19:06.956238Z","iopub.execute_input":"2023-10-15T12:19:06.956641Z","iopub.status.idle":"2023-10-15T12:19:07.256880Z","shell.execute_reply.started":"2023-10-15T12:19:06.956580Z","shell.execute_reply":"2023-10-15T12:19:07.255755Z"},"trusted":true},"execution_count":null,"outputs":[]}]}