{"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":"# Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\npd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:51.145253Z","iopub.execute_input":"2023-08-24T18:48:51.145652Z","iopub.status.idle":"2023-08-24T18:48:51.151582Z","shell.execute_reply.started":"2023-08-24T18:48:51.145622Z","shell.execute_reply":"2023-08-24T18:48:51.150446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow-gpu","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:51.154650Z","iopub.execute_input":"2023-08-24T18:48:51.154970Z","iopub.status.idle":"2023-08-24T18:48:55.499429Z","shell.execute_reply.started":"2023-08-24T18:48:51.154944Z","shell.execute_reply":"2023-08-24T18:48:55.498387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:55.504255Z","iopub.execute_input":"2023-08-24T18:48:55.504632Z","iopub.status.idle":"2023-08-24T18:48:55.509864Z","shell.execute_reply.started":"2023-08-24T18:48:55.504596Z","shell.execute_reply":"2023-08-24T18:48:55.508931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:55.510828Z","iopub.execute_input":"2023-08-24T18:48:55.511182Z","iopub.status.idle":"2023-08-24T18:48:55.523908Z","shell.execute_reply.started":"2023-08-24T18:48:55.511154Z","shell.execute_reply":"2023-08-24T18:48:55.522458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing the dataset\n\nfrom tensorflow.keras.datasets import cifar10\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:55.525218Z","iopub.execute_input":"2023-08-24T18:48:55.525777Z","iopub.status.idle":"2023-08-24T18:48:56.490310Z","shell.execute_reply.started":"2023-08-24T18:48:55.525742Z","shell.execute_reply":"2023-08-24T18:48:56.488991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining the class names based on the additional data of the dataset\n\nclass_names = ['0: airplane', '1: automobile', '2: bird', '3: cat', '4: deer', '5: dog', '6: frog', '7: horse', '8: ship', '9: truck']\n\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:56.493614Z","iopub.execute_input":"2023-08-24T18:48:56.493989Z","iopub.status.idle":"2023-08-24T18:48:56.499481Z","shell.execute_reply.started":"2023-08-24T18:48:56.493957Z","shell.execute_reply":"2023-08-24T18:48:56.498638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This code snippet calculates the highest (maximum) and lowest (minimum) values\n# present in the x_train dataset. It can be useful for gaining an understanding\n# of the data's range and distribution, which can inform preprocessing or analysis steps.\n\nx_train.max(), x_train.min()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:56.500545Z","iopub.execute_input":"2023-08-24T18:48:56.500834Z","iopub.status.idle":"2023-08-24T18:48:56.546696Z","shell.execute_reply.started":"2023-08-24T18:48:56.500808Z","shell.execute_reply":"2023-08-24T18:48:56.545443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.max(), y_train.min()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:56.547880Z","iopub.execute_input":"2023-08-24T18:48:56.548159Z","iopub.status.idle":"2023-08-24T18:48:56.554846Z","shell.execute_reply.started":"2023-08-24T18:48:56.548135Z","shell.execute_reply":"2023-08-24T18:48:56.553771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize pixel values in x_train and x_test datasets.\n# This code divides all pixel values in the x_train and x_test datasets by 255.0.\n# This normalization process scales the pixel values to be in the range [0, 1],\n# which can help improve the training stability and convergence of neural networks.\n# It ensures that the model is not sensitive to the initial magnitude of pixel values\n# and takes into account the relative intensity of each pixel across the dataset.\n\nx_train = x_train / 255.0\nx_test = x_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:56.556555Z","iopub.execute_input":"2023-08-24T18:48:56.556926Z","iopub.status.idle":"2023-08-24T18:48:57.054722Z","shell.execute_reply.started":"2023-08-24T18:48:56.556890Z","shell.execute_reply":"2023-08-24T18:48:57.053694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display an image from the x_train dataset using matplotlib.\n# This code creates a figure, displays the image located at index 1 in the x_train dataset,\n# and adds a colorbar to indicate the pixel intensity values. The imshow function visualizes\n# the image, and the colorbar provides a reference for interpreting the pixel intensity scale.\n# It's a quick way to visually inspect individual images and their corresponding pixel values.\n\nplt.figure()\nplt.imshow(x_train[1])\nplt.colorbar()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.056333Z","iopub.execute_input":"2023-08-24T18:48:57.056812Z","iopub.status.idle":"2023-08-24T18:48:57.412864Z","shell.execute_reply.started":"2023-08-24T18:48:57.056773Z","shell.execute_reply":"2023-08-24T18:48:57.411723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Retrieve the shapes of x_train and x_test datasets.\n# This code returns the dimensions (shape) of the x_train and x_test datasets.\n# The shape of a dataset is represented as a tuple (num_samples, num_features),\n# where 'num_samples' is the number of data instances and 'num_features' is the number\n# of features (in this case, pixel values per image). This information is useful for\n# understanding the structure and size of the data being used for training and testing.\n\nx_train.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.414620Z","iopub.execute_input":"2023-08-24T18:48:57.415113Z","iopub.status.idle":"2023-08-24T18:48:57.422205Z","shell.execute_reply.started":"2023-08-24T18:48:57.415072Z","shell.execute_reply":"2023-08-24T18:48:57.421519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The result indicates that both the training and testing datasets \n# consist of images with a resolution of 32x32 pixels and 3 color channels (RGB). \n# The training dataset contains 50,000 images, while the testing dataset contains 10,000 images.","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.423675Z","iopub.execute_input":"2023-08-24T18:48:57.424397Z","iopub.status.idle":"2023-08-24T18:48:57.433686Z","shell.execute_reply.started":"2023-08-24T18:48:57.424358Z","shell.execute_reply":"2023-08-24T18:48:57.432783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.435297Z","iopub.execute_input":"2023-08-24T18:48:57.435705Z","iopub.status.idle":"2023-08-24T18:48:57.454396Z","shell.execute_reply.started":"2023-08-24T18:48:57.435668Z","shell.execute_reply":"2023-08-24T18:48:57.453452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add 1st convolutional layer with batch normalization and ReLU activation\nmodel.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=[32, 32, 3]))\nmodel.add(tf.keras.layers.BatchNormalization())","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.456122Z","iopub.execute_input":"2023-08-24T18:48:57.456538Z","iopub.status.idle":"2023-08-24T18:48:57.492623Z","shell.execute_reply.started":"2023-08-24T18:48:57.456501Z","shell.execute_reply":"2023-08-24T18:48:57.491554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add 2nd convolutional layer followed by max pooling\nmodel.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.497484Z","iopub.execute_input":"2023-08-24T18:48:57.497791Z","iopub.status.idle":"2023-08-24T18:48:57.522232Z","shell.execute_reply.started":"2023-08-24T18:48:57.497766Z","shell.execute_reply":"2023-08-24T18:48:57.521412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add 3rd convolutional layer with batch normalization and ReLU activation\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))\nmodel.add(tf.keras.layers.BatchNormalization())","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.524012Z","iopub.execute_input":"2023-08-24T18:48:57.524762Z","iopub.status.idle":"2023-08-24T18:48:57.561876Z","shell.execute_reply.started":"2023-08-24T18:48:57.524724Z","shell.execute_reply":"2023-08-24T18:48:57.561004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add 4th convolutional layer followed by max pooling\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.563683Z","iopub.execute_input":"2023-08-24T18:48:57.564504Z","iopub.status.idle":"2023-08-24T18:48:57.591981Z","shell.execute_reply.started":"2023-08-24T18:48:57.564455Z","shell.execute_reply":"2023-08-24T18:48:57.590839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add dropout to reduce overfitting\nmodel.add(tf.keras.layers.Dropout(0.4))","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.593528Z","iopub.execute_input":"2023-08-24T18:48:57.593930Z","iopub.status.idle":"2023-08-24T18:48:57.605895Z","shell.execute_reply.started":"2023-08-24T18:48:57.593894Z","shell.execute_reply":"2023-08-24T18:48:57.604689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Flatten the output of the previous layer\nmodel.add(tf.keras.layers.Flatten())","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.607167Z","iopub.execute_input":"2023-08-24T18:48:57.607892Z","iopub.status.idle":"2023-08-24T18:48:57.619897Z","shell.execute_reply.started":"2023-08-24T18:48:57.607859Z","shell.execute_reply":"2023-08-24T18:48:57.618665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add a dense layer with ReLU activation and dropout\nmodel.add(tf.keras.layers.Dense(units=128, activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.5))  # Adding dropout to dense layer","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.623490Z","iopub.execute_input":"2023-08-24T18:48:57.623966Z","iopub.status.idle":"2023-08-24T18:48:57.656955Z","shell.execute_reply.started":"2023-08-24T18:48:57.623935Z","shell.execute_reply":"2023-08-24T18:48:57.655856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add the output layer with softmax activation for classification\nmodel.add(tf.keras.layers.Dense(units=10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.658306Z","iopub.execute_input":"2023-08-24T18:48:57.659195Z","iopub.status.idle":"2023-08-24T18:48:57.680605Z","shell.execute_reply.started":"2023-08-24T18:48:57.659154Z","shell.execute_reply":"2023-08-24T18:48:57.679671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the initial learning rate for the optimizer\ninitial_learning_rate = 0.001\n\n# Create a learning rate schedule using ExponentialDecay\n# This schedule gradually reduces the learning rate over time.\n# The initial learning rate is 'initial_learning_rate'.\n# 'decay_steps' specifies how often to apply the decay (every 10,000 steps in this case).\n# 'decay_rate' determines the rate of decay (0.9 means reducing the learning rate by 10%).\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate, decay_steps=10000, decay_rate=0.9)\n\n# Create an Adam optimizer with the defined learning rate schedule\n# The optimizer adapts the learning rate during training based on the schedule.\noptimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.681867Z","iopub.execute_input":"2023-08-24T18:48:57.682565Z","iopub.status.idle":"2023-08-24T18:48:57.708737Z","shell.execute_reply.started":"2023-08-24T18:48:57.682526Z","shell.execute_reply":"2023-08-24T18:48:57.707619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.709989Z","iopub.execute_input":"2023-08-24T18:48:57.710297Z","iopub.status.idle":"2023-08-24T18:48:57.725815Z","shell.execute_reply.started":"2023-08-24T18:48:57.710270Z","shell.execute_reply":"2023-08-24T18:48:57.724661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:48:57.727483Z","iopub.execute_input":"2023-08-24T18:48:57.727917Z","iopub.status.idle":"2023-08-24T18:48:57.771909Z","shell.execute_reply.started":"2023-08-24T18:48:57.727881Z","shell.execute_reply":"2023-08-24T18:48:57.770907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model\n\nmodel.fit(x_train, y_train, batch_size=10, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T18:49:34.361852Z","iopub.execute_input":"2023-08-24T18:49:34.362964Z","iopub.status.idle":"2023-08-24T19:19:59.472430Z","shell.execute_reply.started":"2023-08-24T18:49:34.362928Z","shell.execute_reply":"2023-08-24T19:19:59.471255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model evaluation\n\ntest_loss, test_accuracy = model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:20:16.438202Z","iopub.execute_input":"2023-08-24T19:20:16.438583Z","iopub.status.idle":"2023-08-24T19:20:24.458797Z","shell.execute_reply.started":"2023-08-24T19:20:16.438552Z","shell.execute_reply":"2023-08-24T19:20:24.458035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_prob = model.predict(x_test)\ny_pred = y_pred_prob.argmax(axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:20:31.886499Z","iopub.execute_input":"2023-08-24T19:20:31.886889Z","iopub.status.idle":"2023-08-24T19:20:42.572336Z","shell.execute_reply.started":"2023-08-24T19:20:31.886848Z","shell.execute_reply":"2023-08-24T19:20:42.571151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making a single prediction\n\ny_pred[10]","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:21:26.876518Z","iopub.execute_input":"2023-08-24T19:21:26.876938Z","iopub.status.idle":"2023-08-24T19:21:26.884030Z","shell.execute_reply.started":"2023-08-24T19:21:26.876906Z","shell.execute_reply":"2023-08-24T19:21:26.882655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[10]","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:21:28.968361Z","iopub.execute_input":"2023-08-24T19:21:28.968783Z","iopub.status.idle":"2023-08-24T19:21:28.979709Z","shell.execute_reply.started":"2023-08-24T19:21:28.968752Z","shell.execute_reply":"2023-08-24T19:21:28.978065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, confusion_matrix\n\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:21:32.914564Z","iopub.execute_input":"2023-08-24T19:21:32.914990Z","iopub.status.idle":"2023-08-24T19:21:33.084216Z","shell.execute_reply.started":"2023-08-24T19:21:32.914957Z","shell.execute_reply":"2023-08-24T19:21:33.083083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\n# Create a heatmap for the confusion matrix\nplt.figure(figsize=(8, 6))\nsns.set(font_scale=1.2)  # Adjust font scale for better readability\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', cbar=False,\n            xticklabels=['0: airplane', '1: automobile', '2: bird', '3: cat', '4: deer', '5: dog', '6: frog', '7: horse', '8: ship', '9: truck'],\n            yticklabels=['0: airplane', '1: automobile', '2: bird', '3: cat', '4: deer', '5: dog', '6: frog', '7: horse', '8: ship', '9: truck'])\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:21:35.539860Z","iopub.execute_input":"2023-08-24T19:21:35.540254Z","iopub.status.idle":"2023-08-24T19:21:36.149497Z","shell.execute_reply.started":"2023-08-24T19:21:35.540219Z","shell.execute_reply":"2023-08-24T19:21:36.148196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we can see the most complicated pairs of objects to identify were:\n\n# - dog and cat\n# - horse and dog\n# - truck and automobile\n# - cat and deer\n# - airplane and bird\n# - bird and dog","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_cm = accuracy_score(y_test, y_pred)\n\nprint(acc_cm)","metadata":{"execution":{"iopub.status.busy":"2023-08-24T19:21:53.765361Z","iopub.execute_input":"2023-08-24T19:21:53.765752Z","iopub.status.idle":"2023-08-24T19:21:53.773142Z","shell.execute_reply.started":"2023-08-24T19:21:53.765724Z","shell.execute_reply":"2023-08-24T19:21:53.772145Z"},"trusted":true},"execution_count":null,"outputs":[]}]}