{"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)\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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-26T12:09:03.322364Z","iopub.execute_input":"2023-07-26T12:09:03.322733Z","iopub.status.idle":"2023-07-26T12:09:03.333672Z","shell.execute_reply.started":"2023-07-26T12:09:03.322704Z","shell.execute_reply":"2023-07-26T12:09:03.332397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-26T12:09:03.879431Z","iopub.execute_input":"2023-07-26T12:09:03.880089Z","iopub.status.idle":"2023-07-26T12:09:03.884273Z","shell.execute_reply.started":"2023-07-26T12:09:03.880055Z","shell.execute_reply":"2023-07-26T12:09:03.883312Z"},"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":"2023-07-26T12:09:04.391087Z","iopub.execute_input":"2023-07-26T12:09:04.391494Z","iopub.status.idle":"2023-07-26T12:09:06.759295Z","shell.execute_reply.started":"2023-07-26T12:09:04.391462Z","shell.execute_reply":"2023-07-26T12:09:06.758163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T12:09:06.761546Z","iopub.execute_input":"2023-07-26T12:09:06.762075Z","iopub.status.idle":"2023-07-26T12:09:06.780733Z","shell.execute_reply.started":"2023-07-26T12:09:06.762029Z","shell.execute_reply":"2023-07-26T12:09:06.779399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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_train[0]\nX_train = data_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-26T12:09:06.782278Z","iopub.execute_input":"2023-07-26T12:09:06.782692Z","iopub.status.idle":"2023-07-26T12:09:07.607801Z","shell.execute_reply.started":"2023-07-26T12:09:06.782651Z","shell.execute_reply":"2023-07-26T12:09:07.606695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2023-07-26T12:09:13.577304Z","iopub.execute_input":"2023-07-26T12:09:13.577684Z","iopub.status.idle":"2023-07-26T12:09:13.584718Z","shell.execute_reply.started":"2023-07-26T12:09:13.577653Z","shell.execute_reply":"2023-07-26T12:09:13.583520Z"},"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 deriv_ReLU(Z):\n    return Z > 0\n\ndef back_prop(Z1, A1, Z2, A2, W1, 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) * deriv_ReLU(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-26T12:10:27.520720Z","iopub.execute_input":"2023-07-26T12:10:27.521114Z","iopub.status.idle":"2023-07-26T12:10:27.532606Z","shell.execute_reply.started":"2023-07-26T12:10:27.521078Z","shell.execute_reply":"2023-07-26T12:10:27.531573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-07-26T12:10:28.015500Z","iopub.execute_input":"2023-07-26T12:10:28.015916Z","iopub.status.idle":"2023-07-26T12:10:34.575454Z","shell.execute_reply.started":"2023-07-26T12:10:28.015872Z","shell.execute_reply":"2023-07-26T12:10:34.573885Z"},"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, 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, W1, W2, X, Y)\n        W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n        if i % 50 == 0:\n            print(\"Iteration: \", i)\n            print(\"Accuracy: \", get_accuracy(get_predictions(A2), Y))\n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2023-07-26T12:10:40.783013Z","iopub.execute_input":"2023-07-26T12:10:40.783691Z","iopub.status.idle":"2023-07-26T12:10:40.792061Z","shell.execute_reply.started":"2023-07-26T12:10:40.783646Z","shell.execute_reply":"2023-07-26T12:10:40.790895Z"},"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":"2023-07-26T12:10:43.502922Z","iopub.execute_input":"2023-07-26T12:10:43.503333Z","iopub.status.idle":"2023-07-26T12:11:51.450324Z","shell.execute_reply.started":"2023-07-26T12:10:43.503298Z","shell.execute_reply":"2023-07-26T12:11:51.449114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}