{"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":"markdown","source":"# Réseau de neurones MNIST en utilisant la base de Kaggle\n(Conversion en .csv des bases MNIST de Yann Lecun) ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport random\nimport pandas as pd\nfrom matplotlib import pyplot as plt\n\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2023-02-26T18:34:18.415767Z","iopub.execute_input":"2023-02-26T18:34:18.416160Z","iopub.status.idle":"2023-02-26T18:34:22.346869Z","shell.execute_reply.started":"2023-02-26T18:34:18.416128Z","shell.execute_reply":"2023-02-26T18:34:22.345185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\n\ndata_train = data[0:m].T\nY_train = data_train[0]\nX_train = data_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-26T18:34:22.348804Z","iopub.execute_input":"2023-02-26T18:34:22.349155Z","iopub.status.idle":"2023-02-26T18:34:22.619016Z","shell.execute_reply.started":"2023-02-26T18:34:22.349121Z","shell.execute_reply":"2023-02-26T18:34:22.617075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_params():\n    W1 = np.random.rand(100, 784) - 0.5\n    b1 = np.random.rand(100, 1) - 0.5\n    W2 = np.random.rand(10, 100) - 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))\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_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":"2023-02-26T18:34:22.621560Z","iopub.execute_input":"2023-02-26T18:34:22.622077Z","iopub.status.idle":"2023-02-26T18:34:22.638842Z","shell.execute_reply.started":"2023-02-26T18:34:22.622036Z","shell.execute_reply":"2023-02-26T18:34:22.637527Z"},"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, alpha, iterations):\n    W1, b1, W2, b2 = init_params()\n    file = open(\"neuralNetIterations.dat\", \"w\")\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        predictions = get_predictions(A2)\n        acc=get_accuracy(predictions, Y)\n        file.write(str(i)+\"\\t\"+str(100-acc)+\"\\n\")\n        if i % 10 == 0:\n            print(\"Iteration: \", i)\n            print(acc)\n    file.close()\n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2023-02-26T18:34:22.642664Z","iopub.execute_input":"2023-02-26T18:34:22.644368Z","iopub.status.idle":"2023-02-26T18:34:22.656703Z","shell.execute_reply.started":"2023-02-26T18:34:22.644289Z","shell.execute_reply":"2023-02-26T18:34:22.654901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nW1, b1, W2, b2 = gradient_descent(X_train, Y_train, 0.10, 500)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T18:34:22.658378Z","iopub.execute_input":"2023-02-26T18:34:22.658923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"On obtient environ 90% de précision sur 500 itérations dans la base de test\nJ'ai choisi de limiter les itérations a 500 et ne pas me baser sur un taux d'erreur afin de ne pas perdre beaucoup de temps entre les exécutions complètes, compte tenu de la faible puissance de notre machine.","metadata":{}},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)\n    predictions = get_predictions(A2)\n    return predictions\n\ndef test_prediction(totalError,index, W1, b1, W2, b2):\n    current_image = X_train[:, index, None]\n    prediction = make_predictions(current_image, W1, b1, W2, b2)\n    label = Y_train[index]\n    if label != prediction:\n        totalError=totalError+1\n    return totalError","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Calcul de l'erreur sur 100 éléments pris dans la base de test au hasard.","metadata":{}},{"cell_type":"code","source":"totalError = 0\n\nfor i in range(100):\n    totalError = test_prediction(totalError,i , W1, b1, W2, b2)\nprint(\"totalError: \", totalError)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PLOT de la descente du gradiant de l'apprentissage.\nSelon les valeurs écrites dans le fichier \"neuralNetIterations.dat\"","metadata":{}},{"cell_type":"code","source":"plt.plotfile('neuralNetIterations.dat', delimiter='\\t', cols=(0, 1), names=('Iterations', 'Accuracy'), linewidth=0.3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Utilisation du jeu de données de test pour s'assurer de la concordance de nos données.","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest = np.array(test)\na,z = test.shape\n\ndata_test = test[1:2000].T\nX_test = data_test[0:z]\nX_test = X_test / 255\nrandomList = random.sample(range(0, 2000), 100)\nfor i in randomList:\n    current_image = X_test[:, i, None]\n    prediction = make_predictions(current_image, W1, b1, W2, b2)\n    current_image = current_image.reshape((28, 28)) * 255\n    plt.gray()\n    plt.imshow(current_image, interpolation='nearest')\n    plt.show()\n    print(\"Prediction pour cette image: \", prediction)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}