diff --git a/09_up_and_running_with_tensorflow.ipynb b/09_up_and_running_with_tensorflow.ipynb index 9ecf9df..bd230c5 100644 --- a/09_up_and_running_with_tensorflow.ipynb +++ b/09_up_and_running_with_tensorflow.ipynb @@ -1,69 +1,79 @@ { "cells": [ { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "cell_type": "code", + "execution_count": 140, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The watermark extension is already loaded. To reload it, use:\n", + " %reload_ext watermark\n", + "CPython 3.5.5\n", + "IPython 6.3.0\n", + "\n", + "numpy 1.14.2\n", + "scipy 1.0.1\n", + "matplotlib 2.2.2\n", + "tensorflow 1.7.0\n" + ] + } + ], "source": [ - "**Chapter 9 – Up and running with TensorFlow**" + "%load_ext watermark\n", + "%watermark -v -p numpy,scipy,matplotlib,tensorflow" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "_This notebook contains all the sample code and solutions to the exercises in chapter 9._" + "**9장 – 텐서플로 시작하기**" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Setup" + "_이 노트북은 9장에 있는 모든 샘플 코드와 연습문제 해답을 가지고 있습니다._" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "First, let's make sure this notebook works well in both python 2 and 3, import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures:" + "# 설정" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "파이썬 2와 3을 모두 지원합니다. 공통 모듈을 임포트하고 맷플롯립 그림이 노트북 안에 포함되도록 설정하고 생성한 그림을 저장하기 위한 함수를 준비합니다:" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ - "# To support both python 2 and python 3\n", + "# 파이썬 2와 파이썬 3 지원\n", "from __future__ import division, print_function, unicode_literals\n", "\n", - "# Common imports\n", + "# 공통\n", "import numpy as np\n", "import os\n", "\n", - "# to make this notebook's output stable across runs\n", + "# 일관된 출력을 위해 유사난수 초기화\n", "def reset_graph(seed=42):\n", " tf.reset_default_graph()\n", " tf.set_random_seed(seed)\n", " np.random.seed(seed)\n", "\n", - "# To plot pretty figures\n", + "# 맷플롯립 설정\n", "%matplotlib inline\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", @@ -71,13 +81,12 @@ "plt.rcParams['xtick.labelsize'] = 12\n", "plt.rcParams['ytick.labelsize'] = 12\n", "\n", - "# Where to save the figures\n", + "# 한글출력\n", "PROJECT_ROOT_DIR = \".\"\n", "CHAPTER_ID = \"tensorflow\"\n", "\n", "def save_fig(fig_id, tight_layout=True):\n", " path = os.path.join(PROJECT_ROOT_DIR, \"images\", CHAPTER_ID, fig_id + \".png\")\n", - " print(\"Saving figure\", fig_id)\n", " if tight_layout:\n", " plt.tight_layout()\n", " plt.savefig(path, format='png', dpi=300)" @@ -85,22 +94,15 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Creating and running a graph" + "# 계산 그래프 만들고 실행하기" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", @@ -114,12 +116,8 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { @@ -127,7 +125,7 @@ "" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -138,12 +136,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -163,12 +157,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [], "source": [ "sess.close()" @@ -176,12 +166,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "with tf.Session() as sess:\n", @@ -192,12 +178,8 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -205,7 +187,7 @@ "42" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -216,12 +198,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 9, + "metadata": {}, "outputs": [], "source": [ "init = tf.global_variables_initializer()\n", @@ -233,12 +211,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "data": { @@ -246,7 +220,7 @@ "42" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -257,12 +231,8 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 11, + "metadata": {}, "outputs": [], "source": [ "init = tf.global_variables_initializer()" @@ -270,12 +240,8 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -294,12 +260,8 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ "sess.close()" @@ -307,12 +269,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -320,7 +278,7 @@ "42" ] }, - "execution_count": 13, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -331,22 +289,15 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Managing graphs" + "# 그래프 다루기" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { "data": { @@ -354,7 +305,7 @@ "True" ] }, - "execution_count": 14, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -368,12 +319,8 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "data": { @@ -381,7 +328,7 @@ "True" ] }, - "execution_count": 15, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -396,11 +343,8 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "scrolled": true }, "outputs": [ @@ -410,7 +354,7 @@ "False" ] }, - "execution_count": 16, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -421,12 +365,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -450,12 +390,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -475,32 +411,22 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Linear Regression" + "# 선형 회귀" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "## Using the Normal Equation" + "## 정규방정식을 사용해서" ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 20, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -523,28 +449,24 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 21, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ -3.74651413e+01],\n", - " [ 4.35734153e-01],\n", - " [ 9.33829229e-03],\n", - " [ -1.06622010e-01],\n", - " [ 6.44106984e-01],\n", - " [ -4.25131839e-06],\n", - " [ -3.77322501e-03],\n", - " [ -4.26648885e-01],\n", - " [ -4.40514028e-01]], dtype=float32)" + "array([[-3.7185181e+01],\n", + " [ 4.3633747e-01],\n", + " [ 9.3952334e-03],\n", + " [-1.0711310e-01],\n", + " [ 6.4479220e-01],\n", + " [-4.0338000e-06],\n", + " [-3.7813708e-03],\n", + " [-4.2348403e-01],\n", + " [-4.3721911e-01]], dtype=float32)" ] }, - "execution_count": 20, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -555,36 +477,29 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "Compare with pure NumPy" + "넘파이와 비교" ] }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 22, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[ -3.69419202e+01]\n", - " [ 4.36693293e-01]\n", - " [ 9.43577803e-03]\n", - " [ -1.07322041e-01]\n", - " [ 6.45065694e-01]\n", - " [ -3.97638942e-06]\n", - " [ -3.78654265e-03]\n", - " [ -4.21314378e-01]\n", - " [ -4.34513755e-01]]\n" + "[[-3.69419202e+01]\n", + " [ 4.36693293e-01]\n", + " [ 9.43577803e-03]\n", + " [-1.07322041e-01]\n", + " [ 6.45065694e-01]\n", + " [-3.97638942e-06]\n", + " [-3.78654265e-03]\n", + " [-4.21314378e-01]\n", + " [-4.34513755e-01]]\n" ] } ], @@ -598,36 +513,29 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "Compare with Scikit-Learn" + "사이킷런과 비교" ] }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 23, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[ -3.69419202e+01]\n", - " [ 4.36693293e-01]\n", - " [ 9.43577803e-03]\n", - " [ -1.07322041e-01]\n", - " [ 6.45065694e-01]\n", - " [ -3.97638942e-06]\n", - " [ -3.78654265e-03]\n", - " [ -4.21314378e-01]\n", - " [ -4.34513755e-01]]\n" + "[[-3.69419202e+01]\n", + " [ 4.36693293e-01]\n", + " [ 9.43577803e-03]\n", + " [-1.07322041e-01]\n", + " [ 6.45065694e-01]\n", + " [-3.97638942e-06]\n", + " [-3.78654265e-03]\n", + " [-4.21314378e-01]\n", + " [-4.34513755e-01]]\n" ] } ], @@ -641,32 +549,22 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "## Using Batch Gradient Descent" + "## 배치 경사 하강법을 사용해서" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "Gradient Descent requires scaling the feature vectors first. We could do this using TF, but let's just use Scikit-Learn for now." + "경사 하강법은 먼저 특성 벡터의 스케일을 조정해야 합니다. 텐서플로를 사용해 할 수 있지만 그냥 여기서는 사이킷런을 사용하겠습니다." ] }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 24, + "metadata": {}, "outputs": [], "source": [ "from sklearn.preprocessing import StandardScaler\n", @@ -677,23 +575,19 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 25, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[ 1.00000000e+00 6.60969987e-17 5.50808322e-18 6.60969987e-17\n", - " -1.06030602e-16 -1.10161664e-17 3.44255201e-18 -1.07958431e-15\n", - " -8.52651283e-15]\n", - "[ 0.38915536 0.36424355 0.5116157 ..., -0.06612179 -0.06360587\n", + "[ 1.00000000e+00 6.60969987e-17 5.50808322e-18 6.60969987e-17\n", + " -1.06030602e-16 -1.10161664e-17 3.44255201e-18 -1.07958431e-15\n", + " -8.52651283e-15]\n", + "[ 0.38915536 0.36424355 0.5116157 ... -0.06612179 -0.06360587\n", " 0.01359031]\n", - "0.111111111111\n", + "0.11111111111111005\n", "(20640, 9)\n" ] } @@ -707,37 +601,30 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "### Manually computing the gradients" + "### 수동으로 그래디언트 계산하기" ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 26, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Epoch 0 MSE = 9.16154\n", - "Epoch 100 MSE = 0.714501\n", - "Epoch 200 MSE = 0.566705\n", - "Epoch 300 MSE = 0.555572\n", - "Epoch 400 MSE = 0.548812\n", - "Epoch 500 MSE = 0.543636\n", - "Epoch 600 MSE = 0.539629\n", - "Epoch 700 MSE = 0.536509\n", - "Epoch 800 MSE = 0.534068\n", - "Epoch 900 MSE = 0.532147\n" + "에포크 0 MSE = 9.161542\n", + "에포크 100 MSE = 0.7145004\n", + "에포크 200 MSE = 0.56670487\n", + "에포크 300 MSE = 0.5555718\n", + "에포크 400 MSE = 0.5488112\n", + "에포크 500 MSE = 0.5436363\n", + "에포크 600 MSE = 0.5396291\n", + "에포크 700 MSE = 0.5365092\n", + "에포크 800 MSE = 0.53406775\n", + "에포크 900 MSE = 0.5321473\n" ] } ], @@ -763,7 +650,7 @@ "\n", " for epoch in range(n_epochs):\n", " if epoch % 100 == 0:\n", - " print(\"Epoch\", epoch, \"MSE =\", mse.eval())\n", + " print(\"에포크\", epoch, \"MSE =\", mse.eval())\n", " sess.run(training_op)\n", " \n", " best_theta = theta.eval()" @@ -771,28 +658,24 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 27, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 2.06855249],\n", - " [ 0.88740271],\n", - " [ 0.14401658],\n", + "array([[ 2.0685523 ],\n", + " [ 0.8874027 ],\n", + " [ 0.14401656],\n", " [-0.34770882],\n", " [ 0.36178368],\n", - " [ 0.00393812],\n", - " [-0.04269557],\n", - " [-0.66145277],\n", - " [-0.63752776]], dtype=float32)" + " [ 0.00393811],\n", + " [-0.04269556],\n", + " [-0.6614529 ],\n", + " [-0.6375279 ]], dtype=float32)" ] }, - "execution_count": 26, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -803,32 +686,22 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "### Using autodiff" + "### 자동미분 사용하기" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "Same as above except for the `gradients = ...` line:" + "`gradients = ...` 라인만 빼고 위와 동일합니다:" ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 28, + "metadata": {}, "outputs": [], "source": [ "reset_graph()\n", @@ -846,12 +719,8 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 29, + "metadata": {}, "outputs": [], "source": [ "gradients = tf.gradients(mse, [theta])[0]" @@ -859,37 +728,33 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 30, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Epoch 0 MSE = 9.16154\n", - "Epoch 100 MSE = 0.714501\n", - "Epoch 200 MSE = 0.566705\n", - "Epoch 300 MSE = 0.555572\n", - "Epoch 400 MSE = 0.548812\n", - "Epoch 500 MSE = 0.543636\n", - "Epoch 600 MSE = 0.539629\n", - "Epoch 700 MSE = 0.536509\n", - "Epoch 800 MSE = 0.534068\n", - "Epoch 900 MSE = 0.532147\n", - "Best theta:\n", - "[[ 2.06855249]\n", - " [ 0.88740271]\n", - " [ 0.14401658]\n", - " [-0.34770882]\n", - " [ 0.36178368]\n", + "에포크 0 MSE = 9.161542\n", + "에포크 100 MSE = 0.71450037\n", + "에포크 200 MSE = 0.56670487\n", + "에포크 300 MSE = 0.5555718\n", + "에포크 400 MSE = 0.54881126\n", + "에포크 500 MSE = 0.5436363\n", + "에포크 600 MSE = 0.53962916\n", + "에포크 700 MSE = 0.5365092\n", + "에포크 800 MSE = 0.53406775\n", + "에포크 900 MSE = 0.5321473\n", + "best_theta:\n", + "[[ 2.0685523 ]\n", + " [ 0.8874027 ]\n", + " [ 0.14401656]\n", + " [-0.3477088 ]\n", + " [ 0.36178365]\n", " [ 0.00393811]\n", " [-0.04269556]\n", - " [-0.66145277]\n", - " [-0.6375277 ]]\n" + " [-0.66145283]\n", + " [-0.6375278 ]]\n" ] } ], @@ -903,33 +768,26 @@ "\n", " for epoch in range(n_epochs):\n", " if epoch % 100 == 0:\n", - " print(\"Epoch\", epoch, \"MSE =\", mse.eval())\n", + " print(\"에포크\", epoch, \"MSE =\", mse.eval())\n", " sess.run(training_op)\n", " \n", " best_theta = theta.eval()\n", "\n", - "print(\"Best theta:\")\n", + "print(\"best_theta:\")\n", "print(best_theta)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "How could you find the partial derivatives of the following function with regards to `a` and `b`?" + "`a`와 `b`에 대한 다음 함수의 편도함수를 어떻게 구할 수 있나요?" ] }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 31, + "metadata": {}, "outputs": [], "source": [ "def my_func(a, b):\n", @@ -941,12 +799,8 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 32, + "metadata": {}, "outputs": [ { "data": { @@ -954,7 +808,7 @@ "-0.21253923284754914" ] }, - "execution_count": 31, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -965,12 +819,8 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 33, + "metadata": {}, "outputs": [], "source": [ "reset_graph()\n", @@ -987,29 +837,22 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "Let's compute the function at $a=0.2$ and $b=0.3$, and the partial derivatives at that point with regards to $a$ and with regards to $b$:" + "$a=0.2$와 $b=0.3$일 때 함수 값을 계산하고 그 다음 $a$와 $b$에 대한 편미분을 구합니다:" ] }, { "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 34, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "-0.212537\n", - "[-1.1388494, 0.19671395]\n" + "-0.21253741\n", + "[-1.1388495, 0.19671397]\n" ] } ], @@ -1022,22 +865,15 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "### Using a `GradientDescentOptimizer`" + "### `GradientDescentOptimizer` 사용하기" ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 35, + "metadata": {}, "outputs": [], "source": [ "reset_graph()\n", @@ -1055,12 +891,8 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 36, + "metadata": {}, "outputs": [], "source": [ "optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n", @@ -1069,37 +901,33 @@ }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Epoch 0 MSE = 9.16154\n", - "Epoch 100 MSE = 0.714501\n", - "Epoch 200 MSE = 0.566705\n", - "Epoch 300 MSE = 0.555572\n", - "Epoch 400 MSE = 0.548812\n", - "Epoch 500 MSE = 0.543636\n", - "Epoch 600 MSE = 0.539629\n", - "Epoch 700 MSE = 0.536509\n", - "Epoch 800 MSE = 0.534068\n", - "Epoch 900 MSE = 0.532147\n", - "Best theta:\n", - "[[ 2.06855249]\n", - " [ 0.88740271]\n", - " [ 0.14401658]\n", - " [-0.34770882]\n", - " [ 0.36178368]\n", + "에포크 0 MSE = 9.161542\n", + "에포크 100 MSE = 0.7145004\n", + "에포크 200 MSE = 0.56670487\n", + "에포크 300 MSE = 0.5555718\n", + "에포크 400 MSE = 0.54881126\n", + "에포크 500 MSE = 0.5436363\n", + "에포크 600 MSE = 0.53962916\n", + "에포크 700 MSE = 0.5365092\n", + "에포크 800 MSE = 0.53406775\n", + "에포크 900 MSE = 0.5321473\n", + "best_theta:\n", + "[[ 2.0685523 ]\n", + " [ 0.8874027 ]\n", + " [ 0.14401656]\n", + " [-0.3477088 ]\n", + " [ 0.36178365]\n", " [ 0.00393811]\n", " [-0.04269556]\n", - " [-0.66145277]\n", - " [-0.6375277 ]]\n" + " [-0.66145283]\n", + " [-0.6375278 ]]\n" ] } ], @@ -1111,33 +939,26 @@ "\n", " for epoch in range(n_epochs):\n", " if epoch % 100 == 0:\n", - " print(\"Epoch\", epoch, \"MSE =\", mse.eval())\n", + " print(\"에포크\", epoch, \"MSE =\", mse.eval())\n", " sess.run(training_op)\n", " \n", " best_theta = theta.eval()\n", "\n", - "print(\"Best theta:\")\n", + "print(\"best_theta:\")\n", "print(best_theta)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "### Using a momentum optimizer" + "### 모멘텀 옵티마이저 사용하기" ] }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 38, + "metadata": {}, "outputs": [], "source": [ "reset_graph()\n", @@ -1155,12 +976,8 @@ }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 39, + "metadata": {}, "outputs": [], "source": [ "optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,\n", @@ -1169,12 +986,8 @@ }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 40, + "metadata": {}, "outputs": [], "source": [ "training_op = optimizer.minimize(mse)\n", @@ -1184,27 +997,23 @@ }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 41, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Best theta:\n", - "[[ 2.06855798]\n", - " [ 0.82962859]\n", - " [ 0.11875337]\n", + "best_theta:\n", + "[[ 2.068558 ]\n", + " [ 0.82962847]\n", + " [ 0.11875335]\n", " [-0.26554456]\n", - " [ 0.30571091]\n", - " [-0.00450251]\n", + " [ 0.3057109 ]\n", + " [-0.00450249]\n", " [-0.03932662]\n", - " [-0.89986444]\n", - " [-0.87052065]]\n" + " [-0.8998645 ]\n", + " [-0.8705207 ]]\n" ] } ], @@ -1217,44 +1026,34 @@ " \n", " best_theta = theta.eval()\n", "\n", - "print(\"Best theta:\")\n", + "print(\"best_theta:\")\n", "print(best_theta)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Feeding data to the training algorithm" + "# 훈련 알고리즘에 데이터 주입하기" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "## Placeholder nodes" + "## 플레이스홀더 노드" ] }, { "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 42, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[ 6. 7. 8.]]\n" + "[[6. 7. 8.]]\n" ] } ], @@ -1272,19 +1071,15 @@ }, { "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[ 9. 10. 11.]\n", - " [ 12. 13. 14.]]\n" + "[[ 9. 10. 11.]\n", + " [12. 13. 14.]]\n" ] } ], @@ -1294,22 +1089,15 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "## Mini-batch Gradient Descent" + "## 미니배치 경사 하강법" ] }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 44, + "metadata": {}, "outputs": [], "source": [ "n_epochs = 1000\n", @@ -1318,12 +1106,8 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 45, + "metadata": {}, "outputs": [], "source": [ "reset_graph()\n", @@ -1334,12 +1118,8 @@ }, { "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 46, + "metadata": {}, "outputs": [], "source": [ "theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name=\"theta\")\n", @@ -1354,12 +1134,8 @@ }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 47, + "metadata": {}, "outputs": [], "source": [ "n_epochs = 10" @@ -1367,12 +1143,8 @@ }, { "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 48, + "metadata": {}, "outputs": [], "source": [ "batch_size = 100\n", @@ -1381,12 +1153,8 @@ }, { "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 49, + "metadata": {}, "outputs": [], "source": [ "def fetch_batch(epoch, batch_index, batch_size):\n", @@ -1409,28 +1177,24 @@ }, { "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 50, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 2.07033372],\n", - " [ 0.86371452],\n", - " [ 0.12255151],\n", - " [-0.31211874],\n", - " [ 0.38510373],\n", + "array([[ 2.0703337 ],\n", + " [ 0.8637145 ],\n", + " [ 0.12255152],\n", + " [-0.31211877],\n", + " [ 0.38510376],\n", " [ 0.00434168],\n", - " [-0.01232954],\n", + " [-0.0123295 ],\n", " [-0.83376896],\n", - " [-0.80304712]], dtype=float32)" + " [-0.8030471 ]], dtype=float32)" ] }, - "execution_count": 49, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -1441,54 +1205,47 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Saving and restoring a model" + "# 모델의 저장과 복원" ] }, { "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Epoch 0 MSE = 9.16154\n", - "Epoch 100 MSE = 0.714501\n", - "Epoch 200 MSE = 0.566705\n", - "Epoch 300 MSE = 0.555572\n", - "Epoch 400 MSE = 0.548812\n", - "Epoch 500 MSE = 0.543636\n", - "Epoch 600 MSE = 0.539629\n", - "Epoch 700 MSE = 0.536509\n", - "Epoch 800 MSE = 0.534068\n", - "Epoch 900 MSE = 0.532147\n" + "에포크 0 MSE = 9.161542\n", + "에포크 100 MSE = 0.7145004\n", + "에포크 200 MSE = 0.56670487\n", + "에포크 300 MSE = 0.5555718\n", + "에포크 400 MSE = 0.54881126\n", + "에포크 500 MSE = 0.5436363\n", + "에포크 600 MSE = 0.53962916\n", + "에포크 700 MSE = 0.5365092\n", + "에포크 800 MSE = 0.53406775\n", + "에포크 900 MSE = 0.5321473\n" ] } ], "source": [ "reset_graph()\n", "\n", - "n_epochs = 1000 # not shown in the book\n", - "learning_rate = 0.01 # not shown\n", + "n_epochs = 1000 # 책에는 없습니다.\n", + "learning_rate = 0.01 # 책에는 없습니다.\n", "\n", - "X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name=\"X\") # not shown\n", - "y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name=\"y\") # not shown\n", + "X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name=\"X\") # 책에는 없습니다.\n", + "y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name=\"y\") # 책에는 없습니다.\n", "theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name=\"theta\")\n", - "y_pred = tf.matmul(X, theta, name=\"predictions\") # not shown\n", - "error = y_pred - y # not shown\n", - "mse = tf.reduce_mean(tf.square(error), name=\"mse\") # not shown\n", - "optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) # not shown\n", - "training_op = optimizer.minimize(mse) # not shown\n", + "y_pred = tf.matmul(X, theta, name=\"predictions\") # 책에는 없습니다.\n", + "error = y_pred - y # 책에는 없습니다.\n", + "mse = tf.reduce_mean(tf.square(error), name=\"mse\") # 책에는 없습니다.\n", + "optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) # 책에는 없습니다.\n", + "training_op = optimizer.minimize(mse) # 책에는 없습니다.\n", "\n", "init = tf.global_variables_initializer()\n", "saver = tf.train.Saver()\n", @@ -1498,7 +1255,7 @@ "\n", " for epoch in range(n_epochs):\n", " if epoch % 100 == 0:\n", - " print(\"Epoch\", epoch, \"MSE =\", mse.eval()) # not shown\n", + " print(\"에포크\", epoch, \"MSE =\", mse.eval()) # 책에는 없습니다.\n", " save_path = saver.save(sess, \"/tmp/my_model.ckpt\")\n", " sess.run(training_op)\n", " \n", @@ -1508,28 +1265,24 @@ }, { "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 2.06855249],\n", - " [ 0.88740271],\n", - " [ 0.14401658],\n", - " [-0.34770882],\n", - " [ 0.36178368],\n", + "array([[ 2.0685523 ],\n", + " [ 0.8874027 ],\n", + " [ 0.14401656],\n", + " [-0.3477088 ],\n", + " [ 0.36178365],\n", " [ 0.00393811],\n", " [-0.04269556],\n", - " [-0.66145277],\n", - " [-0.6375277 ]], dtype=float32)" + " [-0.66145283],\n", + " [-0.6375278 ]], dtype=float32)" ] }, - "execution_count": 51, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -1540,12 +1293,8 @@ }, { "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 53, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1558,17 +1307,13 @@ "source": [ "with tf.Session() as sess:\n", " saver.restore(sess, \"/tmp/my_model_final.ckpt\")\n", - " best_theta_restored = theta.eval() # not shown in the book" + " best_theta_restored = theta.eval() # 책에는 없습니다." ] }, { "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { "data": { @@ -1576,7 +1321,7 @@ "True" ] }, - "execution_count": 53, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -1587,22 +1332,15 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "If you want to have a saver that loads and restores `theta` with a different name, such as `\"weights\"`:" + "`theta`를 `\"weights\"`와 같은 다른 이름으로 저장하고 복원하는 Saver 객체를 원할 경우엔:" ] }, { "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 55, + "metadata": {}, "outputs": [], "source": [ "saver = tf.train.Saver({\"weights\": theta})" @@ -1610,22 +1348,15 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "By default the saver also saves the graph structure itself in a second file with the extension `.meta`. You can use the function `tf.train.import_meta_graph()` to restore the graph structure. This function loads the graph into the default graph and returns a `Saver` that can then be used to restore the graph state (i.e., the variable values):" + "기본적으로 Saver 객체는 `.meta` 확장자를 가진 두 번째 파일에 그래프 구조도 저장합니다. `tf.train.import_meta_graph()` 함수를 사용하여 그래프 구조를 복원할 수 있습니다. 이 함수는 저장된 그래프를 기본 그래프로 로드하고 상태(즉, 변수 값)를 복원할 수 있는 `Saver` 객체를 반환합니다:" ] }, { "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 56, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1637,24 +1368,20 @@ ], "source": [ "reset_graph()\n", - "# notice that we start with an empty graph.\n", + "# 빈 그래프로 시작합니다\n", "\n", - "saver = tf.train.import_meta_graph(\"/tmp/my_model_final.ckpt.meta\") # this loads the graph structure\n", - "theta = tf.get_default_graph().get_tensor_by_name(\"theta:0\") # not shown in the book\n", + "saver = tf.train.import_meta_graph(\"/tmp/my_model_final.ckpt.meta\") # 그래프 구조를 로드합니다.\n", + "theta = tf.get_default_graph().get_tensor_by_name(\"theta:0\") # 책에는 없습니다.\n", "\n", "with tf.Session() as sess:\n", - " saver.restore(sess, \"/tmp/my_model_final.ckpt\") # this restores the graph's state\n", - " best_theta_restored = theta.eval() # not shown in the book" + " saver.restore(sess, \"/tmp/my_model_final.ckpt\") # 그래프 상태를 로드합니다.\n", + " best_theta_restored = theta.eval() # 책에는 없습니다." ] }, { "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { "data": { @@ -1662,7 +1389,7 @@ "True" ] }, - "execution_count": 56, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -1673,39 +1400,29 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "This means that you can import a pretrained model without having to have the corresponding Python code to build the graph. This is very handy when you keep tweaking and saving your model: you can load a previously saved model without having to search for the version of the code that built it." + "이를 사용하면 그래프를 만든 파이썬 코드가 없이도 미리 훈련된 모델을 임포트할 수 있습니다. 모델을 저장하고 변경할 때도 매우 편리합니다. 이전에 저장된 모델을 구축한 코드의 버전을 찾지 않아도 로드할 수 있습니다." ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "# Visualizing the graph\n", - "## inside Jupyter" + "# 그래프 시각화\n", + "## 쥬피터에서" ] }, { "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": true, - "deletable": true, - "editable": true - }, + "execution_count": 58, + "metadata": {}, "outputs": [], "source": [ "from IPython.display import clear_output, Image, display, HTML\n", "\n", "def strip_consts(graph_def, max_const_size=32):\n", - " \"\"\"Strip large constant values from graph_def.\"\"\"\n", + " \"\"\"graph_def에서 큰 상수 값은 제외시킵니다.\"\"\"\n", " strip_def = tf.GraphDef()\n", " for n0 in graph_def.node:\n", " n = strip_def.node.add() \n", @@ -1718,7 +1435,7 @@ " return strip_def\n", "\n", "def show_graph(graph_def, max_const_size=32):\n", - " \"\"\"Visualize TensorFlow graph.\"\"\"\n", + " \"\"\"텐서플로 그래프 나타내기\"\"\"\n", " if hasattr(graph_def, 'as_graph_def'):\n", " graph_def = graph_def.as_graph_def()\n", " strip_def = strip_consts(graph_def, max_const_size=max_const_size)\n", @@ -1742,13 +1459,8 @@ }, { "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, - "scrolled": true - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { "data": { @@ -1757,7 +1469,7 @@ "