{"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":"# 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","trusted":true,"execution":{"iopub.status.busy":"2025-07-16T06:32:34.501406Z","iopub.execute_input":"2025-07-16T06:32:34.501683Z","iopub.status.idle":"2025-07-16T06:32:36.689926Z","shell.execute_reply.started":"2025-07-16T06:32:34.501655Z","shell.execute_reply":"2025-07-16T06:32:36.688789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n#np.set_printoptions(threshold=np.inf)\nimport pandas as pd\nimport time\nimport matplotlib.pyplot\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T06:32:36.690745Z","iopub.execute_input":"2025-07-16T06:32:36.691075Z","iopub.status.idle":"2025-07-16T06:32:36.696492Z","shell.execute_reply.started":"2025-07-16T06:32:36.691043Z","shell.execute_reply":"2025-07-16T06:32:36.695610Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Loading the dataset\n#data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n#data.head()\n\n# load MNIST dataset\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(len(train))\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(len(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T06:33:11.972411Z","iopub.execute_input":"2025-07-16T06:33:11.973417Z","iopub.status.idle":"2025-07-16T06:33:17.244280Z","shell.execute_reply.started":"2025-07-16T06:33:11.973387Z","shell.execute_reply":"2025-07-16T06:33:17.243122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T07:20:08.078304Z","iopub.execute_input":"2025-07-16T07:20:08.078635Z","iopub.status.idle":"2025-07-16T07:20:08.092397Z","shell.execute_reply.started":"2025-07-16T07:20:08.078612Z","shell.execute_reply":"2025-07-16T07:20:08.091485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Code to visualize the datapoints\nimage_array = np.asfarray(train.iloc[100,1:]).reshape((28,28))\nmatplotlib.pyplot.imshow(image_array, cmap='Greys', interpolation='None')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T07:16:59.986318Z","iopub.execute_input":"2025-07-16T07:16:59.986681Z","iopub.status.idle":"2025-07-16T07:17:00.376847Z","shell.execute_reply.started":"2025-07-16T07:16:59.986655Z","shell.execute_reply":"2025-07-16T07:17:00.376073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Scaling the Train & Test  dataset\nscaled_input_train = (np.asfarray(train.iloc[:,1:]) / 255.0 * 0.99) + 0.01\nprint(scaled_input_train.shape)\nscaled_input_test = (np.asfarray(test.iloc[:,1:]) / 255.0 * 0.99) + 0.01\nprint(scaled_input_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T07:17:04.247257Z","iopub.execute_input":"2025-07-16T07:17:04.247616Z","iopub.status.idle":"2025-07-16T07:17:04.619614Z","shell.execute_reply.started":"2025-07-16T07:17:04.247589Z","shell.execute_reply":"2025-07-16T07:17:04.618731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" # create the target output values (all 0.01, except the desired label which is 0.99) - Train & Test dataset\ntargets_train = np.zeros((len(train),10)) + 0.01\n                         \n# all_values[0] is the target label for this record\ntargets_train[np.arange(len(train)),train.iloc[:,0] ] = 0.99\nprint(targets_train.shape)\n\ntargets_test = np.zeros((len(test),10)) + 0.01\n                         \n# all_values[0] is the target label for this record\ntargets_test[np.arange(len(test)),test.iloc[:,0] ] = 0.99\nprint(targets_test.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T07:17:19.398082Z","iopub.execute_input":"2025-07-16T07:17:19.398779Z","iopub.status.idle":"2025-07-16T07:17:19.407247Z","shell.execute_reply.started":"2025-07-16T07:17:19.398753Z","shell.execute_reply":"2025-07-16T07:17:19.406438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Code using Sigmoid function\n\n# --- Network Configuration ---\ninput_layer = 784\nhidden_1 = 128\nhidden_2 = 64\noutput_layer = 10\n\n# --- Initialize Weights (scaled small) ---\nW1 = np.random.randn(hidden_1, input_layer) *  np.sqrt(1. / hidden_1)\nb1 = np.zeros((1, hidden_1))\nW2 = np.random.randn(hidden_2, hidden_1) *  np.sqrt(1. / hidden_2)\nb2 = np.zeros((1, hidden_2))\nW3 = np.random.randn(output_layer, hidden_2) *  np.sqrt(1. / output_layer)\nb3 = np.zeros((1, output_layer))\n\n# --- Activation Functions ---\ndef sigmoid(x, derivative=False):\n    sig = 1 / (1 + np.exp(-x))\n    if derivative:\n        return sig * (1 - sig)\n    return sig\n\ndef softmax(x):\n    exps = np.exp(x - np.max(x, axis=-1, keepdims=True))\n    return exps / np.sum(exps, axis=-1, keepdims=True)\n\n# --- Hyperparameters ---\nlearning_rate = 0.01\nepochs = 100\n\nfor epoch in range(epochs):\n    # --- Forward Propagation (Train Set) ---\n    A0 = scaled_input_train  # (n_samples, 784)\n\n    Z1 = np.dot(A0, W1.T) + b1  # (n_samples, 128)\n    A1 = sigmoid(Z1)\n\n    Z2 = np.dot(A1, W2.T) + b2  # (n_samples, 64)\n    A2 = sigmoid(Z2)\n\n    Z3 = np.dot(A2, W3.T) + b3  # (n_samples, 10)\n    A3 = softmax(Z3)\n\n    # --- Training Accuracy ---\n    preds = np.argmax(A3, axis=1)\n    labels = np.argmax(targets_train, axis=1)\n    train_accuracy = np.mean(preds == labels)\n\n    if epoch % 10 == 0:\n        print(f\"Epoch {epoch}, Train Accuracy: {train_accuracy:.4f}\")\n\n    # --- Backward Propagation ---\n    error3 = (A3 - targets_train) / A3.shape[0]\n    dW3 = np.dot(error3.T, A2)\n    db3 = np.sum(error3, axis=0, keepdims=True)\n\n    error2 = np.dot(error3, W3) * sigmoid(Z2, derivative=True)\n    dW2 = np.dot(error2.T, A1)\n    db2 = np.sum(error2, axis=0, keepdims=True)\n\n    error1 = np.dot(error2, W2) * sigmoid(Z1, derivative=True)\n    dW1 = np.dot(error1.T, A0)\n    db1 = np.sum(error1, axis=0, keepdims=True)\n\n    # --- Update Parameters ---\n    W1 -= learning_rate * dW1\n    b1 -= learning_rate * db1\n\n    W2 -= learning_rate * dW2\n    b2 -= learning_rate * db2\n\n    W3 -= learning_rate * dW3\n    b3 -= learning_rate * db3\n\nprint(f\"Epoch {epoch}, Train Accuracy: {train_accuracy:.4f}\")\n\n# --- Evaluate on Test Set ---\nA0_test = scaled_input_test  # (n_test_samples, 784)\nZ1_test = np.dot(A0_test, W1.T) + b1\nA1_test = sigmoid(Z1_test)\nZ2_test = np.dot(A1_test, W2.T) + b2\nA2_test = sigmoid(Z2_test)\nZ3_test = np.dot(A2_test, W3.T) + b3\nA3_test = softmax(Z3_test)\n\npreds_test = np.argmax(A3_test, axis=1)\nlabels_test = np.argmax(targets_test, axis=1)\ntest_accuracy = np.mean(preds_test == labels_test)\n\nprint(f\"\\nFinal Test Set Accuracy: {test_accuracy:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T07:17:43.865033Z","iopub.execute_input":"2025-07-16T07:17:43.865309Z","iopub.status.idle":"2025-07-16T07:18:37.483449Z","shell.execute_reply.started":"2025-07-16T07:17:43.865289Z","shell.execute_reply":"2025-07-16T07:18:37.482233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Code using Relu function\n\n# --- Network Configuration ---\ninput_layer = 784\nhidden_1 = 128\nhidden_2 = 64\noutput_layer = 10\n\n# --- Initialize Weights (scaled small) ---\nW1 = np.random.randn(hidden_1, input_layer) *  np.sqrt(1. / hidden_1)\nb1 = np.zeros((1, hidden_1))\nW2 = np.random.randn(hidden_2, hidden_1) *  np.sqrt(1. / hidden_2)\nb2 = np.zeros((1, hidden_2))\nW3 = np.random.randn(output_layer, hidden_2) *  np.sqrt(1. / output_layer)\nb3 = np.zeros((1, output_layer))\n\n# --- Activation Functions ---\ndef relu(x, derivative=False):\n    if derivative:\n        return (x > 0).astype(float)\n    return np.maximum(0, x)\n\ndef softmax(x):\n    exps = np.exp(x - np.max(x, axis=-1, keepdims=True))\n    return exps / np.sum(exps, axis=-1, keepdims=True)\n\n# --- Hyperparameters ---\nlearning_rate = 0.01\nepochs = 100\n\nfor epoch in range(epochs):\n    # --- Forward Propagation (Train Set) ---\n    A0 = scaled_input_train  # (n_samples, 784)\n\n    Z1 = np.dot(A0, W1.T) + b1  # (n_samples, 128)\n    A1 = relu(Z1)\n\n    Z2 = np.dot(A1, W2.T) + b2  # (n_samples, 64)\n    A2 = relu(Z2)\n\n    Z3 = np.dot(A2, W3.T) + b3  # (n_samples, 10)\n    A3 = softmax(Z3)\n\n    # --- Training Accuracy ---\n    preds = np.argmax(A3, axis=1)\n    labels = np.argmax(targets_train, axis=1)\n    train_accuracy = np.mean(preds == labels)\n\n    if epoch % 10 == 0:\n        print(f\"Epoch {epoch}, Train Accuracy: {train_accuracy:.4f}\")\n\n    # --- Backward Propagation ---\n    error3 = (A3 - targets_train) / A3.shape[0]\n    dW3 = np.dot(error3.T, A2)\n    db3 = np.sum(error3, axis=0, keepdims=True)\n\n    error2 = np.dot(error3, W3) * relu(Z2, derivative=True)\n    dW2 = np.dot(error2.T, A1)\n    db2 = np.sum(error2, axis=0, keepdims=True)\n\n    error1 = np.dot(error2, W2) * relu(Z1, derivative=True)\n    dW1 = np.dot(error1.T, A0)\n    db1 = np.sum(error1, axis=0, keepdims=True)\n\n    # --- Update Parameters ---\n    W1 -= learning_rate * dW1\n    b1 -= learning_rate * db1\n\n    W2 -= learning_rate * dW2\n    b2 -= learning_rate * db2\n\n    W3 -= learning_rate * dW3\n    b3 -= learning_rate * db3\n\nprint(f\"Epoch {epoch}, Train Accuracy: {train_accuracy:.4f}\")\n\n# --- Evaluate on Test Set ---\nA0_test = scaled_input_test  # (n_test_samples, 784)\nZ1_test = np.dot(A0_test, W1.T) + b1\nA1_test = relu(Z1_test)\nZ2_test = np.dot(A1_test, W2.T) + b2\nA2_test = relu(Z2_test)\nZ3_test = np.dot(A2_test, W3.T) + b3\nA3_test = softmax(Z3_test)\n\npreds_test = np.argmax(A3_test, axis=1)\nlabels_test = np.argmax(targets_test, axis=1)\ntest_accuracy = np.mean(preds_test == labels_test)\n\nprint(f\"\\nFinal Test Set Accuracy: {test_accuracy:.4f}\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}