{ "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": "4af696d2-94c6-4523-a4cb-552792574b48" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n", "29515/29515 [==============================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n", "26421880/26421880 [==============================] - 1s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n", "5148/5148 [==============================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n", "4422102/4422102 [==============================] - 1s 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" }, "outputs": [], "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/" }, "id": "em0xDl8qa12J", "outputId": "9cd9a669-2595-4783-869c-433ceac72cdd" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " dense (Dense) (None, 100) 78500 \n", " \n", " dense_1 (Dense) (None, 10) 1010 \n", " \n", "=================================================================\n", "Total params: 79510 (310.59 KB)\n", "Trainable params: 79510 (310.59 KB)\n", "Non-trainable params: 0 (0.00 Byte)\n", "_________________________________________________________________\n" ] } ], "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/" }, "id": "bwXDLSOWbm3L", "outputId": "336b4ac7-3f82-40fb-e225-fa115072f7b9" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"패션 MNIST 모델\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " hidden (Dense) (None, 100) 78500 \n", " \n", " output (Dense) (None, 10) 1010 \n", " \n", "=================================================================\n", "Total params: 79510 (310.59 KB)\n", "Trainable params: 79510 (310.59 KB)\n", "Non-trainable params: 0 (0.00 Byte)\n", "_________________________________________________________________\n" ] } ], "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/" }, "id": "bW2coaNQboe5", "outputId": "843207b4-596c-4500-b5c0-6dcd7a7bb786" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential_1\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " dense_2 (Dense) (None, 100) 78500 \n", " \n", " dense_3 (Dense) (None, 10) 1010 \n", " \n", "=================================================================\n", "Total params: 79510 (310.59 KB)\n", "Trainable params: 79510 (310.59 KB)\n", "Non-trainable params: 0 (0.00 Byte)\n", "_________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kkYrPJembpYk", "outputId": "b8849ec0-5274-4dc8-a653-c80b1fb6772b" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "1500/1500 [==============================] - 12s 3ms/step - loss: 0.5710 - accuracy: 0.8064\n", "Epoch 2/5\n", "1500/1500 [==============================] - 4s 3ms/step - loss: 0.4132 - accuracy: 0.8509\n", "Epoch 3/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.3776 - accuracy: 0.8646\n", "Epoch 4/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.3530 - accuracy: 0.8732\n", "Epoch 5/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.3344 - accuracy: 0.8782\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 11 } ], "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": 12, "metadata": { "id": "dG7yF8g6b062" }, "outputs": [], "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": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zHogWhu6g90a", "outputId": "4d4b7714-d834-499b-a928-2c9bd198134f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential_2\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " flatten (Flatten) (None, 784) 0 \n", " \n", " dense_4 (Dense) (None, 100) 78500 \n", " \n", " dense_5 (Dense) (None, 10) 1010 \n", " \n", "=================================================================\n", "Total params: 79510 (310.59 KB)\n", "Trainable params: 79510 (310.59 KB)\n", "Non-trainable params: 0 (0.00 Byte)\n", "_________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "code", "execution_count": 14, "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": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9PGejuuhdvvk", "outputId": "9d6860e5-ea5a-4240-eb45-9a6783c52321" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.5290 - accuracy: 0.8113\n", "Epoch 2/5\n", "1500/1500 [==============================] - 5s 4ms/step - loss: 0.3920 - accuracy: 0.8576\n", "Epoch 3/5\n", "1500/1500 [==============================] - 4s 3ms/step - loss: 0.3525 - accuracy: 0.8726\n", "Epoch 4/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.3301 - accuracy: 0.8821\n", "Epoch 5/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.3141 - accuracy: 0.8867\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "model.compile(loss='sparse_categorical_crossentropy', metrics='accuracy')\n", "\n", "model.fit(train_scaled, train_target, epochs=5)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lVYLpnjeep4y", "outputId": "1f3d6a5f-e210-442e-97d0-e5c393561466" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "375/375 [==============================] - 1s 2ms/step - loss: 0.3683 - accuracy: 0.8726\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "[0.3683287501335144, 0.8725833296775818]" ] }, "metadata": {}, "execution_count": 16 } ], "source": [ "model.evaluate(val_scaled, val_target)" ] }, { "cell_type": "markdown", "metadata": { "id": "3YtLsmGAoavz" }, "source": [ "## 옵티마이저" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "NxVj04Haocwa" }, "outputs": [], "source": [ "model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics='accuracy')" ] }, { "cell_type": "code", "execution_count": 18, "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": 19, "metadata": { "id": "Sh-HDiULlp18" }, "outputs": [], "source": [ "sgd = keras.optimizers.SGD(learning_rate=0.1)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "uF1XolBXsl3a" }, "outputs": [], "source": [ "sgd = keras.optimizers.SGD(momentum=0.9, nesterov=True)" ] }, { "cell_type": "code", "execution_count": 21, "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": 22, "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": 23, "metadata": { "id": "Gdu0hQIAz4JW" }, "outputs": [], "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": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2tcxIfILoi1t", "outputId": "f808142b-a65b-4e7a-a58d-4463fff4b8c7" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "1500/1500 [==============================] - 7s 4ms/step - loss: 0.5266 - accuracy: 0.8154\n", "Epoch 2/5\n", "1500/1500 [==============================] - 5s 3ms/step - loss: 0.3957 - accuracy: 0.8588\n", "Epoch 3/5\n", "1500/1500 [==============================] - 4s 3ms/step - loss: 0.3564 - accuracy: 0.8705\n", "Epoch 4/5\n", "1500/1500 [==============================] - 5s 4ms/step - loss: 0.3280 - accuracy: 0.8796\n", "Epoch 5/5\n", "1500/1500 [==============================] - 4s 3ms/step - loss: 0.3085 - accuracy: 0.8851\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 24 } ], "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": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8gxAWehsv9Gi", "outputId": "4d93e991-ae20-45fb-93e0-35f160829dd8" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "375/375 [==============================] - 1s 2ms/step - loss: 0.3485 - accuracy: 0.8763\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "[0.3484817445278168, 0.8762500286102295]" ] }, "metadata": {}, "execution_count": 25 } ], "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 }