{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bffb7315",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-05-08 13:43:40.179295: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.1 SSE4.2\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras import layers "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4200b396",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_train shape: (60000, 28, 28, 1)\n",
      "60000 train samples\n",
      "10000 test samples\n"
     ]
    }
   ],
   "source": [
    "# Model / data parameters\n",
    "num_classes = 10\n",
    "input_shape = (28, 28, 1)\n",
    "\n",
    "# Load the data and split it between train and test sets\n",
    "(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
    "\n",
    "# Scale images to the [0, 1] range\n",
    "x_train = x_train.astype(\"float32\") / 255\n",
    "x_test = x_test.astype(\"float32\") / 255\n",
    "# Make sure images have shape (28, 28, 1)\n",
    "x_train = np.expand_dims(x_train, -1)\n",
    "x_test = np.expand_dims(x_test, -1)\n",
    "print(\"x_train shape:\", x_train.shape)\n",
    "print(x_train.shape[0], \"train samples\")\n",
    "print(x_test.shape[0], \"test samples\")\n",
    "\n",
    "\n",
    "# convert class vectors to binary class matrices\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "128bcb48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " conv2d (Conv2D)             (None, 26, 26, 32)        320       \n",
      "                                                                 \n",
      " max_pooling2d (MaxPooling2D  (None, 13, 13, 32)       0         \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv2d_1 (Conv2D)           (None, 11, 11, 64)        18496     \n",
      "                                                                 \n",
      " max_pooling2d_1 (MaxPooling  (None, 5, 5, 64)         0         \n",
      " 2D)                                                             \n",
      "                                                                 \n",
      " flatten (Flatten)           (None, 1600)              0         \n",
      "                                                                 \n",
      " dropout (Dropout)           (None, 1600)              0         \n",
      "                                                                 \n",
      " dense (Dense)               (None, 10)                16010     \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 34,826\n",
      "Trainable params: 34,826\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-05-08 13:44:01.973506: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.1 SSE4.2\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    }
   ],
   "source": [
    "model = keras.Sequential(\n",
    "    [\n",
    "        keras.Input(shape=input_shape),\n",
    "        layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n",
    "        layers.MaxPooling2D(pool_size=(2, 2)),\n",
    "        layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n",
    "        layers.MaxPooling2D(pool_size=(2, 2)),\n",
    "        layers.Flatten(),\n",
    "        layers.Dropout(0.5),\n",
    "        layers.Dense(num_classes, activation=\"softmax\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "model.summary()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "58ad127e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.3773 - accuracy: 0.8848 - val_loss: 0.0895 - val_accuracy: 0.9745\n",
      "Epoch 2/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.1153 - accuracy: 0.9646 - val_loss: 0.0571 - val_accuracy: 0.9835\n",
      "Epoch 3/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0849 - accuracy: 0.9737 - val_loss: 0.0450 - val_accuracy: 0.9878\n",
      "Epoch 4/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0717 - accuracy: 0.9778 - val_loss: 0.0429 - val_accuracy: 0.9885\n",
      "Epoch 5/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0621 - accuracy: 0.9804 - val_loss: 0.0368 - val_accuracy: 0.9903\n",
      "Epoch 6/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0571 - accuracy: 0.9827 - val_loss: 0.0398 - val_accuracy: 0.9902\n",
      "Epoch 7/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0505 - accuracy: 0.9840 - val_loss: 0.0352 - val_accuracy: 0.9900\n",
      "Epoch 8/15\n",
      "422/422 [==============================] - 10s 24ms/step - loss: 0.0471 - accuracy: 0.9846 - val_loss: 0.0341 - val_accuracy: 0.9912\n",
      "Epoch 9/15\n",
      "422/422 [==============================] - 10s 24ms/step - loss: 0.0448 - accuracy: 0.9859 - val_loss: 0.0335 - val_accuracy: 0.9913\n",
      "Epoch 10/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0412 - accuracy: 0.9868 - val_loss: 0.0303 - val_accuracy: 0.9917\n",
      "Epoch 11/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0395 - accuracy: 0.9872 - val_loss: 0.0279 - val_accuracy: 0.9925\n",
      "Epoch 12/15\n",
      "422/422 [==============================] - 10s 24ms/step - loss: 0.0365 - accuracy: 0.9884 - val_loss: 0.0286 - val_accuracy: 0.9930\n",
      "Epoch 13/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0351 - accuracy: 0.9886 - val_loss: 0.0280 - val_accuracy: 0.9920\n",
      "Epoch 14/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0350 - accuracy: 0.9883 - val_loss: 0.0289 - val_accuracy: 0.9917\n",
      "Epoch 15/15\n",
      "422/422 [==============================] - 10s 23ms/step - loss: 0.0316 - accuracy: 0.9897 - val_loss: 0.0306 - val_accuracy: 0.9925\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7fbc54279360>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_size = 128\n",
    "epochs = 15\n",
    "\n",
    "model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n",
    "\n",
    "model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8d3cfd8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test loss: 0.025060666725039482\n",
      "Test accuracy: 0.9919999837875366\n"
     ]
    }
   ],
   "source": [
    "score = model.evaluate(x_test, y_test, verbose=0)\n",
    "print(\"Test loss:\", score[0])\n",
    "print(\"Test accuracy:\", score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9254ac0b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
