{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\nfrom matplotlib import pyplot as plt\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","metadata":{"_uuid":"a254a722-14bd-4caf-9c1b-6c89dae42ecb","_cell_guid":"e6f87b93-cb5c-487d-871b-4f051df455c3","collapsed":false,"execution":{"iopub.status.busy":"2023-11-23T07:17:59.357278Z","iopub.execute_input":"2023-11-23T07:17:59.35789Z","iopub.status.idle":"2023-11-23T07:17:59.367958Z","shell.execute_reply.started":"2023-11-23T07:17:59.357855Z","shell.execute_reply":"2023-11-23T07:17:59.366543Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndata.head()","metadata":{"_uuid":"5cb47397-070f-408b-a40a-e29d987ba93a","_cell_guid":"f256b9c3-5422-406e-8aad-a94a9821cce8","collapsed":false,"execution":{"iopub.status.busy":"2023-11-23T07:17:59.376637Z","iopub.execute_input":"2023-11-23T07:17:59.377665Z","iopub.status.idle":"2023-11-23T07:18:02.691615Z","shell.execute_reply.started":"2023-11-23T07:17:59.377629Z","shell.execute_reply":"2023-11-23T07:18:02.690492Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=np.array(data)\nnp.random.shuffle(data)\nm ,n=data.shape\n\ndata_dev=data[0:1000].T\nY_dev=data_dev[0]\nX_dev=data_dev[1:n]\n\ndata_train=data[1000:m].T\nY_train=data_train[0]\nX_train=data_train[1:n]","metadata":{"_uuid":"33347ca3-3f7a-4113-aa4b-55ebd6e4cd64","_cell_guid":"f28ea01e-5115-45f4-8e3c-ee2d8b4e835a","collapsed":false,"execution":{"iopub.status.busy":"2023-11-23T07:18:02.693258Z","iopub.execute_input":"2023-11-23T07:18:02.693557Z","iopub.status.idle":"2023-11-23T07:18:03.396236Z","shell.execute_reply.started":"2023-11-23T07:18:02.693529Z","shell.execute_reply":"2023-11-23T07:18:03.394736Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef init_params():\n    W1 = np.random.rand(10, 784)\n    b1 = np.random.rand(10, 1)\n    W2 = np.random.rand(10, 10)\n    b2 = np.random.rand(10, 1)\n    return W1, b1, W2, b2\n\ndef relu(Z):\n    return np.maximum(0, Z)\n\ndef deriv_relu(Z):\n    return (Z > 0).astype(int)\n\ndef softmax(Z):\n    exp_Z = np.exp(Z)\n    return exp_Z / np.sum(exp_Z, axis=0)\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 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, axis=1, keepdims=True)\n    dZ1 = W2.T.dot(dZ2) * deriv_relu(Z1)\n    dW1 = (1/M) * dZ1.dot(X.T)\n    db1 = (1/M) * np.sum(dZ1, axis=1, keepdims=True)\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\n\ndef get_predictions(A2):\n    return np.argmax(A2, axis=0)\n\ndef get_accuracy(predictions, Y):\n    return np.sum(predictions == Y) / Y.size\n\ndef 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            print(\"Accuracy: \", get_accuracy(get_predictions(A2), Y))\n    return W1, b1, W2, b2","metadata":{"_uuid":"771f532a-f6be-4b91-9d67-bbbfd1c2478c","_cell_guid":"9cbf1bdc-19d0-48d9-b24d-312ea5c0cecd","collapsed":false,"execution":{"iopub.status.busy":"2023-11-23T07:18:03.398155Z","iopub.execute_input":"2023-11-23T07:18:03.398657Z","iopub.status.idle":"2023-11-23T07:18:03.422956Z","shell.execute_reply.started":"2023-11-23T07:18:03.398602Z","shell.execute_reply":"2023-11-23T07:18:03.421513Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"76c32d67-2a8c-40fc-90d2-23c0e39f43ee","_cell_guid":"97434173-72d2-484f-b341-35afa98914e3","collapsed":false,"jupyter":{"outputs_hidden":false},"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\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    current_image = current_image.reshape((28, 28)) * 255\n    plt.gray()\n    plt.imshow(current_image, interpolation='nearest')\n    plt.show()","metadata":{"_uuid":"80b37950-ccd1-4a5a-95e5-58b87407c7ee","_cell_guid":"cdef714d-4a1f-4702-9b06-032651d45a70","collapsed":false,"execution":{"iopub.status.busy":"2023-11-23T07:18:03.426557Z","iopub.execute_input":"2023-11-23T07:18:03.427107Z","iopub.status.idle":"2023-11-23T07:18:03.437102Z","shell.execute_reply.started":"2023-11-23T07:18:03.427014Z","shell.execute_reply":"2023-11-23T07:18:03.435656Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction(5,W1,b1,W2,b2)","metadata":{"_uuid":"80c153c8-2218-4c8c-9368-79cb4a928e60","_cell_guid":"53274899-cf57-46f3-926a-8272c978e5d8","collapsed":false,"execution":{"iopub.status.busy":"2023-11-23T07:18:03.439285Z","iopub.execute_input":"2023-11-23T07:18:03.43969Z","iopub.status.idle":"2023-11-23T07:18:03.818374Z","shell.execute_reply.started":"2023-11-23T07:18:03.439658Z","shell.execute_reply":"2023-11-23T07:18:03.817246Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}