{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","execution":{"iopub.status.busy":"2024-07-08T04:09:22.521684Z","iopub.execute_input":"2024-07-08T04:09:22.522302Z","iopub.status.idle":"2024-07-08T04:09:22.530007Z","shell.execute_reply.started":"2024-07-08T04:09:22.522268Z","shell.execute_reply":"2024-07-08T04:09:22.529047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"THINGS I WANT TO ALTER:\n\nDecreasing dropout\nIncreasing dense layers\nIncreasing/decreasing number of filters and Conv2D Layers\nMore epochs (20?)\nGetting rid of strides\nRun without changing anything, see if anything gets fixed","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nfrom keras.utils import to_categorical\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:22.531719Z","iopub.execute_input":"2024-07-08T04:09:22.532014Z","iopub.status.idle":"2024-07-08T04:09:22.536801Z","shell.execute_reply.started":"2024-07-08T04:09:22.531990Z","shell.execute_reply":"2024-07-08T04:09:22.535838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:22.538301Z","iopub.execute_input":"2024-07-08T04:09:22.538932Z","iopub.status.idle":"2024-07-08T04:09:24.549810Z","shell.execute_reply.started":"2024-07-08T04:09:22.538908Z","shell.execute_reply":"2024-07-08T04:09:24.545522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.551422Z","iopub.status.idle":"2024-07-08T04:09:24.552062Z","shell.execute_reply.started":"2024-07-08T04:09:24.551778Z","shell.execute_reply":"2024-07-08T04:09:24.551801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.iloc[:,1:]\ny_train = train.iloc[:,0]\nX_test = test.iloc[:]\nX_train = X_train/255\ny_train = to_categorical(y_train, 10)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.553847Z","iopub.status.idle":"2024-07-08T04:09:24.554525Z","shell.execute_reply.started":"2024-07-08T04:09:24.554202Z","shell.execute_reply":"2024-07-08T04:09:24.554226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_reshaped = X_train.to_numpy().reshape(42000, 28, 28)\nX_test_reshaped = X_test.to_numpy().reshape(28000, 28, 28)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.556676Z","iopub.status.idle":"2024-07-08T04:09:24.557515Z","shell.execute_reply.started":"2024-07-08T04:09:24.557178Z","shell.execute_reply":"2024-07-08T04:09:24.557204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape, X_train.shape, X_test.shape, X_train_reshaped.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.559500Z","iopub.status.idle":"2024-07-08T04:09:24.560347Z","shell.execute_reply.started":"2024-07-08T04:09:24.560017Z","shell.execute_reply":"2024-07-08T04:09:24.560043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train_reshaped[np.random.random_integers(42000)])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.562656Z","iopub.status.idle":"2024-07-08T04:09:24.563504Z","shell.execute_reply.started":"2024-07-08T04:09:24.563175Z","shell.execute_reply":"2024-07-08T04:09:24.563201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, MaxPooling2D, BatchNormalization, Flatten, Dropout\n","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.565881Z","iopub.status.idle":"2024-07-08T04:09:24.567586Z","shell.execute_reply.started":"2024-07-08T04:09:24.567263Z","shell.execute_reply":"2024-07-08T04:09:24.567296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n\n    Conv2D(32, (3, 3), activation='relu', padding='valid', input_shape=(28, 28, 1)),\n    Conv2D(32, (3, 3), activation='relu', padding='valid'),\n    MaxPooling2D(strides=(2, 2)),\n    Conv2D(64, (3, 3), activation='relu', padding='valid'),\n    Conv2D(64, (3, 3), activation='relu', padding='valid'),\n    MaxPooling2D(strides=(2, 2)),\n    Flatten(),\n    Dense(512, activation='relu'),\n    Dense(10, activation='softmax')\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.569880Z","iopub.status.idle":"2024-07-08T04:09:24.571737Z","shell.execute_reply.started":"2024-07-08T04:09:24.571410Z","shell.execute_reply":"2024-07-08T04:09:24.571438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer= 'adam', metrics=['accuracy'])\nhistory = model.fit(X_train_reshaped, y_train, batch_size= 128, epochs= 110)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.574091Z","iopub.status.idle":"2024-07-08T04:09:24.575909Z","shell.execute_reply.started":"2024-07-08T04:09:24.575592Z","shell.execute_reply":"2024-07-08T04:09:24.575619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['loss'])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.578103Z","iopub.status.idle":"2024-07-08T04:09:24.579988Z","shell.execute_reply.started":"2024-07-08T04:09:24.579669Z","shell.execute_reply":"2024-07-08T04:09:24.579696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = model.predict(X_test_reshaped)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.582176Z","iopub.status.idle":"2024-07-08T04:09:24.584035Z","shell.execute_reply.started":"2024-07-08T04:09:24.583705Z","shell.execute_reply":"2024-07-08T04:09:24.583734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = np.argmax(pred_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.586287Z","iopub.status.idle":"2024-07-08T04:09:24.587248Z","shell.execute_reply.started":"2024-07-08T04:09:24.586962Z","shell.execute_reply":"2024-07-08T04:09:24.586987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test_reshaped[1])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.588993Z","iopub.status.idle":"2024-07-08T04:09:24.589980Z","shell.execute_reply.started":"2024-07-08T04:09:24.589687Z","shell.execute_reply":"2024-07-08T04:09:24.589712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels[1]","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.591721Z","iopub.status.idle":"2024-07-08T04:09:24.592827Z","shell.execute_reply.started":"2024-07-08T04:09:24.592494Z","shell.execute_reply":"2024-07-08T04:09:24.592520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Submission_df = pd.DataFrame()\nSubmission_df['ImageId'] = range(1, len(pred_test) + 1)\nSubmission_df['Label'] = pred_labels","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.594737Z","iopub.status.idle":"2024-07-08T04:09:24.595764Z","shell.execute_reply.started":"2024-07-08T04:09:24.595474Z","shell.execute_reply":"2024-07-08T04:09:24.595499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Submission_df.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T04:09:24.597594Z","iopub.status.idle":"2024-07-08T04:09:24.598606Z","shell.execute_reply.started":"2024-07-08T04:09:24.598259Z","shell.execute_reply":"2024-07-08T04:09:24.598314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}