{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train on 18000 samples, validate on 42000 samples\n", "Epoch 1/5\n", "18000/18000 [==============================] - 2s 117us/step - loss: 1.1511 - acc: 0.7256 - val_loss: 0.6600 - val_acc: 0.8370\n", "Epoch 2/5\n", "18000/18000 [==============================] - 2s 85us/step - loss: 0.5191 - acc: 0.8667 - val_loss: 0.4786 - val_acc: 0.8724\n", "Epoch 3/5\n", "18000/18000 [==============================] - 2s 84us/step - loss: 0.4141 - acc: 0.8867 - val_loss: 0.4146 - val_acc: 0.8840\n", "Epoch 4/5\n", "18000/18000 [==============================] - 2s 87us/step - loss: 0.3684 - acc: 0.8959 - val_loss: 0.3815 - val_acc: 0.8926\n", "Epoch 5/5\n", "18000/18000 [==============================] - 2s 87us/step - loss: 0.3406 - acc: 0.9048 - val_loss: 0.3568 - val_acc: 0.8997\n", "10000/10000 [==============================] - 0s 22us/step\n", "\n", "loss_and_metrics : [0.33103723052740097, 0.90649999999999997]\n" ] } ], "source": [ "# 0. 사용할 패키지 불러오기\n", "from keras.utils import np_utils\n", "from keras.datasets import mnist\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Activation\n", "import numpy as np\n", "from numpy import argmax\n", "\n", "# 1. 데이터셋 생성하기\n", "\n", "# 훈련셋과 시험셋 불러오기\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "# 데이터셋 전처리\n", "x_train = x_train.reshape(60000, 784).astype('float32') / 255.0\n", "x_test = x_test.reshape(10000, 784).astype('float32') / 255.0\n", "\n", "# 원핫인코딩 (one-hot encoding) 처리\n", "y_train = np_utils.to_categorical(y_train)\n", "y_test = np_utils.to_categorical(y_test)\n", "\n", "# 훈련셋과 검증셋 분리\n", "x_val = x_train[:42000] # 데이터셋의 70%를 훈련셋/학습셋으로 사용\n", "x_train = x_train[42000:] # 데이터셋의 30%를 검증셋으로 사용\n", "y_val = y_train[:42000] # 데이터셋의 70%를 훈련셋/학습셋으로 사용\n", "y_train = y_train[42000:] # 데이터셋의 30%를 검증셋으로 사용\n", "\n", "# 2. 모델 구성하기\n", "model = Sequential()\n", "model.add(Dense(units=64, input_dim=28*28, activation='relu'))\n", "model.add(Dense(units=10, activation='softmax'))\n", "\n", "# 3. 모델 학습과정 설정하기\n", "model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n", "\n", "# 4. 모델 학습시키기\n", "model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_val, y_val))\n", "\n", "# 5. 모델 평가하기\n", "loss_and_metrics = model.evaluate(x_test, y_test, batch_size=32)\n", "print('')\n", "print('loss_and_metrics : ' + str(loss_and_metrics))\n", "\n", "# 6. 모델 저장하기\n", "from keras.models import load_model\n", "model.save('mnist_mlp_model.h5')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "G\n", "\n", "\n", "2084647797928\n", "\n", "dense_1_input: InputLayer\n", "\n", "input:\n", "\n", "output:\n", "\n", "(None, 784)\n", "\n", "(None, 784)\n", "\n", "\n", "2084642937936\n", "\n", "dense_1: Dense\n", "\n", "input:\n", "\n", "output:\n", "\n", "(None, 784)\n", "\n", "(None, 64)\n", "\n", "\n", "2084647797928->2084642937936\n", "\n", "\n", "\n", "\n", "2084578302664\n", "\n", "dense_2: Dense\n", "\n", "input:\n", "\n", "output:\n", "\n", "(None, 64)\n", "\n", "(None, 10)\n", "\n", "\n", "2084642937936->2084578302664\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import SVG\n", "from keras.utils.vis_utils import model_to_dot\n", "\n", "%matplotlib inline\n", "\n", "SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train on 18000 samples, validate on 42000 samples\n", "Epoch 1/5\n", "18000/18000 [==============================] - 2s 103us/step - loss: 0.2618 - acc: 0.9269 - val_loss: 0.2936 - val_acc: 0.9162\n", "Epoch 2/5\n", "18000/18000 [==============================] - 2s 87us/step - loss: 0.2534 - acc: 0.9293 - val_loss: 0.2891 - val_acc: 0.9177\n", "Epoch 3/5\n", "18000/18000 [==============================] - 2s 88us/step - loss: 0.2460 - acc: 0.9312 - val_loss: 0.2817 - val_acc: 0.9201\n", "Epoch 4/5\n", "18000/18000 [==============================] - 2s 93us/step - loss: 0.2393 - acc: 0.9332 - val_loss: 0.2762 - val_acc: 0.9211\n", "Epoch 5/5\n", "18000/18000 [==============================] - 2s 86us/step - loss: 0.2327 - acc: 0.9354 - val_loss: 0.2700 - val_acc: 0.9231\n", "10000/10000 [==============================] - 0s 22us/step\n", "\n", "loss_and_metrics : [0.25109204970002175, 0.92830000000000001]\n" ] } ], "source": [ "# 2. 모델 불러오기\n", "from keras.models import load_model\n", "model = load_model('mnist_mlp_model.h5')\n", "\n", "# 3. 모델 학습과정 설정하기\n", "model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n", "\n", "# 4. 모델 학습시키기\n", "model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_val, y_val))\n", "\n", "# 5. 모델 평가하기\n", "loss_and_metrics = model.evaluate(x_test, y_test, batch_size=32)\n", "print('')\n", "print('loss_and_metrics : ' + str(loss_and_metrics))\n", "\n", "# 6. 모델 저장하기\n", "from keras.models import load_model\n", "model.save('mnist_mlp_model.h5')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.4" } }, "nbformat": 4, "nbformat_minor": 2 }