{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:44:51.002328Z","iopub.execute_input":"2024-05-23T11:44:51.002729Z","iopub.status.idle":"2024-05-23T11:44:51.008110Z","shell.execute_reply.started":"2024-05-23T11:44:51.002698Z","shell.execute_reply":"2024-05-23T11:44:51.006969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:23.195543Z","iopub.execute_input":"2024-05-23T11:24:23.196345Z","iopub.status.idle":"2024-05-23T11:24:27.136062Z","shell.execute_reply.started":"2024-05-23T11:24:23.196312Z","shell.execute_reply":"2024-05-23T11:24:27.134822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:38.357384Z","iopub.execute_input":"2024-05-23T11:24:38.357764Z","iopub.status.idle":"2024-05-23T11:24:38.377984Z","shell.execute_reply.started":"2024-05-23T11:24:38.357736Z","shell.execute_reply":"2024-05-23T11:24:38.376727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making the data ready for training","metadata":{}},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data) # shuffle before splitting into dev and training sets\n\ndata_dev = data[0:1000].T\nY_dev = data_dev[0]\nX_dev = data_dev[1:n]\nX_dev = X_dev / 255.\n\ndata_train = data[1000:m].T\nY_train = data_train[0]\nX_train = data_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:42:26.178006Z","iopub.execute_input":"2024-05-23T11:42:26.178459Z","iopub.status.idle":"2024-05-23T11:42:27.000697Z","shell.execute_reply.started":"2024-05-23T11:42:26.178421Z","shell.execute_reply":"2024-05-23T11:42:26.999760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Our NN will have a simple two-layer architecture. \n* Input layer  a[0] will have 784 units corresponding to the 784 pixels in each 28x28 input image. \n* A hidden layer  a[1] will have 10 units with ReLU activation.\n* finally our output layer  a[2] will have 10 units corresponding to the ten digit classes with softmax activation.\n\n","metadata":{}},{"cell_type":"code","source":"def 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":"2024-05-23T11:42:45.021761Z","iopub.execute_input":"2024-05-23T11:42:45.022189Z","iopub.status.idle":"2024-05-23T11:42:45.037679Z","shell.execute_reply.started":"2024-05-23T11:42:45.022152Z","shell.execute_reply":"2024-05-23T11:42:45.036466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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 = frd_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":"2024-05-23T11:42:47.914983Z","iopub.execute_input":"2024-05-23T11:42:47.915959Z","iopub.status.idle":"2024-05-23T11:42:47.925633Z","shell.execute_reply.started":"2024-05-23T11:42:47.915924Z","shell.execute_reply":"2024-05-23T11:42:47.924223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 0.10, 500)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:42:49.498848Z","iopub.execute_input":"2024-05-23T11:42:49.499543Z","iopub.status.idle":"2024-05-23T11:43:50.076503Z","shell.execute_reply.started":"2024-05-23T11:42:49.499509Z","shell.execute_reply":"2024-05-23T11:43:50.074934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)\n    predictions = get_predictions(A2)\n    return predictions\n\ndef test_prediction(index, W1, b1, W2, b2):\n    current_image = X_train[:, index, None]\n    prediction = make_predictions(X_train[:, index, None], W1, b1, W2, b2)\n    label = Y_train[index]\n    print(\"Prediction: \", prediction)\n    print(\"Label: \", label)\n    \n    current_image = current_image.reshape((28, 28)) * 255\n    plt.gray()\n    plt.imshow(current_image, interpolation='nearest')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:44:55.158902Z","iopub.execute_input":"2024-05-23T11:44:55.159919Z","iopub.status.idle":"2024-05-23T11:44:55.168592Z","shell.execute_reply.started":"2024-05-23T11:44:55.159883Z","shell.execute_reply":"2024-05-23T11:44:55.167490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction(0, W1, b1, W2, b2)\ntest_prediction(1, W1, b1, W2, b2)\ntest_prediction(2, W1, b1, W2, b2)\ntest_prediction(3, W1, b1, W2, b2)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:44:57.246621Z","iopub.execute_input":"2024-05-23T11:44:57.247063Z","iopub.status.idle":"2024-05-23T11:44:58.229276Z","shell.execute_reply.started":"2024-05-23T11:44:57.247008Z","shell.execute_reply":"2024-05-23T11:44:58.228205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev_predictions = make_predictions(X_dev, W1, b1, W2, b2)\nget_accuracy(dev_predictions, Y_dev)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:45:11.381157Z","iopub.execute_input":"2024-05-23T11:45:11.381548Z","iopub.status.idle":"2024-05-23T11:45:11.412340Z","shell.execute_reply.started":"2024-05-23T11:45:11.381519Z","shell.execute_reply":"2024-05-23T11:45:11.411001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" 81.9% accuracy, so our model generalized from the training data pretty well.","metadata":{}}]}