{"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":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":8524199,"sourceType":"datasetVersion","datasetId":5090059}],"dockerImageVersionId":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import keras.models\nimport tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom keras.models import Sequential\nfrom keras.layers import Activation, Dense, Flatten, Conv2D\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\n# create a class that contains loss and accuracy batch information for plotting based on a simple model architecture\n# and a single training epoch\nclass BatchHistory(tf.keras.callbacks.Callback):\n    def __init__(self):\n        self.batch_accuracies = None\n        self.batch_losses = None\n\n    def on_train_begin(self, logs=None):\n        if logs is None:\n            logs = {}\n        self.batch_losses = []\n        self.batch_accuracies = []\n\n    def on_batch_end(self, batch, logs=None):\n        if logs is None:\n            logs = {}\n        self.batch_losses.append(logs.get('loss'))\n        self.batch_accuracies.append(logs.get('accuracy'))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-26T18:27:22.966058Z","iopub.execute_input":"2024-05-26T18:27:22.967031Z","iopub.status.idle":"2024-05-26T18:27:27.529089Z","shell.execute_reply.started":"2024-05-26T18:27:22.966830Z","shell.execute_reply":"2024-05-26T18:27:27.527936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load and normalize the MNIST dataset\ndataset_path = '/kaggle/input/mnist-dataset/mnist.npz'\nwith np.load(dataset_path) as data:\n    x_train, y_train = data['x_train'], data['y_train']\n    x_test, y_test = data['x_test'], data['y_test']\n\nIMG_SIZE = 28  # 28x28 pixels\n\n# resize images to apply convolution operation\nx_train_res = np.array(x_train).reshape(-1, IMG_SIZE, IMG_SIZE, 1)\nx_test_res = np.array(x_test).reshape(-1, IMG_SIZE, IMG_SIZE, 1)\n\n# create a neural network with a simplified architecture for baseline model\nmodel = Sequential()\nmodel.add(Conv2D(8, (3, 3), input_shape=(IMG_SIZE, IMG_SIZE, 1)))\nmodel.add(Activation('relu'))\n\nmodel.add(Flatten())\nmodel.add(Dense(8))  # contains 8 neurons\nmodel.add(Activation('relu'))\n\nmodel.add(Dense(10))\nmodel.add(Activation('softmax'))\n\n# compile model with a simpler optimizer (uses stochastic gradient descent instead of adam)\nmodel.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n\n# initialize callback\nbatch_history = BatchHistory()\n\n# train for a single epoch and record history\nhistory = model.fit(x_train_res, y_train, epochs=1, validation_split=0.1, callbacks=[batch_history])","metadata":{"execution":{"iopub.status.busy":"2024-05-26T18:27:27.535632Z","iopub.execute_input":"2024-05-26T18:27:27.535985Z","iopub.status.idle":"2024-05-26T18:27:37.945352Z","shell.execute_reply.started":"2024-05-26T18:27:27.535953Z","shell.execute_reply":"2024-05-26T18:27:37.943903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot training loss\nplt.figure(figsize=(12, 6))\nplt.subplot(1, 2, 1)\nplt.plot(batch_history.batch_losses, label='Baseline Loss')\nplt.xlabel('Batch')\nplt.ylabel('Loss')\nplt.legend()\nplt.title('Baseline Loss')\n\n# plot training accuracy\nplt.subplot(1, 2, 2)\nplt.plot(batch_history.batch_accuracies, label='Baseline Accuracy')\nplt.xlabel('Batch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.title('Baseline Accuracy')\nplt.show()\n\ntest_loss, test_acc = model.evaluate(x_test_res, y_test)\nprint('Test Loss: ', test_loss)\nprint('Test Accuracy: ', test_acc)\n\npredictions = model.predict(x_test_res)\n\n# create and plot confusion matrix\nconf_matrix = confusion_matrix(y_test, np.argmax(predictions, axis=1))\nplt.figure(figsize=(10, 8))\nsns.heatmap(conf_matrix, annot=True, cmap='Blues', fmt='d', xticklabels=range(10), yticklabels=range(10))\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-26T18:27:37.947107Z","iopub.execute_input":"2024-05-26T18:27:37.947530Z","iopub.status.idle":"2024-05-26T18:27:41.400543Z","shell.execute_reply.started":"2024-05-26T18:27:37.947496Z","shell.execute_reply":"2024-05-26T18:27:41.399387Z"},"trusted":true},"execution_count":null,"outputs":[]}]}