{"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"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Data preprocessing","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense , Dropout , Lambda, Flatten\nfrom keras.optimizers import Adam ,RMSprop\nfrom sklearn.model_selection import train_test_split\nfrom keras import  backend as K\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n\n# Any results you write to the current directory are saved as output.\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-19T11:51:45.104671Z","iopub.execute_input":"2024-09-19T11:51:45.105055Z","iopub.status.idle":"2024-09-19T11:52:03.042768Z","shell.execute_reply.started":"2024-09-19T11:51:45.105023Z","shell.execute_reply":"2024-09-19T11:52:03.041279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create the training & test sets, skipping the header row with [1:]\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest= pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-09-19T11:52:03.045080Z","iopub.execute_input":"2024-09-19T11:52:03.045787Z","iopub.status.idle":"2024-09-19T11:52:08.646595Z","shell.execute_reply.started":"2024-09-19T11:52:03.045750Z","shell.execute_reply":"2024-09-19T11:52:08.645344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train.iloc[:,1:]\nx.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-09-19T11:52:08.647883Z","iopub.execute_input":"2024-09-19T11:52:08.648543Z","iopub.status.idle":"2024-09-19T11:52:08.668764Z","shell.execute_reply.started":"2024-09-19T11:52:08.648504Z","shell.execute_reply":"2024-09-19T11:52:08.667533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train.iloc[:,1:]\n\nX_train=x.values #pandas->numpy\nX_train=X_train.astype('float')\n","metadata":{"execution":{"iopub.status.busy":"2024-09-19T11:52:10.875821Z","iopub.status.idle":"2024-09-19T11:52:10.876237Z","shell.execute_reply.started":"2024-09-19T11:52:10.876045Z","shell.execute_reply":"2024-09-19T11:52:10.876062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y= train.iloc[:,0]\ny_train=y.values\ny_train=y_train.astype('int32')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T11:52:10.878112Z","iopub.status.idle":"2024-09-19T11:52:10.878566Z","shell.execute_reply.started":"2024-09-19T11:52:10.878364Z","shell.execute_reply":"2024-09-19T11:52:10.878385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX_test = test.values.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T11:52:10.879747Z","iopub.status.idle":"2024-09-19T11:52:10.880125Z","shell.execute_reply.started":"2024-09-19T11:52:10.879949Z","shell.execute_reply":"2024-09-19T11:52:10.879965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#expand 1 more dimention as 1 for colour channel gray\nX_train = X_train.reshape(X_train.shape[0], 28, 28,1)\nX_train.shape\nX_test = X_test.reshape(X_test.shape[0], 28, 28,1)\nX_test.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-02-08T07:28:48.084503Z","iopub.execute_input":"2024-02-08T07:28:48.084979Z","iopub.status.idle":"2024-02-08T07:28:48.093814Z","shell.execute_reply.started":"2024-02-08T07:28:48.084944Z","shell.execute_reply":"2024-02-08T07:28:48.091704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_px = X_train.mean().astype(np.float32)\nstd_px = X_train.std().astype(np.float32)\n\ndef standardize(x):\n    return (x-mean_px)/std_px","metadata":{"execution":{"iopub.status.busy":"2024-02-08T07:30:59.915055Z","iopub.execute_input":"2024-02-08T07:30:59.915572Z","iopub.status.idle":"2024-02-08T07:31:00.004932Z","shell.execute_reply.started":"2024-02-08T07:30:59.915531Z","shell.execute_reply":"2024-02-08T07:31:00.003346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Module","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Lambda, Conv2D, BatchNormalization, MaxPooling2D, Flatten, Dense, Dropout\n\n# Define your standardize function somewhere above this code.\n# It should take an input tensor and return a standardized tensor,\n# typically by subtracting the mean and dividing by the standard deviation.\n\nmodel = Sequential([\n    Lambda(standardize, input_shape=(28, 28, 1)),  # Make sure `standardize` is defined\n    Conv2D(filters=32, kernel_size=3, activation='relu', padding='same'),\n    BatchNormalization(axis=1),\n    Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),\n    MaxPooling2D(padding='same'),\n    BatchNormalization(axis=1),\n    Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.2),\n    Dense(10, activation='softmax')  # Last layer has 10 units for 10 classes\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-02-06T15:59:32.231325Z","iopub.execute_input":"2024-02-06T15:59:32.231822Z","iopub.status.idle":"2024-02-06T15:59:32.636152Z","shell.execute_reply.started":"2024-02-06T15:59:32.231788Z","shell.execute_reply":"2024-02-06T15:59:32.634850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" model.compile(optimizer=RMSprop(learning_rate=0.001),\n  loss='categorical_crossentropy',\n  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-06T15:59:35.793072Z","iopub.execute_input":"2024-02-06T15:59:35.793493Z","iopub.status.idle":"2024-02-06T15:59:35.827737Z","shell.execute_reply.started":"2024-02-06T15:59:35.793462Z","shell.execute_reply":"2024-02-06T15:59:35.826320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#one hot encoding: 9->0000000001,\nfrom tensorflow.keras.utils import to_categorical\ny_train= to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T15:59:36.632906Z","iopub.execute_input":"2024-02-06T15:59:36.633363Z","iopub.status.idle":"2024-02-06T15:59:36.643987Z","shell.execute_reply.started":"2024-02-06T15:59:36.633324Z","shell.execute_reply":"2024-02-06T15:59:36.642323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX = X_train\ny = y_train\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:00:05.567950Z","iopub.execute_input":"2024-02-06T16:00:05.568501Z","iopub.status.idle":"2024-02-06T16:00:05.652404Z","shell.execute_reply.started":"2024-02-06T16:00:05.568456Z","shell.execute_reply":"2024-02-06T16:00:05.650844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen =ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,\n                               height_shift_range=0.08, zoom_range=0.08)\nbatches = gen.flow(X_train, y_train, batch_size=64)\nval_batches = gen.flow(X_val, y_val, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:00:06.506552Z","iopub.execute_input":"2024-02-06T16:00:06.506991Z","iopub.status.idle":"2024-02-06T16:00:06.513823Z","shell.execute_reply.started":"2024-02-06T16:00:06.506960Z","shell.execute_reply":"2024-02-06T16:00:06.512912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x=batches, steps_per_epoch=batches.n, epochs=50,\n                    validation_data=val_batches, validation_steps=val_batches.n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit_generator(generator=batches, steps_per_epoch=batches.n, epochs=50,\n                    validation_data=val_batches, validation_steps=val_batches.n)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:00:07.393541Z","iopub.execute_input":"2024-02-06T16:00:07.394982Z","iopub.status.idle":"2024-02-06T16:02:02.154187Z","shell.execute_reply.started":"2024-02-06T16:00:07.394918Z","shell.execute_reply":"2024-02-06T16:02:02.152262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:02:10.260633Z","iopub.execute_input":"2024-02-06T16:02:10.261157Z","iopub.status.idle":"2024-02-06T16:02:10.270003Z","shell.execute_reply.started":"2024-02-06T16:02:10.261119Z","shell.execute_reply":"2024-02-06T16:02:10.268985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Assuming 'history' is the result from the fit function\n# And it contains 'mse' as a metric. If it's not the case, replace 'mse' and 'val_mse' with 'accuracy' and 'val_accuracy'\n\nmse = history.history['loss']\nval_mse = history.history['loss']\n\nepochs = range(1, len(mse) + 1)\n\nplt.plot(epochs, mse, 'bo', label='Training MSE')\nplt.plot(epochs, val_mse, 'b', label='Validation MSE')\nplt.title('Training and Validation MSE')\nplt.xlabel('Epochs')\nplt.ylabel('MSE')\nplt.legend()\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:02:12.175123Z","iopub.execute_input":"2024-02-06T16:02:12.176266Z","iopub.status.idle":"2024-02-06T16:02:12.462621Z","shell.execute_reply.started":"2024-02-06T16:02:12.176213Z","shell.execute_reply":"2024-02-06T16:02:12.461121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction=model.predict(X_test,verbose=0)\npredictions = np.argmax(prediction, axis=-1)\npredictions.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:02:15.534387Z","iopub.execute_input":"2024-02-06T16:02:15.534802Z","iopub.status.idle":"2024-02-06T16:02:40.582855Z","shell.execute_reply.started":"2024-02-06T16:02:15.534770Z","shell.execute_reply":"2024-02-06T16:02:40.581302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npred=pd.Series(predictions)\nsubmission=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission['Label']=pred\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T16:03:07.564066Z","iopub.execute_input":"2024-02-06T16:03:07.564530Z","iopub.status.idle":"2024-02-06T16:03:07.633742Z","shell.execute_reply.started":"2024-02-06T16:03:07.564498Z","shell.execute_reply":"2024-02-06T16:03:07.632409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}