{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "pqxnslml3tu6" }, "source": [ "# 신경망 모델 훈련" ] }, { "cell_type": "markdown", "metadata": { "id": "fN7zF6Zy3tvE" }, "source": [ "
\n",
" 구글 코랩에서 실행하기\n",
" | \n",
"
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",
"│ 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 (\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",
"│ flatten (Flatten) │ (None, 784) │ 0 │\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 = model_fn()\n", "\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "P-UK21N_mCM0" }, "outputs": [], "source": [ "model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", "\n", "history = model.fit(train_scaled, train_target, epochs=5, verbose=0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1ix_EA2LpaR0", "outputId": "3b9179e3-cd4f-4c14-afea-4d09331c4570" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "dict_keys(['accuracy', 'loss'])\n" ] } ], "source": [ "print(history.history.keys())" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 449 }, "id": "uz_TCdfPmG6e", "outputId": "0ff3e6cd-2552-4e1f-d887-89d381bcd190" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "
Model: \"sequential_4\"\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_4 (\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_8 (\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",
"│ dropout (\u001b[38;5;33mDropout\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m100\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n",
"├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
"│ dense_9 (\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_4 (Flatten) │ (None, 784) │ 0 │\n",
"├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
"│ dense_8 (Dense) │ (None, 100) │ 78,500 │\n",
"├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
"│ dropout (Dropout) │ (None, 100) │ 0 │\n",
"├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
"│ dense_9 (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 = model_fn(keras.layers.Dropout(0.3))\n", "\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "_TSe7oM9v1lW" }, "outputs": [], "source": [ "model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", "history = model.fit(train_scaled, train_target, epochs=20, verbose=0,\n", " validation_data=(val_scaled, val_target))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 449 }, "id": "Rj_syB_iv30l", "outputId": "1f3a4f5d-72cd-4e78-fb26-b51b72579417" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "