{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# MNIST Model\nHello everyone !\n\nHere is one of my first \"Deep Learning project\"\nThe MNIST Dataset is a cool dataset of hand written digits, they are placed on a 28x28 grid. It would be important for the following explenation.\n\nFor this Model we will used Tensorflow and only tensorflow\n\nto install it you could use this command \n```sh\n    pip install tensorflow\n```","metadata":{"_uuid":"dbd3bcea-691f-4640-94c9-140128126aca","_cell_guid":"a283b041-64ea-4f26-99e0-1524aba763fe","trusted":true}},{"cell_type":"code","source":"%pip install tensorflow","metadata":{"_uuid":"78609c36-3374-40fb-b323-26d7189d0b26","_cell_guid":"39ac9711-15e3-4970-9a2c-8d8e8593311e","collapsed":false,"execution":{"iopub.status.busy":"2023-12-18T15:04:26.377608Z","iopub.execute_input":"2023-12-18T15:04:26.378258Z","iopub.status.idle":"2023-12-18T15:04:42.033901Z","shell.execute_reply.started":"2023-12-18T15:04:26.378223Z","shell.execute_reply":"2023-12-18T15:04:42.032498Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We import tensorflow as tf because tf is easyer to write\nimport tensorflow as tf","metadata":{"_uuid":"768ff04d-efe5-4f40-b42f-d5bf4851685f","_cell_guid":"c2718789-aee5-4c85-b156-48f5ac7315a3","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:26.187577Z","iopub.execute_input":"2023-12-18T15:09:26.188120Z","iopub.status.idle":"2023-12-18T15:09:26.193303Z","shell.execute_reply.started":"2023-12-18T15:09:26.188079Z","shell.execute_reply":"2023-12-18T15:09:26.192002Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import dataset using Tensorflow\n\nmnist = tf.keras.datasets.mnist\n#Separating the data between the 'TrainSet' and the 'TestSet'\n(x_train,y_train),(x_test,y_test) = mnist.load_data()","metadata":{"_uuid":"f07e97e7-4058-4b7e-8dc5-fb892d315f6a","_cell_guid":"7d2e5e75-34de-4cac-88cf-1a74447b03fc","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:26.623797Z","iopub.execute_input":"2023-12-18T15:09:26.624637Z","iopub.status.idle":"2023-12-18T15:09:26.990266Z","shell.execute_reply.started":"2023-12-18T15:09:26.624476Z","shell.execute_reply":"2023-12-18T15:09:26.988457Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using the normalize function helps to prevent loss of information during training and also ensures that the model can generalize well on unseen data.\nx_train = tf.keras.utils.normalize(x_train, axis=1)\nx_test = tf.keras.utils.normalize(x_test, axis=1)","metadata":{"_uuid":"65038a05-36af-47b2-88e8-e948816081eb","_cell_guid":"4f499147-ec2e-4870-8d76-824d49f13cc2","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:26.997999Z","iopub.execute_input":"2023-12-18T15:09:27.000085Z","iopub.status.idle":"2023-12-18T15:09:27.621210Z","shell.execute_reply.started":"2023-12-18T15:09:27.000012Z","shell.execute_reply":"2023-12-18T15:09:27.619813Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Choosing the models \"Sequential()\"\nmodel = tf.keras.models.Sequential()","metadata":{"_uuid":"614b2ef4-44a5-4f08-bcff-f65a3107589d","_cell_guid":"a3cccf49-cc43-4aec-a7f0-59312b985fe9","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:27.622960Z","iopub.execute_input":"2023-12-18T15:09:27.623350Z","iopub.status.idle":"2023-12-18T15:09:27.635389Z","shell.execute_reply.started":"2023-12-18T15:09:27.623313Z","shell.execute_reply":"2023-12-18T15:09:27.633591Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Here we used the\"layers Flatten\" to put the image who is a 2d vector into a 1d vector from (28x28) to (1x784)\n# This Layer is the entry one\n\n\nmodel.add(tf.keras.layers.Flatten(input_shape=(28,28)))\n#Here is the building the two \"hiden layers\" Who will ajst the wheigt and biais of the neural network\nmodel.add(tf.keras.layers.Dense(128, activation='relu'))\nmodel.add(tf.keras.layers.Dense(128, activation='relu'))\n#Here is the output of the neural network, There is only 10 possible output so the output number is 10.\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))","metadata":{"_uuid":"77c5e5a1-d3ee-41a8-8a30-e25afea2f8e1","_cell_guid":"add4e342-237a-473a-b5d0-4b17e3212100","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:27.652420Z","iopub.execute_input":"2023-12-18T15:09:27.652911Z","iopub.status.idle":"2023-12-18T15:09:27.728934Z","shell.execute_reply.started":"2023-12-18T15:09:27.652870Z","shell.execute_reply":"2023-12-18T15:09:27.727333Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Here is the compile fonction where we define \n# Why Adam ? This is what i found in the tensorflow docs\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"_uuid":"aa2ef181-4e46-4ef4-aa86-9146930ddeb7","_cell_guid":"ea35c029-7724-4b30-8af5-be1a110d87bb","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:28.143145Z","iopub.execute_input":"2023-12-18T15:09:28.143935Z","iopub.status.idle":"2023-12-18T15:09:28.163977Z","shell.execute_reply.started":"2023-12-18T15:09:28.143895Z","shell.execute_reply":"2023-12-18T15:09:28.159514Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fiting the model using the fit() function\nmodel.fit(x_train, y_train, epochs=3)\n#Adding epochs can help improving the accuracy\nmodel_5 = model.fit(x_train, y_train, epochs=5)","metadata":{"_uuid":"9a0d0675-ed6f-459a-9332-23451dfa7ec5","_cell_guid":"2b4194fa-1fc0-4718-82a1-49fa506db675","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:09:28.685376Z","iopub.execute_input":"2023-12-18T15:09:28.685890Z","iopub.status.idle":"2023-12-18T15:10:52.477898Z","shell.execute_reply.started":"2023-12-18T15:09:28.685848Z","shell.execute_reply":"2023-12-18T15:10:52.476115Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Output analysis\n\nWe can check the mean of the 2 epochs :\n- The model_3 with 3 epoch have a accuracy of 95,54% and a loss of 0,1480\n- The model_5 with 5 epoch have a accuracy of 98,88% and a loss of 0,138\n\nWe can see that the at the 3/3 epochs the accuracy was at 97,77% and at the 5/5 epochs the acuracy was at 99,26%\nWich means that for 100 image tested, 0,74% were missclasified","metadata":{"_uuid":"6f88f4e1-1e34-4a75-885f-c3554e449662","_cell_guid":"58c0d2e2-1c8a-4fa0-a181-d125dc2811b9","trusted":true}},{"cell_type":"code","source":"#Saving the modele\nmodel.save('numberRecognition.model')","metadata":{"_uuid":"94dad301-57e9-4584-b50a-5d2aebdb6e74","_cell_guid":"1a9aa383-1251-4d12-b765-79bde88a63ed","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:20:38.724602Z","iopub.execute_input":"2023-12-18T15:20:38.725149Z","iopub.status.idle":"2023-12-18T15:20:39.438620Z","shell.execute_reply.started":"2023-12-18T15:20:38.725108Z","shell.execute_reply":"2023-12-18T15:20:39.437035Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading the model\n\n#new_model = tf.keras.load_model('numberRecognition.model')","metadata":{"_uuid":"3b540c9a-cb29-4fa9-99f8-68f649244f64","_cell_guid":"e5b22f9f-4982-4412-ad1a-fb2f24fcf824","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:20:39.973638Z","iopub.execute_input":"2023-12-18T15:20:39.974267Z","iopub.status.idle":"2023-12-18T15:20:39.980854Z","shell.execute_reply.started":"2023-12-18T15:20:39.974196Z","shell.execute_reply":"2023-12-18T15:20:39.979406Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluating the loss and the accuracy of the model.\nloss, accuracy = model.evaluate(x_test,y_test)\nprint(loss)\nprint(accuracy)","metadata":{"_uuid":"c04a6f6f-65a2-4aee-83ff-9af4d5b323ce","_cell_guid":"4d6487c7-6080-4fd2-aeb9-2544ec4b8237","collapsed":false,"tags":[],"execution":{"iopub.status.busy":"2023-12-18T15:20:43.004523Z","iopub.execute_input":"2023-12-18T15:20:43.004971Z","iopub.status.idle":"2023-12-18T15:20:43.715445Z","shell.execute_reply.started":"2023-12-18T15:20:43.004934Z","shell.execute_reply":"2023-12-18T15:20:43.714263Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}