{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "iNFOeMfl3tIu" }, "source": [ "# 심층 신경망" ] }, { "cell_type": "markdown", "metadata": { "id": "zKfwb8gS3tI2" }, "source": [ "\n", " \n", "
\n", " 구글 코랩에서 실행하기\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "ZT8SDtZVv41l" }, "outputs": [], "source": [ "# 실행마다 동일한 결과를 얻기 위해 케라스에 랜덤 시드를 사용하고 텐서플로 연산을 결정적으로 만듭니다.\n", "import tensorflow as tf\n", "\n", "tf.keras.utils.set_random_seed(42)\n", "tf.config.experimental.enable_op_determinism()" ] }, { "cell_type": "markdown", "metadata": { "id": "dPE5XsFhcfVD" }, "source": [ "## 2개의 층" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4sNOMcdaFVKa", "outputId": "172da922-8d45-4894-a309-141325c36a46" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n", "\u001b[1m29515/29515\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n", "\u001b[1m26421880/26421880\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n", "\u001b[1m5148/5148\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 1us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n", "\u001b[1m4422102/4422102\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n" ] } ], "source": [ "from tensorflow import keras\n", "\n", "(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "aJJiRMa6FkWx" }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "train_scaled = train_input / 255.0\n", "train_scaled = train_scaled.reshape(-1, 28*28)\n", "\n", "train_scaled, val_scaled, train_target, val_target = train_test_split(\n", " train_scaled, train_target, test_size=0.2, random_state=42)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "MCZWQiEwF6MD", "outputId": "fbe50704-5d7f-47de-dd27-d1b3b610511f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n", " super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n" ] } ], "source": [ "dense1 = keras.layers.Dense(100, activation='sigmoid', input_shape=(784,))\n", "dense2 = keras.layers.Dense(10, activation='softmax')" ] }, { "cell_type": "markdown", "metadata": { "id": "Agy5gCVhcrm-" }, "source": [ "## 심층 신경망 만들기" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "xmWL7kOoGB4P" }, "outputs": [], "source": [ "model = keras.Sequential([dense1, dense2])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 197 }, "id": "em0xDl8qa12J", "outputId": "31e599be-a83e-41f0-bf73-759741557e5d" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1mModel: \"sequential\"\u001b[0m\n" ], "text/html": [ "
Model: \"sequential\"\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", "│ dense (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m100\u001b[0m) │ \u001b[38;5;34m78,500\u001b[0m │\n", "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", "│ dense_1 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m1,010\u001b[0m │\n", "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
              "│ dense (Dense)                        │ (None, 100)                 │          78,500 │\n",
              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
              "│ dense_1 (Dense)                      │ (None, 10)                  │           1,010 │\n",
              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Total params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Trainable params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
              "
\n" ] }, "metadata": {} } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "qAi41rBTdk7k" }, "source": [ "## 층을 추가하는 다른 방법" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "0XeV6V4ha3I8" }, "outputs": [], "source": [ "model = keras.Sequential([\n", " keras.layers.Dense(100, activation='sigmoid', input_shape=(784,), name='hidden'),\n", " keras.layers.Dense(10, activation='softmax', name='output')\n", "], name='패션 MNIST 모델')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 198 }, "id": "bwXDLSOWbm3L", "outputId": "a14d71eb-5765-4c0a-81af-8f8d2f8e7d50" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1mModel: \"패션 MNIST 모델\"\u001b[0m\n" ], "text/html": [ "
Model: \"패션 MNIST 모델\"\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", "│ hidden (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m100\u001b[0m) │ \u001b[38;5;34m78,500\u001b[0m │\n", "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", "│ output (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m1,010\u001b[0m │\n", "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
              "│ hidden (Dense)                       │ (None, 100)                 │          78,500 │\n",
              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
              "│ output (Dense)                       │ (None, 10)                  │           1,010 │\n",
              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Total params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Trainable params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
              "
\n" ] }, "metadata": {} } ], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "yZSAxgZCbax7" }, "outputs": [], "source": [ "model = keras.Sequential()\n", "model.add(keras.layers.Dense(100, activation='sigmoid', input_shape=(784,)))\n", "model.add(keras.layers.Dense(10, activation='softmax'))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 197 }, "id": "bW2coaNQboe5", "outputId": "13286f4d-4081-4ba3-cfbf-03e930749e62" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1mModel: \"sequential_1\"\u001b[0m\n" ], "text/html": [ "
Model: \"sequential_1\"\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", "│ dense_2 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m100\u001b[0m) │ \u001b[38;5;34m78,500\u001b[0m │\n", "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", "│ dense_3 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m1,010\u001b[0m │\n", "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
              "│ dense_2 (Dense)                      │ (None, 100)                 │          78,500 │\n",
              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
              "│ dense_3 (Dense)                      │ (None, 10)                  │           1,010 │\n",
              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Total params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Trainable params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
              "
\n" ] }, "metadata": {} } ], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kkYrPJembpYk", "outputId": "02c5131e-b695-4108-9f1b-754d419b8b39" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 3ms/step - accuracy: 0.7525 - loss: 0.7720\n", "Epoch 2/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 2ms/step - accuracy: 0.8463 - loss: 0.4270\n", "Epoch 3/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 2ms/step - accuracy: 0.8604 - loss: 0.3857\n", "Epoch 4/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.8696 - loss: 0.3600\n", "Epoch 5/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 2ms/step - accuracy: 0.8759 - loss: 0.3410\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", "\n", "model.fit(train_scaled, train_target, epochs=5)" ] }, { "cell_type": "markdown", "metadata": { "id": "_dfXJDhwcyAK" }, "source": [ "## 렐루 활성화 함수" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "dG7yF8g6b062", "outputId": "71b3e0e9-d1b2-46bb-dd3d-eb25e8487b16", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n", " super().__init__(**kwargs)\n" ] } ], "source": [ "model = keras.Sequential()\n", "model.add(keras.layers.Flatten(input_shape=(28, 28)))\n", "model.add(keras.layers.Dense(100, activation='relu'))\n", "model.add(keras.layers.Dense(10, activation='softmax'))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 230 }, "id": "zHogWhu6g90a", "outputId": "2491bab6-148a-4827-cfd2-1bc16f2c907a" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1mModel: \"sequential_2\"\u001b[0m\n" ], "text/html": [ "
Model: \"sequential_2\"\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", "│ flatten (\u001b[38;5;33mFlatten\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m784\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n", "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", "│ dense_4 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m100\u001b[0m) │ \u001b[38;5;34m78,500\u001b[0m │\n", "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", "│ dense_5 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m1,010\u001b[0m │\n", "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
              "│ flatten (Flatten)                    │ (None, 784)                 │               0 │\n",
              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
              "│ dense_4 (Dense)                      │ (None, 100)                 │          78,500 │\n",
              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
              "│ dense_5 (Dense)                      │ (None, 10)                  │           1,010 │\n",
              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Total params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m79,510\u001b[0m (310.59 KB)\n" ], "text/html": [ "
 Trainable params: 79,510 (310.59 KB)\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
              "
\n" ] }, "metadata": {} } ], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "JfPe_ruQdhqA" }, "outputs": [], "source": [ "(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()\n", "\n", "train_scaled = train_input / 255.0\n", "\n", "train_scaled, val_scaled, train_target, val_target = train_test_split(\n", " train_scaled, train_target, test_size=0.2, random_state=42)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9PGejuuhdvvk", "outputId": "4fc4dfa3-5969-4495-e6e6-92b1225a6882" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.7637 - loss: 0.6723\n", "Epoch 2/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 2ms/step - accuracy: 0.8515 - loss: 0.4054\n", "Epoch 3/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.8676 - loss: 0.3595\n", "Epoch 4/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.8786 - loss: 0.3344\n", "Epoch 5/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.8858 - loss: 0.3177\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 17 } ], "source": [ "model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", "\n", "model.fit(train_scaled, train_target, epochs=5)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lVYLpnjeep4y", "outputId": "58a72357-71df-4dc4-cd36-8f554343a9f2" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[1m375/375\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 1ms/step - accuracy: 0.8671 - loss: 0.3837\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "[0.3847014605998993, 0.8665000200271606]" ] }, "metadata": {}, "execution_count": 18 } ], "source": [ "model.evaluate(val_scaled, val_target)" ] }, { "cell_type": "markdown", "metadata": { "id": "3YtLsmGAoavz" }, "source": [ "## 옵티마이저" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "NxVj04Haocwa" }, "outputs": [], "source": [ "model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "1426O4G8Hpfi" }, "outputs": [], "source": [ "sgd = keras.optimizers.SGD()\n", "model.compile(optimizer=sgd, loss='sparse_categorical_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "id": "Sh-HDiULlp18" }, "outputs": [], "source": [ "sgd = keras.optimizers.SGD(learning_rate=0.1)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "id": "uF1XolBXsl3a" }, "outputs": [], "source": [ "sgd = keras.optimizers.SGD(momentum=0.9, nesterov=True)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "id": "Hy2MENbL170j" }, "outputs": [], "source": [ "adagrad = keras.optimizers.Adagrad()\n", "model.compile(optimizer=adagrad, loss='sparse_categorical_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "id": "KkpbSMXWtakr" }, "outputs": [], "source": [ "rmsprop = keras.optimizers.RMSprop()\n", "model.compile(optimizer=rmsprop, loss='sparse_categorical_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "id": "Gdu0hQIAz4JW", "outputId": "9eed5bc9-f729-47da-b3c7-1c407e470da3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n", " super().__init__(**kwargs)\n" ] } ], "source": [ "model = keras.Sequential()\n", "model.add(keras.layers.Flatten(input_shape=(28, 28)))\n", "model.add(keras.layers.Dense(100, activation='relu'))\n", "model.add(keras.layers.Dense(10, activation='softmax'))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2tcxIfILoi1t", "outputId": "6f650dcc-2441-4df3-8e3d-45a744f2dd70" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.7691 - loss: 0.6706\n", "Epoch 2/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.8515 - loss: 0.4134\n", "Epoch 3/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.8691 - loss: 0.3618\n", "Epoch 4/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.8793 - loss: 0.3302\n", "Epoch 5/5\n", "\u001b[1m1500/1500\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.8873 - loss: 0.3088\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 28 } ], "source": [ "model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", "\n", "model.fit(train_scaled, train_target, epochs=5)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8gxAWehsv9Gi", "outputId": "5659c776-a4f5-4dad-a9c1-4ac4ad36d498" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[1m375/375\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 1ms/step - accuracy: 0.8762 - loss: 0.3506\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "[0.35239025950431824, 0.8725833296775818]" ] }, "metadata": {}, "execution_count": 29 } ], "source": [ "model.evaluate(val_scaled, val_target)" ] } ], "metadata": { "accelerator": "GPU", "colab": { "name": "7-2 심층 신경망.ipynb", "provenance": [] }, "kernelspec": { "display_name": "default:Python", "language": "python", "name": "conda-env-default-py" }, "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.9.10" } }, "nbformat": 4, "nbformat_minor": 0 }