{"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":"markdown","source":"<h1>Building NN from Scratch</h1>\nWe will building NN from scratch and fit it with MNIST database of handwritten digits (the 'hello world' of Neural Network. This is just for my training and my understanding","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"#importing libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:37.206580Z","iopub.execute_input":"2023-07-17T03:09:37.207758Z","iopub.status.idle":"2023-07-17T03:09:37.213516Z","shell.execute_reply.started":"2023-07-17T03:09:37.207717Z","shell.execute_reply":"2023-07-17T03:09:37.212111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load the data\ndf=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:37.215718Z","iopub.execute_input":"2023-07-17T03:09:37.216194Z","iopub.status.idle":"2023-07-17T03:09:40.852738Z","shell.execute_reply.started":"2023-07-17T03:09:37.216159Z","shell.execute_reply":"2023-07-17T03:09:40.851047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#let's look at our data\ndf.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:40.855760Z","iopub.execute_input":"2023-07-17T03:09:40.856298Z","iopub.status.idle":"2023-07-17T03:09:40.883856Z","shell.execute_reply.started":"2023-07-17T03:09:40.856252Z","shell.execute_reply":"2023-07-17T03:09:40.882313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#some preprocessing to prepare our data\ndf = np.array(df)\nm, n = df.shape\nnp.random.shuffle(df) # shuffle before splitting into dev and training sets\n\ndf_dev = df[0:1000].T\nY_dev = df_dev[0]\nX_dev = df_dev[1:n]\nX_dev = X_dev / 255.\n\ndf_train = df[1000:m].T\nY_train = df_train[0]\nX_train = df_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:40.885654Z","iopub.execute_input":"2023-07-17T03:09:40.886075Z","iopub.status.idle":"2023-07-17T03:09:41.866267Z","shell.execute_reply.started":"2023-07-17T03:09:40.886040Z","shell.execute_reply":"2023-07-17T03:09:41.864761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#let's build our neural network\ndef init_params():\n    W1 = np.random.rand(10, 784) - 0.5\n    b1 = np.random.rand(10, 1) - 0.5\n    W2 = np.random.rand(10, 10) - 0.5\n    b2 = np.random.rand(10, 1) - 0.5\n    return W1, b1, W2, b2\n\ndef ReLU(Z):\n    return np.maximum(Z, 0)\n\ndef softmax(Z):\n    A = np.exp(Z) / sum(np.exp(Z))\n    return A\n    \ndef forward_prop(W1, b1, W2, b2, X):\n    Z1 = W1.dot(X) + b1\n    A1 = ReLU(Z1)\n    Z2 = W2.dot(A1) + b2\n    A2 = softmax(Z2)\n    return Z1, A1, Z2, A2\n\ndef ReLU_deriv(Z):\n    return Z > 0\n\ndef one_hot(Y):\n    one_hot_Y = np.zeros((Y.size, Y.max() + 1))\n    one_hot_Y[np.arange(Y.size), Y] = 1\n    one_hot_Y = one_hot_Y.T\n    return one_hot_Y\n\ndef backward_prop(Z1, A1, Z2, A2, W1, W2, X, Y):\n    one_hot_Y = one_hot(Y)\n    dZ2 = A2 - one_hot_Y\n    dW2 = 1 / m * dZ2.dot(A1.T)\n    db2 = 1 / m * np.sum(dZ2)\n    dZ1 = W2.T.dot(dZ2) * ReLU_deriv(Z1)\n    dW1 = 1 / m * dZ1.dot(X.T)\n    db1 = 1 / m * np.sum(dZ1)\n    return dW1, db1, dW2, db2\n\ndef update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):\n    W1 = W1 - alpha * dW1\n    b1 = b1 - alpha * db1    \n    W2 = W2 - alpha * dW2  \n    b2 = b2 - alpha * db2    \n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:41.869125Z","iopub.execute_input":"2023-07-17T03:09:41.869505Z","iopub.status.idle":"2023-07-17T03:09:41.885501Z","shell.execute_reply.started":"2023-07-17T03:09:41.869476Z","shell.execute_reply":"2023-07-17T03:09:41.884074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#build our predictions and training functions\ndef get_predictions(A2):\n    return np.argmax(A2, 0)\n\ndef get_accuracy(predictions, Y):\n    print(predictions, Y)\n    return np.sum(predictions == Y) / Y.size\n\ndef gradient_descent(X, Y, alpha, iterations):\n    W1, b1, W2, b2 = init_params()\n    for i in range(iterations):\n        Z1, A1, Z2, A2 = forward_prop(W1, b1, W2, b2, X)\n        dW1, db1, dW2, db2 = backward_prop(Z1, A1, Z2, A2, W1, W2, X, Y)\n        W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n        if i % 10 == 0:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(A2)\n            print(get_accuracy(predictions, Y))\n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:41.887317Z","iopub.execute_input":"2023-07-17T03:09:41.887695Z","iopub.status.idle":"2023-07-17T03:09:41.909730Z","shell.execute_reply.started":"2023-07-17T03:09:41.887667Z","shell.execute_reply":"2023-07-17T03:09:41.908367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1,b1,W2,b2=gradient_descent(X_train,Y_train,0.1,500)","metadata":{"execution":{"iopub.status.busy":"2023-07-17T03:09:41.911386Z","iopub.execute_input":"2023-07-17T03:09:41.911740Z","iopub.status.idle":"2023-07-17T03:11:09.342269Z","shell.execute_reply.started":"2023-07-17T03:09:41.911700Z","shell.execute_reply":"2023-07-17T03:11:09.340665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}