{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nimport warnings\nimport numpy as np\nwarnings.filterwarnings(\"ignore\")\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-25T05:13:07.061015Z","iopub.execute_input":"2024-02-25T05:13:07.061819Z","iopub.status.idle":"2024-02-25T05:13:07.072679Z","shell.execute_reply.started":"2024-02-25T05:13:07.061777Z","shell.execute_reply":"2024-02-25T05:13:07.068622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train,y_train),(x_test,y_test)=keras.datasets.mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.074920Z","iopub.execute_input":"2024-02-25T05:13:07.075717Z","iopub.status.idle":"2024-02-25T05:13:07.412398Z","shell.execute_reply.started":"2024-02-25T05:13:07.075675Z","shell.execute_reply":"2024-02-25T05:13:07.410716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.414016Z","iopub.execute_input":"2024-02-25T05:13:07.414741Z","iopub.status.idle":"2024-02-25T05:13:07.423715Z","shell.execute_reply.started":"2024-02-25T05:13:07.414699Z","shell.execute_reply":"2024-02-25T05:13:07.422402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.matshow(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.425281Z","iopub.execute_input":"2024-02-25T05:13:07.425951Z","iopub.status.idle":"2024-02-25T05:13:07.739954Z","shell.execute_reply.started":"2024-02-25T05:13:07.425899Z","shell.execute_reply":"2024-02-25T05:13:07.739075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[0]","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.742715Z","iopub.execute_input":"2024-02-25T05:13:07.743295Z","iopub.status.idle":"2024-02-25T05:13:07.750479Z","shell.execute_reply.started":"2024-02-25T05:13:07.743263Z","shell.execute_reply":"2024-02-25T05:13:07.749029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.752124Z","iopub.execute_input":"2024-02-25T05:13:07.752691Z","iopub.status.idle":"2024-02-25T05:13:07.767791Z","shell.execute_reply.started":"2024-02-25T05:13:07.752660Z","shell.execute_reply":"2024-02-25T05:13:07.766394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(x_train)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.769410Z","iopub.execute_input":"2024-02-25T05:13:07.769848Z","iopub.status.idle":"2024-02-25T05:13:07.783805Z","shell.execute_reply.started":"2024-02-25T05:13:07.769807Z","shell.execute_reply":"2024-02-25T05:13:07.782483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.785391Z","iopub.execute_input":"2024-02-25T05:13:07.785806Z","iopub.status.idle":"2024-02-25T05:13:07.798570Z","shell.execute_reply.started":"2024-02-25T05:13:07.785773Z","shell.execute_reply":"2024-02-25T05:13:07.797122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train/255\nx_test=x_test/255","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.800267Z","iopub.execute_input":"2024-02-25T05:13:07.800698Z","iopub.status.idle":"2024-02-25T05:13:07.977321Z","shell.execute_reply.started":"2024-02-25T05:13:07.800664Z","shell.execute_reply":"2024-02-25T05:13:07.976069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_flattened=x_train.reshape(len(x_train),28*28)\nx_test_flattened=x_test.reshape(len(x_test),28*28)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.979030Z","iopub.execute_input":"2024-02-25T05:13:07.979404Z","iopub.status.idle":"2024-02-25T05:13:07.988184Z","shell.execute_reply.started":"2024-02-25T05:13:07.979372Z","shell.execute_reply":"2024-02-25T05:13:07.986700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_flattened.shape\nx_test_flattened.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:07.989756Z","iopub.execute_input":"2024-02-25T05:13:07.990145Z","iopub.status.idle":"2024-02-25T05:13:08.003864Z","shell.execute_reply.started":"2024-02-25T05:13:07.990114Z","shell.execute_reply":"2024-02-25T05:13:08.002470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=keras.Sequential(\n    [\n        keras.layers.Dense(10,input_shape=(784,),activation='sigmoid')\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:08.005712Z","iopub.execute_input":"2024-02-25T05:13:08.006204Z","iopub.status.idle":"2024-02-25T05:13:08.037570Z","shell.execute_reply.started":"2024-02-25T05:13:08.006162Z","shell.execute_reply":"2024-02-25T05:13:08.036195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodel.fit(x_train_flattened,y_train,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:08.039397Z","iopub.execute_input":"2024-02-25T05:13:08.039869Z","iopub.status.idle":"2024-02-25T05:13:52.597120Z","shell.execute_reply.started":"2024-02-25T05:13:08.039828Z","shell.execute_reply":"2024-02-25T05:13:52.595007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict=model.predict(x_train_flattened)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:52.603678Z","iopub.execute_input":"2024-02-25T05:13:52.604119Z","iopub.status.idle":"2024-02-25T05:13:56.994055Z","shell.execute_reply.started":"2024-02-25T05:13:52.604085Z","shell.execute_reply":"2024-02-25T05:13:56.992705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test_flattened,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:56.996388Z","iopub.execute_input":"2024-02-25T05:13:56.996766Z","iopub.status.idle":"2024-02-25T05:13:57.909390Z","shell.execute_reply.started":"2024-02-25T05:13:56.996736Z","shell.execute_reply":"2024-02-25T05:13:57.907833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:57.910895Z","iopub.execute_input":"2024-02-25T05:13:57.911282Z","iopub.status.idle":"2024-02-25T05:13:57.921873Z","shell.execute_reply.started":"2024-02-25T05:13:57.911248Z","shell.execute_reply":"2024-02-25T05:13:57.920127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict[0]","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:57.923268Z","iopub.execute_input":"2024-02-25T05:13:57.923670Z","iopub.status.idle":"2024-02-25T05:13:57.933755Z","shell.execute_reply.started":"2024-02-25T05:13:57.923628Z","shell.execute_reply":"2024-02-25T05:13:57.932187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.matshow(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:57.935499Z","iopub.execute_input":"2024-02-25T05:13:57.935976Z","iopub.status.idle":"2024-02-25T05:13:58.264510Z","shell.execute_reply.started":"2024-02-25T05:13:57.935944Z","shell.execute_reply":"2024-02-25T05:13:58.263111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(y_predict[0])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.266181Z","iopub.execute_input":"2024-02-25T05:13:58.266675Z","iopub.status.idle":"2024-02-25T05:13:58.277816Z","shell.execute_reply.started":"2024-02-25T05:13:58.266630Z","shell.execute_reply":"2024-02-25T05:13:58.276282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.matshow(x_train[10])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.279484Z","iopub.execute_input":"2024-02-25T05:13:58.280171Z","iopub.status.idle":"2024-02-25T05:13:58.615298Z","shell.execute_reply.started":"2024-02-25T05:13:58.280135Z","shell.execute_reply":"2024-02-25T05:13:58.614262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(y_predict[10])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.616647Z","iopub.execute_input":"2024-02-25T05:13:58.617047Z","iopub.status.idle":"2024-02-25T05:13:58.627735Z","shell.execute_reply.started":"2024-02-25T05:13:58.616963Z","shell.execute_reply":"2024-02-25T05:13:58.626428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted_labels=[]\nfor i in y_predict:\n    y_predicted_labels.append(np.argmax(i))\ny_predicted_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.629240Z","iopub.execute_input":"2024-02-25T05:13:58.629578Z","iopub.status.idle":"2024-02-25T05:13:58.858017Z","shell.execute_reply.started":"2024-02-25T05:13:58.629549Z","shell.execute_reply":"2024-02-25T05:13:58.856670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix=tf.math.confusion_matrix(labels=y_train,predictions=y_predicted_labels)\nconfusion_matrix","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.859395Z","iopub.execute_input":"2024-02-25T05:13:58.859775Z","iopub.status.idle":"2024-02-25T05:13:58.907154Z","shell.execute_reply.started":"2024-02-25T05:13:58.859742Z","shell.execute_reply":"2024-02-25T05:13:58.906269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_predicted_labels)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.908832Z","iopub.execute_input":"2024-02-25T05:13:58.910363Z","iopub.status.idle":"2024-02-25T05:13:58.918066Z","shell.execute_reply.started":"2024-02-25T05:13:58.910304Z","shell.execute_reply":"2024-02-25T05:13:58.917018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sn\nplt.figure(figsize=(10, 7))\nsn.heatmap(confusion_matrix, annot=True, fmt='d')\n\nplt.xlabel('Predicted')\nplt.ylabel('Truth')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:58.920649Z","iopub.execute_input":"2024-02-25T05:13:58.921951Z","iopub.status.idle":"2024-02-25T05:13:59.489920Z","shell.execute_reply.started":"2024-02-25T05:13:58.921839Z","shell.execute_reply":"2024-02-25T05:13:59.488714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=keras.Sequential(\n    [\n        keras.layers.Dense(100,input_shape=(784,),activation='relu'),\n        keras.layers.Dense(10,activation='sigmoid')\n    ]\n)\nmodel.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodel.fit(x_train_flattened,y_train,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:13:59.491523Z","iopub.execute_input":"2024-02-25T05:13:59.491930Z","iopub.status.idle":"2024-02-25T05:15:08.094861Z","shell.execute_reply.started":"2024-02-25T05:13:59.491899Z","shell.execute_reply":"2024-02-25T05:15:08.093390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test_flattened,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:15:08.096659Z","iopub.execute_input":"2024-02-25T05:15:08.097049Z","iopub.status.idle":"2024-02-25T05:15:09.192916Z","shell.execute_reply.started":"2024-02-25T05:15:08.097018Z","shell.execute_reply":"2024-02-25T05:15:09.191245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict=model.predict(x_train_flattened)\ny_predicted_labels=[]\nfor i in y_predict:\n    y_predicted_labels.append(np.argmax(i))\ny_predicted_labels[:5]\n\nconfusion_matrix=tf.math.confusion_matrix(labels=y_train,predictions=y_predicted_labels)\nconfusion_matrix","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:15:09.195084Z","iopub.execute_input":"2024-02-25T05:15:09.195574Z","iopub.status.idle":"2024-02-25T05:15:15.309590Z","shell.execute_reply.started":"2024-02-25T05:15:09.195531Z","shell.execute_reply":"2024-02-25T05:15:15.308375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sn\nplt.figure(figsize=(10, 7))\nsn.heatmap(confusion_matrix, annot=True, fmt='d')\n\nplt.xlabel('Predicted')\nplt.ylabel('Truth')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:15:15.310970Z","iopub.execute_input":"2024-02-25T05:15:15.311333Z","iopub.status.idle":"2024-02-25T05:15:15.886006Z","shell.execute_reply.started":"2024-02-25T05:15:15.311301Z","shell.execute_reply":"2024-02-25T05:15:15.884192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Save from extra work of flatterning the matrix\nmodel=keras.Sequential(\n    [\n        keras.layers.Flatten(input_shape=(28,28)),\n        keras.layers.Dense(100,activation='relu'),\n        keras.layers.Dense(10,activation='sigmoid')\n    ]\n)\nmodel.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodel.fit(x_train,y_train,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:15:15.887629Z","iopub.execute_input":"2024-02-25T05:15:15.887974Z","iopub.status.idle":"2024-02-25T05:16:26.649918Z","shell.execute_reply.started":"2024-02-25T05:15:15.887945Z","shell.execute_reply":"2024-02-25T05:16:26.648644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:16:26.651837Z","iopub.execute_input":"2024-02-25T05:16:26.652816Z","iopub.status.idle":"2024-02-25T05:16:28.161965Z","shell.execute_reply.started":"2024-02-25T05:16:26.652765Z","shell.execute_reply":"2024-02-25T05:16:28.160943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict=model.predict(x_test)\ny_predicted_labels=[]\nfor i in y_predict:\n    y_predicted_labels.append(np.argmax(i))\n\nconfusion_matrix=tf.math.confusion_matrix(labels=y_test,predictions=y_predicted_labels)\nconfusion_matrix","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:16:28.163465Z","iopub.execute_input":"2024-02-25T05:16:28.163937Z","iopub.status.idle":"2024-02-25T05:16:29.660512Z","shell.execute_reply.started":"2024-02-25T05:16:28.163902Z","shell.execute_reply":"2024-02-25T05:16:29.659311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sn\nplt.figure(figsize=(10, 7))\nsn.heatmap(confusion_matrix, annot=True, fmt='d')\n\nplt.xlabel('Predicted')\nplt.ylabel('Truth')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-25T05:16:29.662019Z","iopub.execute_input":"2024-02-25T05:16:29.662775Z","iopub.status.idle":"2024-02-25T05:16:30.229191Z","shell.execute_reply.started":"2024-02-25T05:16:29.662738Z","shell.execute_reply":"2024-02-25T05:16:30.227703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pls Upvote 🙏","metadata":{}}]}