{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"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":"2022-04-09T14:56:18.407931Z","iopub.execute_input":"2022-04-09T14:56:18.408242Z","iopub.status.idle":"2022-04-09T14:56:18.423024Z","shell.execute_reply.started":"2022-04-09T14:56:18.408212Z","shell.execute_reply":"2022-04-09T14:56:18.421793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:19.009352Z","iopub.execute_input":"2022-04-09T14:56:19.009619Z","iopub.status.idle":"2022-04-09T14:56:19.014214Z","shell.execute_reply.started":"2022-04-09T14:56:19.009591Z","shell.execute_reply":"2022-04-09T14:56:19.013111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:19.676780Z","iopub.execute_input":"2022-04-09T14:56:19.677087Z","iopub.status.idle":"2022-04-09T14:56:22.850299Z","shell.execute_reply.started":"2022-04-09T14:56:19.677045Z","shell.execute_reply":"2022-04-09T14:56:22.849439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = pd.get_dummies(df.label).values\nX = df.drop('label', axis=1)/255\nX = X.values","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:22.851920Z","iopub.execute_input":"2022-04-09T14:56:22.852209Z","iopub.status.idle":"2022-04-09T14:56:23.186793Z","shell.execute_reply.started":"2022-04-09T14:56:22.852180Z","shell.execute_reply":"2022-04-09T14:56:23.186140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sig(x):\n    return 1/(1 + np.exp(-x))\n\ndef dsig(x):\n    return sig(x) * (1 - sig(x))\n\ndef J(x, y):\n    return np.sum((x - y)**2)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:23.187730Z","iopub.execute_input":"2022-04-09T14:56:23.187962Z","iopub.status.idle":"2022-04-09T14:56:23.193020Z","shell.execute_reply.started":"2022-04-09T14:56:23.187932Z","shell.execute_reply":"2022-04-09T14:56:23.192404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alpha = 0.01\nEs = list()\naccs = list()\n\nw1 = np.random.normal(size = (100, X.shape[1]))\nw2 = np.random.normal(size = (10,100))\n\nfor epoch in range(500):\n    # sample random batch:\n    index = np.random.randint(0, 42000, 128)\n\n    ys = y[index].T\n    xs = X[index].T\n\n    # forward pass:\n    z1 = w1 @ xs\n    a1 = sig(z1)\n    z2 = w2 @ a1\n    pred = sig(z2)\n    \n    \n    \n    # tracking:\n    error = J(pred, ys)\n    Es.append(error)\n    \n    acc = (pred.argmax(axis=0) == ys.argmax(axis=0)).mean()\n    accs.append(acc)\n    \n    # reduce learningrate:\n    if epoch > 250:\n        alpha *= alpha*np.exp(-0.1)\n\n    # calculate gradients:\n    oE = -(ys - pred)\n    w2n = alpha * oE * dsig(z2) @ a1.T\n\n    hE = w2.T @ oE\n    w1n = alpha * hE * dsig(z1) @ xs.T\n    \n    # update weights:\n    w1 = w1 - w1n\n    w2 = w2 - w2n","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:23.274678Z","iopub.execute_input":"2022-04-09T14:56:23.274974Z","iopub.status.idle":"2022-04-09T14:56:25.660492Z","shell.execute_reply.started":"2022-04-09T14:56:23.274939Z","shell.execute_reply":"2022-04-09T14:56:25.659526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize = (18, 5), sharex=True)\nax[0].plot(Es)\nax[0].grid()\nax[0].set_title('Error')\n\nax[1].plot(accs)\nax[1].set_yticks(np.arange(0, 1.2, 0.2))\nax[1].grid()\nax[1].set_title('Accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:25.662440Z","iopub.execute_input":"2022-04-09T14:56:25.662959Z","iopub.status.idle":"2022-04-09T14:56:26.034867Z","shell.execute_reply.started":"2022-04-09T14:56:25.662907Z","shell.execute_reply":"2022-04-09T14:56:26.033957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = np.random.randint(0, X.shape[1])\nsample = X[index]\nz1 = np.dot(w1, sample)\na1 = sig(z1)\nz2 = np.dot(w2, a1)\nprint(\"Prediction:\", sig(z2).argmax())\nprint(\"True:\", y[index].argmax())\nplt.imshow(sample.reshape(28, 28), cmap='gray');","metadata":{"execution":{"iopub.status.busy":"2022-04-09T14:56:26.035971Z","iopub.execute_input":"2022-04-09T14:56:26.036248Z","iopub.status.idle":"2022-04-09T14:56:26.259195Z","shell.execute_reply.started":"2022-04-09T14:56:26.036218Z","shell.execute_reply":"2022-04-09T14:56:26.257088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}