{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"text-align: left;\">Convolutional Neural Networks (CNN) :&nbsp;</h1><div><h3 style=\"text-align: left;\">&nbsp;Content: içerik</h3><p></p><ol style=\"text-align: left;\"><li>&nbsp;Loading the Data Set</li><li>Normalization (normalleştirme), Reshape (yeniden şekillendirme)and Label Encoding (etiket kodlaması)</li><li>Train Test Split (datayı eğitim ve test datası olarak ayrımak )</li><li>Convolutional Neural Network (Evrişimsel Sinir Ağı)</li><li>What is Convolution Operation? ( Konvolüsyon : Birlikte evrimleşme - Matematikte ve özellike fonksiyonel analizde konvolüsyon ya da evrişim, bir fonksiyonun şeklinin başka fonksiyon tarafından nasıl modifiye edildiğini gösteren bir integral işlemdir.)</li><li>Same Padding (dolgu)</li><li>Max Pooling (havuzlama)</li><li>Flattening (düzleştirme)</li><li>Full Connection (tam bağlantı)</li><li>Implementing(uygulama) with Keras</li><li>Create Model</li><li>Define(tanımla) Optimizer</li><li>Compile(derleme) Model</li><li>Epochs and Batch Size</li><li>Data Augmentation(büyütme)</li><li>Fit(yerleştirmek) the Model</li><li>Evaluate(değerlendirmek) the Model</li><li>Deep Learning Tutorial for Beginners</li><li>Artificial Neural Network with Pytorch</li><li>Convolutional Neural Network with Pytorch</li><li>Recurrent Neural Network with Pytorch</li><li>Conclusion (sonuç)</li></ol><p></p></div>","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n# import warnings\nimport warnings\n# filter warnings\nwarnings.filterwarnings('ignore')\n\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":"2022-03-06T18:39:42.954753Z","iopub.execute_input":"2022-03-06T18:39:42.955373Z","iopub.status.idle":"2022-03-06T18:39:42.966665Z","shell.execute_reply.started":"2022-03-06T18:39:42.955327Z","shell.execute_reply":"2022-03-06T18:39:42.965658Z"},"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\")\nprint(train.shape)\nprint(test.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:39:46.252769Z","iopub.execute_input":"2022-03-06T18:39:46.253394Z","iopub.status.idle":"2022-03-06T18:39:50.861786Z","shell.execute_reply.started":"2022-03-06T18:39:46.253355Z","shell.execute_reply":"2022-03-06T18:39:50.861101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train[\"label\"]\nX_train = train.drop(labels = [\"label\"],axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:39:53.605419Z","iopub.execute_input":"2022-03-06T18:39:53.605945Z","iopub.status.idle":"2022-03-06T18:39:53.703159Z","shell.execute_reply.started":"2022-03-06T18:39:53.605894Z","shell.execute_reply":"2022-03-06T18:39:53.702242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5,5))\ng = sns.countplot(y=Y_train, palette=\"icefire\")\nplt.title(\"Number of digit classes\")\n\n# countplotta y= şeklinde yazarsan kategorileri y ye atıyor x e de toplam sayıları yani count u  atıyor ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:09.015649Z","iopub.execute_input":"2022-03-06T18:40:09.016261Z","iopub.status.idle":"2022-03-06T18:40:09.442782Z","shell.execute_reply.started":"2022-03-06T18:40:09.016205Z","shell.execute_reply":"2022-03-06T18:40:09.441591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.value_counts()\n# value counts kategorilerin sayısını çoktan aza doğru sıralıyor","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:05.602566Z","iopub.execute_input":"2022-03-06T18:40:05.602888Z","iopub.status.idle":"2022-03-06T18:40:05.610051Z","shell.execute_reply.started":"2022-03-06T18:40:05.602851Z","shell.execute_reply":"2022-03-06T18:40:05.609178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot some samples\nimg = X_train.iloc[0].to_numpy()\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[0,0])\nplt.axis(\"off\")\nplt.show()\n\n# burada veriyi görselleştiriyoruz : X_train deki ilk satırı alıyoruz 28 * 28 lik matris yapıyoruz .. ve gray renginde görselleştiriyoruz ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:13.348294Z","iopub.execute_input":"2022-03-06T18:40:13.348578Z","iopub.status.idle":"2022-03-06T18:40:13.447598Z","shell.execute_reply.started":"2022-03-06T18:40:13.348549Z","shell.execute_reply":"2022-03-06T18:40:13.446522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = X_train.iloc[3].to_numpy()\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[3,0])\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:21.124997Z","iopub.execute_input":"2022-03-06T18:40:21.125332Z","iopub.status.idle":"2022-03-06T18:40:21.220781Z","shell.execute_reply.started":"2022-03-06T18:40:21.125285Z","shell.execute_reply":"2022-03-06T18:40:21.219414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = X_train.iloc[300].to_numpy()\nimg = img.reshape((14,14,4)) \nplt.imshow(img,cmap='gray')\nplt.title(Y_train.iloc[300])\nplt.axis(\"off\")\nplt.show()\n\n# yani illa ki 28,28  yapmak zorunda değiliz / boyutu küçültüp renk vererek farklı resimler çıkarabiliriz 4,49,4 gibi ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:17.508793Z","iopub.execute_input":"2022-03-06T18:40:17.509464Z","iopub.status.idle":"2022-03-06T18:40:17.615515Z","shell.execute_reply.started":"2022-03-06T18:40:17.509411Z","shell.execute_reply":"2022-03-06T18:40:17.613420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalization, Reshape and Label Encoding\n\nNormalization\nWe perform a grayscale normalization to reduce the effect of illumination's differences. // Aydınlatma farklılıklarının etkisini azaltmak için gri tonlamalı bir normalleştirme gerçekleştiririz.\nIf we perform normalization, CNN works faster. // Normalleştirme yaparsak CNN daha hızlı çalışır.\n\nReshape\nTrain and test images (28 x 28)\nWe reshape all data to 28x28x1 3D matrices.\nKeras needs an extra dimension in the end which correspond to channels. Our images are gray scaled so it use only one channel.\n//Keras'ın sonunda kanallara karşılık gelen ekstra bir boyuta ihtiyacı var. Görüntülerimiz gri tonlamalı olduğundan yalnızca bir kanal kullanır. \n\nLabel Encoding\nEncode labels to one hot vectors  // Etiketleri tek bir sıcak vektöre kodlayın \n2 => [0,0,1,0,0,0,0,0,0,0]\n4 => [0,0,0,0,1,0,0,0,0,0]","metadata":{}},{"cell_type":"code","source":"# Normalize the data\nX_train = X_train / 255.0\ntest = test / 255.0\nprint(\"x_train shape: \",X_train.shape)\nprint(\"test shape: \",test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:24.595253Z","iopub.execute_input":"2022-03-06T18:40:24.595524Z","iopub.status.idle":"2022-03-06T18:40:24.695002Z","shell.execute_reply.started":"2022-03-06T18:40:24.595495Z","shell.execute_reply":"2022-03-06T18:40:24.694355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Label Encoding \nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nY_train = to_categorical(Y_train, num_classes = 10)\n\n# burada 0 dan 9 a kadar olan kategorileri  kod haline getiriyoruz  . mesela altın gümüş ve bronz madalya alanların altın için 1,0,0 gümüş için 0,1,0 olması gibi \n# bunun için kerasta hazır kod sistemi varmış : to_categorical diye","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:26.252786Z","iopub.execute_input":"2022-03-06T18:40:26.253091Z","iopub.status.idle":"2022-03-06T18:40:26.259149Z","shell.execute_reply.started":"2022-03-06T18:40:26.253055Z","shell.execute_reply":"2022-03-06T18:40:26.258200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:28.813913Z","iopub.execute_input":"2022-03-06T18:40:28.814611Z","iopub.status.idle":"2022-03-06T18:40:28.821779Z","shell.execute_reply.started":"2022-03-06T18:40:28.814557Z","shell.execute_reply":"2022-03-06T18:40:28.821092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(\"x_train shape: \",X_train.shape)\nprint(\"test shape: \",test.shape)\n\n# Keras 3 lü matrix ile çalıştığı için bu şekilde matrixi değiştiriyoruz ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:30.962368Z","iopub.execute_input":"2022-03-06T18:40:30.962850Z","iopub.status.idle":"2022-03-06T18:40:30.970418Z","shell.execute_reply.started":"2022-03-06T18:40:30.962811Z","shell.execute_reply":"2022-03-06T18:40:30.969668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train Test Split\nWe split the data into train and test sets.\ntest size is 10%.\ntrain size is 90%.","metadata":{}},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_test shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_test shape\",Y_val.shape)\n\n# burada gerçek yarışma olduğu için bize verilen train datasını train ve val diye ayırıyoruz bir de yarışmanın verdiği gerçek test datası var .. ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:37.589796Z","iopub.execute_input":"2022-03-06T18:40:37.590782Z","iopub.status.idle":"2022-03-06T18:40:38.012828Z","shell.execute_reply.started":"2022-03-06T18:40:37.590733Z","shell.execute_reply":"2022-03-06T18:40:38.011639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"What is Convolution Operation?\nWe have some image and feature detector(3*3)\nFeature detector does not need to be 3 by 3 matrix. It can be 5 by 5 or 7 by 7.\nFeature detector = kernel = filter\nFeauture detector detects features like edges or convex shapes. Example, if out input is dog, feature detector can detect features like ear or tail of the dog.\n\nÖzellik algılayıcı, kenarlar veya dışbükey şekiller gibi özellikleri algılar. Örneğin, çıkış girişi köpek ise, özellik dedektörü köpeğin kulağı veya kuyruğu gibi özellikleri algılayabilir.\n\n\nfeature map = conv(input image, feature detector). Element wise multiplication of matrices.\nfeature map = convolved feature\nStride = navigating in input image.   ///  resmin üzerinden kaçar alayarak gidiyor .. 1 er 1 er mi 23 şer 2 şer mi \nWe reduce the size of image. This is important bc code runs faster. However, we lost information.\nWe create multiple feature maps bc we use multiple feature detectors(filters).\nLets look at gimp. Edge detect: [0,10,0],[10,-4,10],[0,10,0]","metadata":{}},{"cell_type":"code","source":"# \nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\nmodel = Sequential()\n# filtre değişir\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n#\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n# fully connected\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:40.688521Z","iopub.execute_input":"2022-03-06T18:40:40.689207Z","iopub.status.idle":"2022-03-06T18:40:40.787380Z","shell.execute_reply.started":"2022-03-06T18:40:40.689154Z","shell.execute_reply":"2022-03-06T18:40:40.786337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)\noptimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:43.703966Z","iopub.execute_input":"2022-03-06T18:40:43.704311Z","iopub.status.idle":"2022-03-06T18:40:43.709476Z","shell.execute_reply.started":"2022-03-06T18:40:43.704272Z","shell.execute_reply":"2022-03-06T18:40:43.708506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:49.689378Z","iopub.execute_input":"2022-03-06T18:40:49.689993Z","iopub.status.idle":"2022-03-06T18:40:49.700509Z","shell.execute_reply.started":"2022-03-06T18:40:49.689951Z","shell.execute_reply":"2022-03-06T18:40:49.698997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set a learning rate annealer\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:40:51.981037Z","iopub.execute_input":"2022-03-06T18:40:51.981913Z","iopub.status.idle":"2022-03-06T18:40:51.986722Z","shell.execute_reply.started":"2022-03-06T18:40:51.981868Z","shell.execute_reply":"2022-03-06T18:40:51.985530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 2  \nbatch_size = 200","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:59:30.747034Z","iopub.execute_input":"2022-03-06T18:59:30.747579Z","iopub.status.idle":"2022-03-06T18:59:30.752043Z","shell.execute_reply.started":"2022-03-06T18:59:30.747530Z","shell.execute_reply":"2022-03-06T18:59:30.751081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data augmentation\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=10,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.1, # Randomly zoom image 10%\n        width_shift_range=0.1,  # randomly shift images horizontally 10%\n        height_shift_range=0.1,  # randomly shift images vertically 10%\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_train)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:59:33.154279Z","iopub.execute_input":"2022-03-06T18:59:33.154562Z","iopub.status.idle":"2022-03-06T18:59:33.292325Z","shell.execute_reply.started":"2022-03-06T18:59:33.154533Z","shell.execute_reply":"2022-03-06T18:59:33.291242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_val,Y_val), verbose = 2,\n                              steps_per_epoch=X_train.shape[0] // batch_size,\n                              callbacks=[learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2022-03-06T18:59:35.465201Z","iopub.execute_input":"2022-03-06T18:59:35.465484Z","iopub.status.idle":"2022-03-06T19:02:13.635553Z","shell.execute_reply.started":"2022-03-06T18:59:35.465455Z","shell.execute_reply":"2022-03-06T19:02:13.634476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the loss and accuracy curves for training and validation \nplt.plot(history.history['val_loss'], color='b', label=\"validation loss\")\nplt.title(\"Test Loss\")\nplt.xlabel(\"Number of Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:05:17.944348Z","iopub.execute_input":"2022-03-06T19:05:17.944829Z","iopub.status.idle":"2022-03-06T19:05:18.172155Z","shell.execute_reply.started":"2022-03-06T19:05:17.944777Z","shell.execute_reply":"2022-03-06T19:05:18.171296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:07:42.031592Z","iopub.execute_input":"2022-03-06T19:07:42.032456Z","iopub.status.idle":"2022-03-06T19:07:42.412426Z","shell.execute_reply.started":"2022-03-06T19:07:42.032395Z","shell.execute_reply":"2022-03-06T19:07:42.411324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix\nimport seaborn as sns\n# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_val,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nf,ax = plt.subplots(figsize=(8, 8))\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.01,cmap=\"Greens\",linecolor=\"gray\", fmt= '.1f',ax=ax)\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:07:51.991740Z","iopub.execute_input":"2022-03-06T19:07:51.992062Z","iopub.status.idle":"2022-03-06T19:07:55.259521Z","shell.execute_reply.started":"2022-03-06T19:07:51.992026Z","shell.execute_reply":"2022-03-06T19:07:55.258731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"errors = (Y_pred_classes - Y_true != 0)\n\nY_pred_classes_errors = Y_pred_classes[errors]\nY_pred_errors = Y_pred[errors]\nY_true_errors = Y_true[errors]\nX_val_errors = X_val[errors]\n\ndef display_errors(errors_index,img_errors,pred_errors, obs_errors):\n    \n    n = 0\n    nrows = 5\n    ncols = 4\n    fig, ax = plt.subplots(nrows,ncols,sharex=True,sharey=True,squeeze=True)\n    fig.set_size_inches(15, 20, forward=True)\n    for row in range(nrows):\n        for col in range(ncols):\n            error = errors_index[n]\n            ax[row,col].imshow((img_errors[error]).reshape((28,28)))\n            ax[row,col].set_title(\"Predicted label :{}\\nTrue label :{} \".format(pred_errors[error],obs_errors[error]))\n            n += 1\n\nY_pred_errors_prob = np.max(Y_pred_errors,axis = 1)\n\ntrue_prob_errors = np.diagonal(np.take(Y_pred_errors, Y_true_errors, axis=1))\n\ndelta_pred_true_errors = Y_pred_errors_prob - true_prob_errors\n\nsorted_dela_errors = np.argsort(delta_pred_true_errors)\n\nmost_important_errors = sorted_dela_errors[-20:]\n\ndisplay_errors(most_important_errors, X_val_errors, Y_pred_classes_errors, Y_true_errors)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:13:43.028365Z","iopub.execute_input":"2022-03-06T19:13:43.029307Z","iopub.status.idle":"2022-03-06T19:13:45.731630Z","shell.execute_reply.started":"2022-03-06T19:13:43.029260Z","shell.execute_reply":"2022-03-06T19:13:45.730769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(test[1000].reshape(1,28,28,1)).argmax()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:16:54.762521Z","iopub.execute_input":"2022-03-06T19:16:54.763055Z","iopub.status.idle":"2022-03-06T19:16:54.829213Z","shell.execute_reply.started":"2022-03-06T19:16:54.762988Z","shell.execute_reply":"2022-03-06T19:16:54.828331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test[1000],cmap='Greys')","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:18:34.834124Z","iopub.execute_input":"2022-03-06T19:18:34.834475Z","iopub.status.idle":"2022-03-06T19:18:35.053475Z","shell.execute_reply.started":"2022-03-06T19:18:34.834436Z","shell.execute_reply":"2022-03-06T19:18:35.052598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:19:01.066937Z","iopub.execute_input":"2022-03-06T19:19:01.067280Z","iopub.status.idle":"2022-03-06T19:19:19.758727Z","shell.execute_reply.started":"2022-03-06T19:19:01.067219Z","shell.execute_reply":"2022-03-06T19:19:19.757731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T19:19:39.659789Z","iopub.execute_input":"2022-03-06T19:19:39.660105Z","iopub.status.idle":"2022-03-06T19:19:39.715244Z","shell.execute_reply.started":"2022-03-06T19:19:39.660067Z","shell.execute_reply":"2022-03-06T19:19:39.714449Z"},"trusted":true},"execution_count":null,"outputs":[]}]}