{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\nfrom skimage.util import random_noise\n\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-05T15:45:12.680071Z","iopub.execute_input":"2023-01-05T15:45:12.680588Z","iopub.status.idle":"2023-01-05T15:45:14.253489Z","shell.execute_reply.started":"2023-01-05T15:45:12.680544Z","shell.execute_reply":"2023-01-05T15:45:14.252555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data) # shuffle before splitting into dev and training sets\n\ndata_dev = data[0:1000].T\ntestlabel = data_dev[0]\ntest = data_dev[1:n]\ntest = test / 255.\n\ndata_train = data[1000:m].T\ntrainlabel = data_train[0]\ntrain = data_train[1:n]\ntrain = train / 255.\n\ntrainnoise = random_noise(train)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:45:14.255341Z","iopub.execute_input":"2023-01-05T15:45:14.255907Z","iopub.status.idle":"2023-01-05T15:45:15.424422Z","shell.execute_reply.started":"2023-01-05T15:45:14.255873Z","shell.execute_reply":"2023-01-05T15:45:15.422726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_params(): #gives arrays of the sizes specified, randomly between 0 and 1\n    w1 = np.random.rand(16, 784) - 0.5\n    b1 = np.random.rand(16, 1) - 0.5\n    w2 = np.random.rand(16, 16) - 0.5 \n    b2 = np.random.rand(16, 1) - 0.5\n    w3 = np.random.rand(10, 16) - 0.5\n    b3 = np.random.rand(10, 1) - 0.5\n    return w1, b1, w2, b2, w3, b3\n\ndef sigmoidactivation(a):\n    A = np.exp(a) / sum(np.exp(a))\n    return A\n    \ndef ReLU(a):\n    return np.maximum(a, 0)\n\ndef forward(w1,b1,w2,b2,w3,b3,X):\n    z1 = w1.dot(X)+b1\n    a1 = ReLU(z1)\n    z2 = w2.dot(a1)+b2\n    a2 = ReLU(z2)\n    z3 = w3.dot(a2)+b3\n    a3 = sigmoidactivation(z3)\n    return z1,z2,z3,a1,a2,a3","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:45:15.425520Z","iopub.execute_input":"2023-01-05T15:45:15.425805Z","iopub.status.idle":"2023-01-05T15:45:15.435586Z","shell.execute_reply.started":"2023-01-05T15:45:15.425780Z","shell.execute_reply":"2023-01-05T15:45:15.434136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encode(Y):\n    one_hot_Y = np.zeros((Y.size, Y.max() + 1))\n    one_hot_Y[np.arange(Y.size), Y] = 1\n    encode = one_hot_Y.T\n    return encode\n\ndef ReLUdev(a):\n    return a > 0\n\ndef backwards(w1,w2,w3,z1,z2,z3,a1,a2,a3,X,Y):\n    dz3 = a3-encode(Y)\n    db3 = (1 / m) * np.sum(dz3)\n    dw3 = (1 / m) * dz3.dot((a2.T))\n    dz2 = w3.T.dot(dz3) * ReLUdev(z2)\n    db2 = (1 / m) * np.sum(dz2)\n    dw2 = (1 / m) * dz2.dot((a1.T))\n    dz1 = w2.T.dot(dz2) * ReLUdev(z1)\n    dw1 = 1 / m * dz1.dot(X.T)\n    db1 = 1 / m * np.sum(dz1)\n    return dz1,dz2,dz3,db1,db2,db3,dw1,dw2,dw3\n","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:45:15.438476Z","iopub.execute_input":"2023-01-05T15:45:15.440385Z","iopub.status.idle":"2023-01-05T15:45:15.452572Z","shell.execute_reply.started":"2023-01-05T15:45:15.440266Z","shell.execute_reply":"2023-01-05T15:45:15.450329Z"},"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 update_params(w1, b1, w2, b2, w3, b3, dw1, db1, dw2, db2, dw3, db3, alpha):\n    w1 = w1 - alpha * dw1\n    b1 = b1 - alpha * db1    \n    w2 = w2 - alpha * dw2  \n    b2 = b2 - alpha * db2   \n    w3 = w3 - alpha * dw3\n    b3 = b3 - alpha * db3\n    return w1, b1, w2, b2, w3, b3\n\ndef gradient_descent(X, Y, alpha, iterations):\n    w1, b1, w2, b2, w3, b3 = init_params()\n    for i in range(iterations):\n        z1,z2,z3,a1,a2,a3 = forward(w1,b1,w2,b2,w3,b3,X)\n        dz1,dz2,dz3,db1,db2,db3,dw1,dw2,dw3 = backwards(w1,w2,w3,z1,z2,z3,a1,a2,a3,X,Y)\n        w1, b1, w2, b2, w3, b3 = update_params(w1, b1, w2, b2, w3, b3, dw1, db1, dw2, db2, dw3,db3, alpha)\n        if i % 10 == 0:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(a3)\n            print(get_accuracy(predictions, Y))\n    return w1, b1, w2, b2, w3, b3\n\ndef gradient_descent_no_initiate(w1, b1, w2, b2, w3, b3,X, Y, alpha, iterations):\n    for i in range(iterations):\n        z1,z2,z3,a1,a2,a3 = forward(w1,b1,w2,b2,w3,b3,X)\n        dz1,dz2,dz3,db1,db2,db3,dw1,dw2,dw3 = backwards(w1,w2,w3,z1,z2,z3,a1,a2,a3,X,Y)\n        w1, b1, w2, b2, w3, b3 = update_params(w1, b1, w2, b2, w3, b3, dw1, db1, dw2, db2, dw3,db3, alpha)\n        if i % 10 == 0:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(a3)\n            print(get_accuracy(predictions, Y))\n    return w1, b1, w2, b2, w3, b3","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:45:15.454801Z","iopub.execute_input":"2023-01-05T15:45:15.455319Z","iopub.status.idle":"2023-01-05T15:45:15.472699Z","shell.execute_reply.started":"2023-01-05T15:45:15.455287Z","shell.execute_reply":"2023-01-05T15:45:15.470986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w1, b1, w2, b2, w3, b3 = gradient_descent(train, trainlabel, 0.10, 500)\nw1, b1, w2, b2, w3, b3 = gradient_descent_no_initiate(w1, b1, w2, b2, w3, b3, trainnoise, trainlabel, 0.10, 500)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:45:15.474303Z","iopub.execute_input":"2023-01-05T15:45:15.474682Z","iopub.status.idle":"2023-01-05T15:46:52.574321Z","shell.execute_reply.started":"2023-01-05T15:45:15.474652Z","shell.execute_reply":"2023-01-05T15:46:52.573477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2, W3, b3):\n    _, _,_, _, _, A2 = forward(W1, b1, W2, b2, W3,b3,X)\n    predictions = get_predictions(A2)\n    return predictions\n\ndef test_prediction(index, W1, b1, W2, b2, W3, b3):\n    current_image = train[:, index, None]\n    prediction = make_predictions(train[:, index, None], W1, b1, W2, b2,W3,b3)\n    label = trainlabel[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()\n\ndef show_error(n,W1, b1, W2, b2, W3, b3):\n    for i in range (1,n):\n        current_image = train[:, i, None]\n        prediction = make_predictions(train[:, i, None], W1, b1, W2, b2, W3, b3)\n        label = trainlabel[i]\n        if label-prediction==0:\n            x=0\n        else:\n            test_prediction(i, W1, b1, W2, b2, W3, b3)\n        ","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:52.575626Z","iopub.execute_input":"2023-01-05T15:46:52.576127Z","iopub.status.idle":"2023-01-05T15:46:52.585573Z","shell.execute_reply.started":"2023-01-05T15:46:52.576097Z","shell.execute_reply":"2023-01-05T15:46:52.584865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_error(100,w1,b1,w2,b2,w3,b3)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:52.586840Z","iopub.execute_input":"2023-01-05T15:46:52.587340Z","iopub.status.idle":"2023-01-05T15:46:53.705941Z","shell.execute_reply.started":"2023-01-05T15:46:52.587311Z","shell.execute_reply":"2023-01-05T15:46:53.705103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testpredictions = make_predictions(test, w1, b1, w2, b2,w3,b3)\nget_accuracy(testpredictions, testlabel)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:53.707494Z","iopub.execute_input":"2023-01-05T15:46:53.708028Z","iopub.status.idle":"2023-01-05T15:46:53.728453Z","shell.execute_reply.started":"2023-01-05T15:46:53.707998Z","shell.execute_reply":"2023-01-05T15:46:53.727632Z"},"trusted":true},"execution_count":null,"outputs":[]}]}