{"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":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import 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\n#from keras import  backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\n\n\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-18T01:57:13.195751Z","iopub.execute_input":"2024-02-18T01:57:13.196411Z","iopub.status.idle":"2024-02-18T01:57:13.205295Z","shell.execute_reply.started":"2024-02-18T01:57:13.196375Z","shell.execute_reply":"2024-02-18T01:57:13.203929Z"},"trusted":true},"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","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:13.207849Z","iopub.execute_input":"2024-02-18T01:57:13.208658Z","iopub.status.idle":"2024-02-18T01:57:17.370595Z","shell.execute_reply.started":"2024-02-18T01:57:13.208618Z","shell.execute_reply":"2024-02-18T01:57:17.369268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = (train.iloc[:,1:].values).astype('float32') # all pixel values\ny_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\nX_test = test.values.astype('float32')\n","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:17.372044Z","iopub.execute_input":"2024-02-18T01:57:17.372386Z","iopub.status.idle":"2024-02-18T01:57:17.495928Z","shell.execute_reply.started":"2024-02-18T01:57:17.372358Z","shell.execute_reply":"2024-02-18T01:57:17.494899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#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","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:17.497098Z","iopub.execute_input":"2024-02-18T01:57:17.497405Z","iopub.status.idle":"2024-02-18T01:57:17.508574Z","shell.execute_reply.started":"2024-02-18T01:57:17.497380Z","shell.execute_reply":"2024-02-18T01:57:17.507352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#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-18T01:57:17.511220Z","iopub.execute_input":"2024-02-18T01:57:17.511522Z","iopub.status.idle":"2024-02-18T01:57:17.520069Z","shell.execute_reply.started":"2024-02-18T01:57:17.511498Z","shell.execute_reply":"2024-02-18T01:57:17.518894Z"},"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-18T01:57:17.521491Z","iopub.execute_input":"2024-02-18T01:57:17.521828Z","iopub.status.idle":"2024-02-18T01:57:17.634593Z","shell.execute_reply.started":"2024-02-18T01:57:17.521802Z","shell.execute_reply":"2024-02-18T01:57:17.633320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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])","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:17.636257Z","iopub.execute_input":"2024-02-18T01:57:17.636922Z","iopub.status.idle":"2024-02-18T01:57:17.785213Z","shell.execute_reply.started":"2024-02-18T01:57:17.636862Z","shell.execute_reply":"2024-02-18T01:57:17.784314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" model.compile(optimizer=RMSprop(lr=0.001),\n  loss='categorical_crossentropy',\n  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:17.786281Z","iopub.execute_input":"2024-02-18T01:57:17.787118Z","iopub.status.idle":"2024-02-18T01:57:17.800506Z","shell.execute_reply.started":"2024-02-18T01:57:17.787087Z","shell.execute_reply":"2024-02-18T01:57:17.799625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\ny_train= to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:17.801606Z","iopub.execute_input":"2024-02-18T01:57:17.802914Z","iopub.status.idle":"2024-02-18T01:57:17.808526Z","shell.execute_reply.started":"2024-02-18T01:57:17.802861Z","shell.execute_reply":"2024-02-18T01:57:17.807326Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:17.810049Z","iopub.execute_input":"2024-02-18T01:57:17.810538Z","iopub.status.idle":"2024-02-18T01:57:18.367887Z","shell.execute_reply.started":"2024-02-18T01:57:17.810511Z","shell.execute_reply":"2024-02-18T01:57:18.367035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-18T01:57:18.368912Z","iopub.execute_input":"2024-02-18T01:57:18.369617Z","iopub.status.idle":"2024-02-18T01:57:18.377683Z","shell.execute_reply.started":"2024-02-18T01:57:18.369586Z","shell.execute_reply":"2024-02-18T01:57:18.376308Z"},"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-18T01:57:18.379003Z","iopub.execute_input":"2024-02-18T01:57:18.379324Z","iopub.status.idle":"2024-02-18T01:57:18.386425Z","shell.execute_reply.started":"2024-02-18T01:57:18.379297Z","shell.execute_reply":"2024-02-18T01:57:18.385108Z"},"trusted":true},"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-18T01:57:18.387737Z","iopub.execute_input":"2024-02-18T01:57:18.388098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"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()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction=model.predict(X_test,verbose=0)\npredictions = np.argmax(prediction, axis=-1)\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":{"trusted":true},"execution_count":null,"outputs":[]}]}