{"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 numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-17T02:11:07.169664Z","iopub.execute_input":"2024-05-17T02:11:07.170116Z","iopub.status.idle":"2024-05-17T02:11:07.175428Z","shell.execute_reply.started":"2024-05-17T02:11:07.170084Z","shell.execute_reply":"2024-05-17T02:11:07.174357Z"},"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-17T02:11:09.224998Z","iopub.execute_input":"2024-05-17T02:11:09.225395Z","iopub.status.idle":"2024-05-17T02:11:12.466646Z","shell.execute_reply.started":"2024-05-17T02:11:09.225365Z","shell.execute_reply":"2024-05-17T02:11:12.465353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-17T02:11:14.869195Z","iopub.execute_input":"2024-05-17T02:11:14.869638Z","iopub.status.idle":"2024-05-17T02:11:14.891003Z","shell.execute_reply.started":"2024-05-17T02:11:14.869606Z","shell.execute_reply":"2024-05-17T02:11:14.889828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data)\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_dev[0]\nX_train = data_dev[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-17T02:11:21.194031Z","iopub.execute_input":"2024-05-17T02:11:21.194452Z","iopub.status.idle":"2024-05-17T02:11:21.941048Z","shell.execute_reply.started":"2024-05-17T02:11:21.194423Z","shell.execute_reply":"2024-05-17T02:11:21.939817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2024-05-17T02:10:56.288831Z","iopub.execute_input":"2024-05-17T02:10:56.289228Z","iopub.status.idle":"2024-05-17T02:10:56.300410Z","shell.execute_reply.started":"2024-05-17T02:10:56.289200Z","shell.execute_reply":"2024-05-17T02:10:56.299160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 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 ReLU_deriv(Z):\n    return Z > 0\n\ndef back_prop(Z1, A1, Z2, A2, W2, X, Y):\n    m = Y.size\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-17T02:12:01.369049Z","iopub.execute_input":"2024-05-17T02:12:01.369482Z","iopub.status.idle":"2024-05-17T02:12:01.384926Z","shell.execute_reply.started":"2024-05-17T02:12:01.369446Z","shell.execute_reply":"2024-05-17T02:12:01.383621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gradient_descent(X, Y, iterations, alpha):\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 = back_prop(Z1, A1, Z2, A2, 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(\"Accuracy: \", get_accuracy(predictions, Y))\n    return W1, b1, W2, b2\n\ndef get_accuracy(predictions, Y):\n    print(predictions, Y)\n    return np.sum(predictions == Y) / Y.size\n\ndef get_predictions(A2):\n    return np.argmax(A2, 0)","metadata":{"execution":{"iopub.status.busy":"2024-05-17T02:12:09.406339Z","iopub.execute_input":"2024-05-17T02:12:09.406784Z","iopub.status.idle":"2024-05-17T02:12:09.417223Z","shell.execute_reply.started":"2024-05-17T02:12:09.406722Z","shell.execute_reply":"2024-05-17T02:12:09.416027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 500, 0.1)","metadata":{"execution":{"iopub.status.busy":"2024-05-17T02:12:35.055334Z","iopub.execute_input":"2024-05-17T02:12:35.055719Z","iopub.status.idle":"2024-05-17T02:12:37.673163Z","shell.execute_reply.started":"2024-05-17T02:12:35.055690Z","shell.execute_reply":"2024-05-17T02:12:37.671564Z"},"trusted":true},"execution_count":null,"outputs":[]}]}