{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30558,"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\n\nnp.set_printoptions(threshold=10, precision=4, suppress=True)\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-27T09:42:46.592275Z","iopub.execute_input":"2023-10-27T09:42:46.593484Z","iopub.status.idle":"2023-10-27T09:42:50.311496Z","shell.execute_reply.started":"2023-10-27T09:42:46.593439Z","shell.execute_reply":"2023-10-27T09:42:50.30988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-27T09:42:50.314283Z","iopub.execute_input":"2023-10-27T09:42:50.314738Z","iopub.status.idle":"2023-10-27T09:42:50.340978Z","shell.execute_reply.started":"2023-10-27T09:42:50.314698Z","shell.execute_reply":"2023-10-27T09:42:50.339484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm,n = data.shape\nnp.random.shuffle(data)\nprint(data, 'hello')\n\ndata_tt = data.T\nprint(data_train)\nY_train = data_tt[0]\nx_train = data_tt[1:n]\nx_train = x_train / 255.\n\n_,m_train = x_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-10-27T09:42:50.342744Z","iopub.execute_input":"2023-10-27T09:42:50.343659Z","iopub.status.idle":"2023-10-27T09:42:51.489041Z","shell.execute_reply.started":"2023-10-27T09:42:50.343594Z","shell.execute_reply":"2023-10-27T09:42:51.487457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Once the data is in the right form we start creating the NN.","metadata":{}},{"cell_type":"code","source":"def base_params():\n    W1 = np.random.rand(10, 784) - 0.5\n    b1 = np.random.rand(10, 1) - 0.5\n    W2 = np.random.rand(10, 10) - 0.5\n    b2 = np.random.rand(10,1) - 0.5\n    return(W1,b1,W2,b2)\n\ndef ReLU(Z):\n    return np.maximum(Z, 0)\n\ndef ReLU_deriv(Z):\n    return Z > 0\n\ndef softmax(Z):\n    A = np.exp(Z) / sum(np.exp(Z))\n    return A\n\ndef forward_propagation(W1,b1,W2,b2,X):\n    Z1 = W1.dot(X) + b1\n    a1 = ReLU(Z1)\n    Z2 = W2.dot(a1) + b2\n    a2 = softmax(Z2)\n    return Z1, a1, Z2, a2\n\ndef OneHotEncoded(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\ndef backward_propagation(Z1,a1,W1, Z2,a2,W2, X, Y):\n    enc_Y = OneHotEncoded(Y)\n    dZ2 = a2 - enc_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, learning_rate):\n    W1 = W1 - learning_rate * dW1\n    b1 = b1 - learning_rate * db1    \n    W2 = W2 - learning_rate * dW2  \n    b2 = b2 - learning_rate * db2    \n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2023-10-27T09:42:51.492727Z","iopub.execute_input":"2023-10-27T09:42:51.493269Z","iopub.status.idle":"2023-10-27T09:42:51.509469Z","shell.execute_reply.started":"2023-10-27T09:42:51.493217Z","shell.execute_reply":"2023-10-27T09:42:51.508107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_predictions(a2):\n    return np.argmax(a2,0)\n\ndef get_accuracy(predictions, Y):\n    print(predictions, Y)\n    return np.sum(predictions == Y)/ Y.size\n\ndef gradient_descent(X, Y, iterations, learning_rate):\n    W1,b1,W2,b2 = base_params()\n    for i in range(iterations):\n        Z1, a1, Z2, a2 = forward_propagation(W1,b1,W2,b2, X)\n        dW1, db1, dW2, db2 = backward_propagation(Z1,a1,W1, Z2,a2,W2, X , Y)\n        W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, learning_rate)\n        if i % 25 == 0:\n            print('Iteration: ', i)\n            predictions = get_predictions(a2)\n            print(get_accuracy(predictions,Y))\n    return W1,b1,W2,b2","metadata":{"execution":{"iopub.status.busy":"2023-10-27T09:42:51.511106Z","iopub.execute_input":"2023-10-27T09:42:51.511487Z","iopub.status.idle":"2023-10-27T09:42:51.533589Z","shell.execute_reply.started":"2023-10-27T09:42:51.511457Z","shell.execute_reply":"2023-10-27T09:42:51.532078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1,b1,W2,b2 = gradient_descent(x_train, Y_train, 500, 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T09:48:59.968702Z","iopub.execute_input":"2023-10-27T09:48:59.969189Z","iopub.status.idle":"2023-10-27T09:50:36.198465Z","shell.execute_reply.started":"2023-10-27T09:48:59.969155Z","shell.execute_reply":"2023-10-27T09:50:36.196738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualizing the results:","metadata":{}},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, a2 = forward_propagation(W1, b1, W2, b2, X)\n    predictions = get_predictions(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":{"execution":{"iopub.status.busy":"2023-10-27T09:47:10.614657Z","iopub.execute_input":"2023-10-27T09:47:10.615138Z","iopub.status.idle":"2023-10-27T09:47:10.625231Z","shell.execute_reply.started":"2023-10-27T09:47:10.615105Z","shell.execute_reply":"2023-10-27T09:47:10.62372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    test_prediction(i*4, W1, b1, W2, b2)\n    test_prediction(i*1, W1, b1, W2, b2)\n    test_prediction(i*2, W1, b1, W2, b2)\n    test_prediction(i*3, W1, b1, W2, b2)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T09:48:26.959704Z","iopub.execute_input":"2023-10-27T09:48:26.960194Z","iopub.status.idle":"2023-10-27T09:48:37.590356Z","shell.execute_reply.started":"2023-10-27T09:48:26.960159Z","shell.execute_reply":"2023-10-27T09:48:37.588859Z"},"trusted":true},"execution_count":null,"outputs":[]}]}