{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"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","trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:45.089194Z","iopub.execute_input":"2025-02-04T10:30:45.089616Z","iopub.status.idle":"2025-02-04T10:30:46.336728Z","shell.execute_reply.started":"2025-02-04T10:30:45.089569Z","shell.execute_reply":"2025-02-04T10:30:46.335672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:46.338859Z","iopub.execute_input":"2025-02-04T10:30:46.339422Z","iopub.status.idle":"2025-02-04T10:30:50.116453Z","shell.execute_reply.started":"2025-02-04T10:30:46.339375Z","shell.execute_reply":"2025-02-04T10:30:50.115373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(data)\n\nm, n = data.shape\n\nnp.random.shuffle(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.117565Z","iopub.execute_input":"2025-02-04T10:30:50.117850Z","iopub.status.idle":"2025-02-04T10:30:50.763530Z","shell.execute_reply.started":"2025-02-04T10:30:50.117822Z","shell.execute_reply":"2025-02-04T10:30:50.762685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_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":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.765670Z","iopub.execute_input":"2025-02-04T10:30:50.766040Z","iopub.status.idle":"2025-02-04T10:30:50.904113Z","shell.execute_reply.started":"2025-02-04T10:30:50.766007Z","shell.execute_reply":"2025-02-04T10:30:50.903175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.905040Z","iopub.execute_input":"2025-02-04T10:30:50.905312Z","iopub.status.idle":"2025-02-04T10:30:50.911980Z","shell.execute_reply.started":"2025-02-04T10:30:50.905285Z","shell.execute_reply":"2025-02-04T10:30:50.910947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def ReLU(Z):\n    return np.maximum(0, Z)\n\ndef softmax(Z):\n    expZ = np.exp(Z - np.max(Z, axis=0, keepdims=True))\n    return expZ / np.sum(expZ, axis=0, keepdims=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.913388Z","iopub.execute_input":"2025-02-04T10:30:50.913723Z","iopub.status.idle":"2025-02-04T10:30:50.922647Z","shell.execute_reply.started":"2025-02-04T10:30:50.913692Z","shell.execute_reply":"2025-02-04T10:30:50.921720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def 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    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.923746Z","iopub.execute_input":"2025-02-04T10:30:50.924355Z","iopub.status.idle":"2025-02-04T10:30:50.936168Z","shell.execute_reply.started":"2025-02-04T10:30:50.924320Z","shell.execute_reply":"2025-02-04T10:30:50.935202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_params():\n    W1 = np.random.randn(10, 784)\n    b1 = np.random.randn(10, 1)\n\n    W2 = np.random.randn(10, 10)\n    b2 = np.random.randn(10, 1)\n\n    return W1, b1, W2, b2\n\n\ndef forward_prop(W1, b1, W2, b2, X):\n    Z1 = W1.dot(X) + b1\n    A1 = ReLU(Z1)\n\n    Z2 = W2.dot(A1) + b2\n    A2 = softmax(Z2)\n\n    return Z1, A1, Z2, A2\n\ndef ReLU_deriv(Z):\n    return Z > 0\n\ndef back_prop(Z1, A1, Z2, A2, W1, W2, X, Y):\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.937337Z","iopub.execute_input":"2025-02-04T10:30:50.938303Z","iopub.status.idle":"2025-02-04T10:30:50.950291Z","shell.execute_reply.started":"2025-02-04T10:30:50.938269Z","shell.execute_reply":"2025-02-04T10:30:50.949353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(A):\n    return np.argmax(A, 0)\n\ndef get_accuracy(predictions, Y):\n    return np.mean(predictions == Y)\n\ndef gradient_descent(X, Y, num_iterations, alpha):\n    W1, b1, W2, b2 = init_params()\n    for i in range(num_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\n        if i % 100 == 0:\n            print(\"Iteration: \", i)\n            predictions = predict(A2)\n            print(f\"Accuracy: {get_accuracy(predictions, Y) * 100:.2f}%\")\n\n    return W1, b1, W2, b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.951514Z","iopub.execute_input":"2025-02-04T10:30:50.951851Z","iopub.status.idle":"2025-02-04T10:30:50.964773Z","shell.execute_reply.started":"2025-02-04T10:30:50.951815Z","shell.execute_reply":"2025-02-04T10:30:50.963683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 10000, 0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:30:50.967149Z","iopub.execute_input":"2025-02-04T10:30:50.967697Z","iopub.status.idle":"2025-02-04T10:48:59.724487Z","shell.execute_reply.started":"2025-02-04T10:30:50.967664Z","shell.execute_reply":"2025-02-04T10:48:59.722957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)\n    predictions = predict(A2)\n    return predictions\n\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    \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-02-04T10:48:59.725766Z","iopub.execute_input":"2025-02-04T10:48:59.726194Z","iopub.status.idle":"2025-02-04T10:48:59.739003Z","shell.execute_reply.started":"2025-02-04T10:48:59.726153Z","shell.execute_reply":"2025-02-04T10:48:59.737995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_prediction(0, W1, b1, W2, b2)\ntest_prediction(1, W1, b1, W2, b2)\ntest_prediction(2, W1, b1, W2, b2)\ntest_prediction(3, W1, b1, W2, b2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-04T10:48:59.740067Z","iopub.execute_input":"2025-02-04T10:48:59.740448Z","iopub.status.idle":"2025-02-04T10:49:00.654299Z","shell.execute_reply.started":"2025-02-04T10:48:59.740406Z","shell.execute_reply":"2025-02-04T10:49:00.653010Z"}},"outputs":[],"execution_count":null}]}