{"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":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:34:13.708451Z","iopub.execute_input":"2024-09-11T20:34:13.708969Z","iopub.status.idle":"2024-09-11T20:34:17.378129Z","shell.execute_reply.started":"2024-09-11T20:34:13.708921Z","shell.execute_reply":"2024-09-11T20:34:17.376639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:34:17.380243Z","iopub.execute_input":"2024-09-11T20:34:17.380634Z","iopub.status.idle":"2024-09-11T20:34:17.402501Z","shell.execute_reply.started":"2024-09-11T20:34:17.380592Z","shell.execute_reply":"2024-09-11T20:34:17.401111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we dont want to work with pandas dataframe, we want to workw with numpy array so that we can manipulate them and do fancy linear algebra","metadata":{}},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape     # m is number of rows and n is columns\nnp.random.shuffle(data)\n\ndata_dev = data[0:1000].T   # we flip it such that each column is an example rather than each row\n#X_dev = data_dev[]\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]     # All our data loaded in\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:34:22.085085Z","iopub.execute_input":"2024-09-11T20:34:22.085548Z","iopub.status.idle":"2024-09-11T20:34:23.883607Z","shell.execute_reply.started":"2024-09-11T20:34:22.085507Z","shell.execute_reply":"2024-09-11T20:34:23.882208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we are going to split our data into dev and training sets,\nThere is always a risk of overfitting, there is a risk that your model is going to figure out exactly how to exactly make the right predictions for your tainig data, but not generalized to the data that you actually want to be generalizing to. \nSo, you set aside a chunk of data that you dont train and that's your dev or your cross validation data nd that way test your hyper parameters on that data you can test your performance on that data\nYou eliminate the risk of overfitting to your actual training data \n\nwe gonna get the dimension of the data \nSo we suffle our data by   \"np.random.shuffle(data)\"","metadata":{}},{"cell_type":"code","source":"Y_train\n# X_train[0].shape   # I dont want this\n\n# X_train[:, 0].shape     # this is our first column and it has 784 pixels in it \nX_train","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:34:28.260122Z","iopub.execute_input":"2024-09-11T20:34:28.260565Z","iopub.status.idle":"2024-09-11T20:34:28.269699Z","shell.execute_reply.started":"2024-09-11T20:34:28.260523Z","shell.execute_reply":"2024-09-11T20:34:28.268208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets work on Neural network\nfirst, we'll initialize all our parameters and we need a starting w1,b1,w2,b2\n\ndef_init doesnot need any arguement ","metadata":{}},{"cell_type":"code","source":"def init_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 softmax(Z):\n    A = np.exp(Z) / sum(np.exp(Z))\n    return A\n\ndef forward_prop(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 ReLU_deriv(Z):\n     return Z > 0\n    \ndef one_hot(Y):\n    one_hot_Y = np.zeros((Y.size, Y.max() + 1))  #create a matrix of zeros\n    one_hot_Y[np.arange(Y.size), Y] = 1    \n    one_hot_Y = one_hot_Y.T         # Transpose of matrix\n    return one_hot_Y\n# def deriv_RelU(Z):\n#     return Z > 0\n\ndef backward_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":{"execution":{"iopub.status.busy":"2024-09-11T20:34:32.449720Z","iopub.execute_input":"2024-09-11T20:34:32.450209Z","iopub.status.idle":"2024-09-11T20:34:32.466557Z","shell.execute_reply.started":"2024-09-11T20:34:32.450162Z","shell.execute_reply":"2024-09-11T20:34:32.465146Z"},"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    \n\ndef gradient_descent(X, Y, alpha, iterations):\n    W1, b1, W2, b2 = init_params()\n    for i in range(iterations):\n        Z1, A1, Z2, A2 = forward_prop(W1, b1, W2, b2, X)\n        dW1, db1, dW2, db2 = backward_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        if i % 10 == 0:\n            print(\"Iterations = \", i)\n            predictions = get_predictions(A2)\n#             print(\"Accuracy = \", get_accuracy(get_predictions(A2), Y))\n            print(\"Accuracy= \",get_accuracy(predictions, Y))\n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:34:34.072735Z","iopub.execute_input":"2024-09-11T20:34:34.073221Z","iopub.status.idle":"2024-09-11T20:34:34.083957Z","shell.execute_reply.started":"2024-09-11T20:34:34.073177Z","shell.execute_reply":"2024-09-11T20:34:34.082789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 0.10, 10000)","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:40:48.086679Z","iopub.execute_input":"2024-09-11T20:40:48.087835Z","iopub.status.idle":"2024-09-11T21:01:06.358546Z","shell.execute_reply.started":"2024-09-11T20:40:48.087779Z","shell.execute_reply":"2024-09-11T21:01:06.356843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(X1,W1,b1,W2,b2):\n    _,_,_,A2 = forward_prop(W1,b1,W2,b2,X1)\n    predictions = get_predictions(A2)\n    return predictions\ndef test_predictions(index, W1, b1, W2, b2):\n    current_image = X_train[:, index, None]\n    predictions = make_predictions(X_train[:, index, None], W1, b1, W2, b2)\n    label = Y_train[index]\n    print(\"predictions: \", predictions)\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":"2024-09-11T20:39:15.043568Z","iopub.execute_input":"2024-09-11T20:39:15.044156Z","iopub.status.idle":"2024-09-11T20:39:15.054433Z","shell.execute_reply.started":"2024-09-11T20:39:15.044104Z","shell.execute_reply":"2024-09-11T20:39:15.053150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions(0, W1, b1, W2, b2)\ntest_predictions(1, W1, b1, W2, b2)\ntest_predictions(2, W1, b1, W2, b2) \ntest_predictions(3, W1, b1, W2, b2)","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:39:18.068914Z","iopub.execute_input":"2024-09-11T20:39:18.069423Z","iopub.status.idle":"2024-09-11T20:39:19.092918Z","shell.execute_reply.started":"2024-09-11T20:39:18.069376Z","shell.execute_reply":"2024-09-11T20:39:19.091288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev_predictions = make_predictions(X_dev, W1, b1, W2, b2)\nget_accuracy(dev_predictions, Y_dev)","metadata":{"execution":{"iopub.status.busy":"2024-09-11T20:40:21.088116Z","iopub.execute_input":"2024-09-11T20:40:21.088594Z","iopub.status.idle":"2024-09-11T20:40:21.126922Z","shell.execute_reply.started":"2024-09-11T20:40:21.088553Z","shell.execute_reply":"2024-09-11T20:40:21.125366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}