{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:04:53.295533Z","iopub.execute_input":"2025-07-23T18:04:53.295881Z","iopub.status.idle":"2025-07-23T18:04:53.306862Z","shell.execute_reply.started":"2025-07-23T18:04:53.295859Z","shell.execute_reply":"2025-07-23T18:04:53.304855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nfrom keras.layers import Conv2D,Dense,Flatten,AveragePooling2D\nfrom keras import Sequential\nfrom keras.datasets import mnist\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:04:53.309142Z","iopub.execute_input":"2025-07-23T18:04:53.309647Z","iopub.status.idle":"2025-07-23T18:04:53.352274Z","shell.execute_reply.started":"2025-07-23T18:04:53.309615Z","shell.execute_reply":"2025-07-23T18:04:53.349782Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Making","metadata":{}},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(6,kernel_size=(5,5),padding='valid',activation='tanh',input_shape=(28,28,1)))\nmodel.add(AveragePooling2D(pool_size=(2,2),strides=2,padding='valid'))\nmodel.add(Conv2D(16,kernel_size=(5,5),padding='valid',activation='tanh'))\nmodel.add(AveragePooling2D(pool_size=(2,2),strides=2,padding='valid'))\nmodel.add(Flatten())\nmodel.add(Dense(120,activation='tanh'))\nmodel.add(Dense(84,activation='tanh'))\nmodel.add(Dense(10,activation='softmax'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:04:53.353510Z","iopub.execute_input":"2025-07-23T18:04:53.353940Z","iopub.status.idle":"2025-07-23T18:04:53.484052Z","shell.execute_reply.started":"2025-07-23T18:04:53.353903Z","shell.execute_reply":"2025-07-23T18:04:53.482623Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data :","metadata":{}},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:04:53.486316Z","iopub.execute_input":"2025-07-23T18:04:53.486702Z","iopub.status.idle":"2025-07-23T18:04:53.893056Z","shell.execute_reply.started":"2025-07-23T18:04:53.486675Z","shell.execute_reply":"2025-07-23T18:04:53.891482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.utils import to_categorical\n\ny_train = to_categorical(y_train, num_classes=10)\ny_test = to_categorical(y_test, num_classes=10)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:06:59.072025Z","iopub.execute_input":"2025-07-23T18:06:59.072459Z","iopub.status.idle":"2025-07-23T18:06:59.089615Z","shell.execute_reply.started":"2025-07-23T18:06:59.072390Z","shell.execute_reply":"2025-07-23T18:06:59.088470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:07:06.322529Z","iopub.execute_input":"2025-07-23T18:07:06.322884Z","iopub.status.idle":"2025-07-23T18:07:06.349729Z","shell.execute_reply.started":"2025-07-23T18:07:06.322860Z","shell.execute_reply":"2025-07-23T18:07:06.348486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\nmodel.fit(x_train,y_train,epochs=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:07:13.628330Z","iopub.execute_input":"2025-07-23T18:07:13.628811Z","iopub.status.idle":"2025-07-23T18:11:01.068465Z","shell.execute_reply.started":"2025-07-23T18:07:13.628780Z","shell.execute_reply":"2025-07-23T18:11:01.067447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(x_test, y_test)\nprint(f\"Test accuracy: {test_acc:.4f}, Test loss: {test_loss:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:25:18.841011Z","iopub.execute_input":"2025-07-23T18:25:18.841338Z","iopub.status.idle":"2025-07-23T18:25:21.311965Z","shell.execute_reply.started":"2025-07-23T18:25:18.841315Z","shell.execute_reply":"2025-07-23T18:25:21.310791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\npredictions = model.predict(x_test)\npredicted_classes = np.argmax(predictions, axis=1)\ntrue_classes = np.argmax(y_test, axis=1)  \n\nmisclassified_indices = np.where(predicted_classes != true_classes)[0]\n\nfor i in range(5):\n    idx = misclassified_indices[i]\n    plt.imshow(x_test[idx].reshape(28,28), cmap='gray')\n    plt.title(f\"True: {true_classes[idx]}, Pred: {predicted_classes[idx]}\")\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:26:28.489872Z","iopub.execute_input":"2025-07-23T18:26:28.490205Z","iopub.status.idle":"2025-07-23T18:26:31.251767Z","shell.execute_reply.started":"2025-07-23T18:26:28.490184Z","shell.execute_reply":"2025-07-23T18:26:31.250500Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"mnist_lenet5.h5\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T18:27:23.488623Z","iopub.execute_input":"2025-07-23T18:27:23.489021Z","iopub.status.idle":"2025-07-23T18:27:23.557201Z","shell.execute_reply.started":"2025-07-23T18:27:23.488996Z","shell.execute_reply":"2025-07-23T18:27:23.555903Z"}},"outputs":[],"execution_count":null}]}