{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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,"isSourceIdPinned":false,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-21T23:58:17.565342Z","iopub.execute_input":"2025-12-21T23:58:17.565629Z","iopub.status.idle":"2025-12-21T23:58:20.889779Z","shell.execute_reply.started":"2025-12-21T23:58:17.565605Z","shell.execute_reply":"2025-12-21T23:58:20.888926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(data)\n\nm, n = data.shape\n\ntrain_data = data[1000:].T\ntest_data = data[:1000].T\n\n\nY_train = train_data[0]\nX_train = train_data[1:n]/255\n\nY_test = test_data[0]\nX_test = test_data[1:n]/255\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T23:58:25.765005Z","iopub.execute_input":"2025-12-21T23:58:25.765351Z","iopub.status.idle":"2025-12-21T23:58:26.091857Z","shell.execute_reply.started":"2025-12-21T23:58:25.765322Z","shell.execute_reply":"2025-12-21T23:58:26.090860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_params():\n    W1 = np.random.rand(32, 784) - 0.5\n    b1 = np.random.rand(32, 1) - 0.5\n    W2 = np.random.rand(32, 32) - 0.5\n    b2 = np.random.rand(32, 1) - 0.5\n    W3 = np.random.rand(10, 32) - 0.5\n    b3 = np.random.rand(10, 1) - 0.5\n    return W1, b1, W2, b2, W3, b3\ndef g(Z):\n    return np.maximum(Z, 0)\ndef softmax(Z):\n    return np.exp(Z)/sum(np.exp(Z))\ndef gp(Z):\n    return Z > 0\ndef one_hot(Y):\n    one_hot = np.zeros((Y.size, 10))\n    one_hot[np.arange(Y.size), Y] = 1\n    return one_hot.T\ndef fprop(W1, b1, W2, b2, W3, b3, X):\n    Z1 = W1.dot(X) + b1\n    A1 = g(Z1)\n    Z2 = W2.dot(A1) + b2\n    A2 = g(Z2)\n    Z3 = W3.dot(A2) + b3\n    A3 = softmax(Z3)\n    return Z1, A1, Z2, A2, Z3, A3\ndef bprop(Z1, A1, Z2, A2, Z3, A3, W1, b1, W2, b2, W3, b3, X, Y):\n    Y = one_hot(Y)\n    dZ3 = A3 - Y\n    dW3 = 1/m * dZ3.dot(A2.T)\n    db3 = 1/m * np.sum(dZ3, axis=1, keepdims=True)\n    dZ2 = W3.T.dot(dZ3) * gp(Z2)\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) * gp(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, dW3, db3\ndef update_params(W1, b1, W2, b2, W3, b3, dW3, db3, dW2, db2, dW1, db1, lr):\n    W3-= lr * dW3\n    b3-= lr * db3\n    W2-= lr * dW2\n    b2-= lr * db2\n    W1-= lr * dW1\n    b1-= lr * db1\n    \n    return W1, b1, W2, b2, W3, b3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T23:58:29.975684Z","iopub.execute_input":"2025-12-21T23:58:29.976026Z","iopub.status.idle":"2025-12-21T23:58:29.986980Z","shell.execute_reply.started":"2025-12-21T23:58:29.976002Z","shell.execute_reply":"2025-12-21T23:58:29.985912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gradient_descent(X, Y, lr, it):\n    W1, b1, W2, b2, W3, b3 = init_params()\n    for i in range(it):\n        Z1, A1, Z2, A2, Z3, A3 = fprop(W1, b1, W2, b2, W3, b3, X)\n        dW1, db1, dW2, db2, dW3, db3 = bprop(Z1, A1, Z2, A2, Z3, A3, W1, b1, W2, b2, W3, b3, X, Y)\n        W1, b1, W2, b2, W3, b3 = update_params(W1, b1, W2, b2, W3, b3, dW3, db3, dW2, db2, dW1, db1, lr)\n        if i % 5 == 0:\n            print(\"Iteration: \", i)\n            predictions = np.argmax(A3, 0)\n            print(np.sum(predictions == Y)/Y.size)\n    return W1, b1, W2, b2, W3, b3\n\ndef gradient_descent_2(W1, b1, W2, b2, W3, b3, X, Y, lr, it):\n    for i in range(it):\n        Z1, A1, Z2, A2, Z3, A3 = fprop(W1, b1, W2, b2, W3, b3, X)\n        dW1, db1, dW2, db2, dW3, db3 = bprop(Z1, A1, Z2, A2, Z3, A3, W1, b1, W2, b2, W3, b3, X, Y)\n        W1, b1, W2, b2, W3, b3 = update_params(W1, b1, W2, b2, W3, b3, dW3, db3, dW2, db2, dW1, db1, lr)\n        if i % 5 == 0:\n            print(\"Iteration: \", i)\n            predictions = np.argmax(A3, 0)\n            print(np.sum(predictions == Y)/Y.size)\n    return W1, b1, W2, b2, W3, b3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T23:58:34.815562Z","iopub.execute_input":"2025-12-21T23:58:34.815891Z","iopub.status.idle":"2025-12-21T23:58:34.825087Z","shell.execute_reply.started":"2025-12-21T23:58:34.815866Z","shell.execute_reply":"2025-12-21T23:58:34.824150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1, b1, W2, b2, W3, b3 = gradient_descent(X_train, Y_train, 0.1, 250)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T23:58:42.083942Z","iopub.execute_input":"2025-12-21T23:58:42.084553Z","iopub.status.idle":"2025-12-21T23:59:21.170793Z","shell.execute_reply.started":"2025-12-21T23:58:42.084523Z","shell.execute_reply":"2025-12-21T23:59:21.169912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.savez(\"mnist_weights.npz\", W1=W1, b1=b1, W2=W2, b2=b2, W3=W3, b3=b3)\nprint(\"Saved -> mnist_weights.npz\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T21:34:07.206860Z","iopub.execute_input":"2025-08-21T21:34:07.207127Z","iopub.status.idle":"2025-08-21T21:34:07.215119Z","shell.execute_reply.started":"2025-08-21T21:34:07.207103Z","shell.execute_reply":"2025-08-21T21:34:07.214139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.load(\"mnist_weights.npz\")\nW1, b1, W2, b2, W3, b3 = data[\"W1\"], data[\"b1\"], data[\"W2\"], data[\"b2\"], data[\"W3\"], data[\"b3\"]\nprint(\"Weights loaded.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T21:34:07.216361Z","iopub.execute_input":"2025-08-21T21:34:07.216678Z","iopub.status.idle":"2025-08-21T21:34:07.243843Z","shell.execute_reply.started":"2025-08-21T21:34:07.216654Z","shell.execute_reply":"2025-08-21T21:34:07.242642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from matplotlib import pyplot as plt \n\ndef make_predictions(X, W1, b1, W2, b2, W3, b3):\n    _, _, _, _, _, A3 = fprop(W1, b1, W2, b2, W3, b3, X)\n    predictions = np.argmax(A3, 0)\n    return predictions\n\ndef test_prediction(index, W1, b1, W2, b2, W3, b3):\n    current_image = X_train[:, index, None]\n    prediction = make_predictions(X_train[:, index, None], W1, b1, W2, b2, W3, b3)\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T23:59:47.847413Z","iopub.execute_input":"2025-12-21T23:59:47.847701Z","iopub.status.idle":"2025-12-21T23:59:47.854204Z","shell.execute_reply.started":"2025-12-21T23:59:47.847678Z","shell.execute_reply":"2025-12-21T23:59:47.853214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\ntest_prediction(random.randint(1, 1000), W1, b1, W2, b2, W3, b3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T00:00:29.585456Z","iopub.execute_input":"2025-12-22T00:00:29.585776Z","iopub.status.idle":"2025-12-22T00:00:29.742502Z","shell.execute_reply.started":"2025-12-22T00:00:29.585723Z","shell.execute_reply":"2025-12-22T00:00:29.741281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dev_predictions = make_predictions(X_test, W1, b1, W2, b2, W3, b3)\nnp.sum(dev_predictions == Y_test)/Y_test.size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-22T00:00:35.085854Z","iopub.execute_input":"2025-12-22T00:00:35.086234Z","iopub.status.idle":"2025-12-22T00:00:35.097991Z","shell.execute_reply.started":"2025-12-22T00:00:35.086204Z","shell.execute_reply":"2025-12-22T00:00:35.096879Z"}},"outputs":[],"execution_count":null}]}