{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"isInternetEnabled":true,"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 tensorflow import keras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:05:24.746176Z","iopub.execute_input":"2025-07-03T14:05:24.746461Z","iopub.status.idle":"2025-07-03T14:05:42.790990Z","shell.execute_reply.started":"2025-07-03T14:05:24.746436Z","shell.execute_reply":"2025-07-03T14:05:42.790119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def initialize_parameters(layers_dim):\n    np.random.seed(42)\n    params = {}\n    for i in range(1, len(layers_dim)):\n        params['W' + str(i)] = np.random.randn(layers_dim[i], layers_dim[i - 1]) * np.sqrt(1. / layers_dim[i - 1])\n        params['b' + str(i)] = np.random.randn(layers_dim[i], 1)\n    return params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:05:42.792259Z","iopub.execute_input":"2025-07-03T14:05:42.792837Z","iopub.status.idle":"2025-07-03T14:05:42.798719Z","shell.execute_reply.started":"2025-07-03T14:05:42.792797Z","shell.execute_reply":"2025-07-03T14:05:42.797880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def relu(Z):\n    return np.maximum(0, Z)\n\ndef relu_derivative(Z):\n    return (Z > 0).astype(float)\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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:05:42.799667Z","iopub.execute_input":"2025-07-03T14:05:42.799990Z","iopub.status.idle":"2025-07-03T14:05:42.828671Z","shell.execute_reply.started":"2025-07-03T14:05:42.799963Z","shell.execute_reply":"2025-07-03T14:05:42.827815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def propagate_forward(X, layers_dim, params):\n    cache = {'A0': X}\n    A = X\n\n    for i in range(1, len(layers_dim)):\n        W = params['W' + str(i)]\n        b = params['b' + str(i)]\n        Z = np.dot(W, A) + b\n\n        if i < len(layers_dim) - 1:\n            A = relu(Z)\n        else:\n            A = softmax(Z)\n\n        cache['Z' + str(i)] = Z\n        cache['A' + str(i)] = A\n\n    return A, cache","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:05:42.830328Z","iopub.execute_input":"2025-07-03T14:05:42.830693Z","iopub.status.idle":"2025-07-03T14:05:42.848050Z","shell.execute_reply.started":"2025-07-03T14:05:42.830668Z","shell.execute_reply":"2025-07-03T14:05:42.847062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def compute_loss(AL, Y):\n    m = Y.shape[1]\n    loss = -np.sum(Y * np.log(AL + 1e-8)) / m\n    return loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T16:32:04.669658Z","iopub.execute_input":"2025-07-03T16:32:04.670650Z","iopub.status.idle":"2025-07-03T16:32:04.676738Z","shell.execute_reply.started":"2025-07-03T16:32:04.670615Z","shell.execute_reply":"2025-07-03T16:32:04.675833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def backprop(cache, params, Y, layers_dim, learning_rate=0.01):\n    m = Y.shape[1]\n    L = len(layers_dim) - 1\n\n    dZ = cache['A' + str(L)] - Y \n\n    for l in reversed(range(1, L + 1)):\n        A_prev = cache['A' + str(l - 1)]\n        Z = cache['Z' + str(l)]\n        W = params['W' + str(l)]\n\n        if l < L:\n            dZ = dZ * relu_derivative(Z)\n\n        dW = np.dot(dZ, A_prev.T) / m\n        db = np.sum(dZ, axis=1, keepdims=True) / m\n\n        params['W' + str(l)] -= learning_rate * dW\n        params['b' + str(l)] -= learning_rate * db\n\n        if l > 1:\n            dZ = np.dot(W.T, dZ)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:05:42.866754Z","iopub.execute_input":"2025-07-03T14:05:42.867068Z","iopub.status.idle":"2025-07-03T14:05:42.884505Z","shell.execute_reply.started":"2025-07-03T14:05:42.867047Z","shell.execute_reply":"2025-07-03T14:05:42.883633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_predictions(AL):\n    return np.argmax(AL, axis=0)\n\ndef get_accuracy(predictions, Y):\n    labels = np.argmax(Y, axis=0)\n    return np.mean(predictions == labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:05:42.885381Z","iopub.execute_input":"2025-07-03T14:05:42.885626Z","iopub.status.idle":"2025-07-03T14:05:42.900367Z","shell.execute_reply.started":"2025-07-03T14:05:42.885602Z","shell.execute_reply":"2025-07-03T14:05:42.899582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training Function\ndef train(X, Y, layers_dim, iterations=1000, learning_rate=0.01):\n    params = initialize_parameters(layers_dim)\n\n    for i in range(iterations):\n        AL, cache = propagate_forward(X, layers_dim, params)\n        loss = compute_loss(AL, Y)\n        backprop(cache, params, Y, layers_dim, learning_rate)\n\n        \n        if i % 100 == 0:\n            predictions = get_predictions(AL)\n            acc = get_accuracy(predictions, Y)\n            print(f\"Iteration {i}: Loss = {loss:.4f} | Accuracy = {acc:.4f}\")\n\n\n    return params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:57:41.981365Z","iopub.execute_input":"2025-07-03T15:57:41.982040Z","iopub.status.idle":"2025-07-03T15:57:41.987709Z","shell.execute_reply.started":"2025-07-03T15:57:41.982016Z","shell.execute_reply":"2025-07-03T15:57:41.986787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(X, params, layers_dim):\n    AL, _ = propagate_forward(X, layers_dim, params)\n    return np.argmax(AL, axis=0)  # Class predictions (0–9)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:51:01.639694Z","iopub.execute_input":"2025-07-03T15:51:01.640564Z","iopub.status.idle":"2025-07-03T15:51:01.644938Z","shell.execute_reply.started":"2025-07-03T15:51:01.640536Z","shell.execute_reply":"2025-07-03T15:51:01.644178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()\n\nX_train = X_train / 255.0\nX_test = X_test / 255.0\n\nX_train = X_train.reshape(-1, 784).T  # Shape: (784, num_samples)\nX_test = X_test.reshape(-1, 784).T\n\ny_train_oh = np.eye(10)[y_train].T  # Shape: (10, num_samples)\ny_test_oh = np.eye(10)[y_test].T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T15:51:02.039825Z","iopub.execute_input":"2025-07-03T15:51:02.040158Z","iopub.status.idle":"2025-07-03T15:51:02.590520Z","shell.execute_reply.started":"2025-07-03T15:51:02.040133Z","shell.execute_reply":"2025-07-03T15:51:02.589628Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"layers_dim = [784,64,32,10]\ntrained_params = train(X_train, y_train_oh, layers_dim, iterations=900, learning_rate=0.1)","metadata":{"execution":{"iopub.status.busy":"2025-07-03T15:58:01.181559Z","iopub.execute_input":"2025-07-03T15:58:01.182507Z","iopub.status.idle":"2025-07-03T16:03:36.805108Z","shell.execute_reply.started":"2025-07-03T15:58:01.182468Z","shell.execute_reply":"2025-07-03T16:03:36.804105Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = predict(X_test, trained_params, layers_dim)\ny_true = np.argmax(y_test_oh, axis=0)\n\naccuracy = np.mean(y_pred == y_true) * 100\nprint(f\"Test Accuracy: {accuracy:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T16:03:36.806347Z","iopub.execute_input":"2025-07-03T16:03:36.806687Z","iopub.status.idle":"2025-07-03T16:03:36.856859Z","shell.execute_reply.started":"2025-07-03T16:03:36.806664Z","shell.execute_reply":"2025-07-03T16:03:36.855999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trained_params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-03T14:59:38.635468Z","iopub.execute_input":"2025-07-03T14:59:38.635786Z","iopub.status.idle":"2025-07-03T14:59:38.645959Z","shell.execute_reply.started":"2025-07-03T14:59:38.635740Z","shell.execute_reply":"2025-07-03T14:59:38.645125Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###### ","metadata":{}}]}