From 59b05dd0d34d0c1f81f97616038872cc8f85a8b2 Mon Sep 17 00:00:00 2001 From: Haesun Park Date: Fri, 20 Apr 2018 14:59:43 +0900 Subject: [PATCH] =?UTF-8?q?14=EC=9E=A5=20=EC=8B=A4=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 14_recurrent_neural_networks.ipynb | 1784 ++++++++++++++-------------- 1 file changed, 884 insertions(+), 900 deletions(-) diff --git a/14_recurrent_neural_networks.ipynb b/14_recurrent_neural_networks.ipynb index 94212be..0c88d05 100644 --- a/14_recurrent_neural_networks.ipynb +++ b/14_recurrent_neural_networks.ipynb @@ -1,53 +1,78 @@ { "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Chapter 14 – Recurrent Neural Networks**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "_This notebook contains all the sample code and solutions to the exercises in chapter 14._" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Setup" - ] - }, - { - "cell_type": "markdown", - "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": "code", "execution_count": 1, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPython 3.5.5\n", + "IPython 6.3.0\n", + "\n", + "numpy 1.14.2\n", + "sklearn 0.19.1\n", + "scipy 1.0.1\n", + "matplotlib 2.2.2\n", + "tensorflow 1.7.0\n" + ] + } + ], + "source": [ + "%load_ext watermark\n", + "%watermark -v -p numpy,sklearn,scipy,matplotlib,tensorflow" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**14장 – 순환 신경망**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "_이 노트북은 14장에 있는 모든 샘플 코드와 연습문제 해답을 가지고 있습니다._" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 설정" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "파이썬 2와 3을 모두 지원합니다. 공통 모듈을 임포트하고 맷플롯립 그림이 노트북 안에 포함되도록 설정하고 생성한 그림을 저장하기 위한 함수를 준비합니다:" + ] + }, + { + "cell_type": "code", + "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", @@ -55,13 +80,16 @@ "plt.rcParams['xtick.labelsize'] = 12\n", "plt.rcParams['ytick.labelsize'] = 12\n", "\n", - "# Where to save the figures\n", + "# 한글출력\n", + "plt.rcParams['font.family'] = 'NanumBarunGothic'\n", + "plt.rcParams['axes.unicode_minus'] = False\n", + "\n", + "# 그림을 저장할 폴더\n", "PROJECT_ROOT_DIR = \".\"\n", "CHAPTER_ID = \"rnn\"\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)" @@ -71,12 +99,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Then of course we will need TensorFlow:" + "텐서플로를 임포트합니다:" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -87,19 +115,19 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Basic RNNs" + "# 기본 RNN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Manual RNN" + "## 수동으로 RNN 만들기" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -123,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -139,17 +167,17 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[-0.0664006 0.96257669 0.68105793 0.70918542 -0.89821601]\n", - " [ 0.9977755 -0.71978903 -0.99657607 0.96739239 -0.99989718]\n", - " [ 0.99999774 -0.99898803 -0.99999893 0.99677622 -0.99999988]\n", - " [ 1. -1. -1. -0.99818915 0.99950868]]\n" + "[[-0.06640061 0.9625767 0.6810579 0.7091854 -0.89821595]\n", + " [ 0.99777555 -0.7197888 -0.99657613 0.96739244 -0.99989706]\n", + " [ 0.99999785 -0.9989881 -0.99999887 0.9967763 -0.9999999 ]\n", + " [ 1. -1. -1. -0.9981892 0.9995087 ]]\n" ] } ], @@ -159,17 +187,17 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[ 1. -1. -1. 0.40200251 -0.99999982]\n", - " [-0.12210421 0.62805271 0.96718431 -0.99371219 -0.25839362]\n", - " [ 0.99999827 -0.9999994 -0.9999975 -0.85943311 -0.99998808]\n", - " [ 0.99928284 -0.99999815 -0.99990582 0.98579627 -0.92205757]]\n" + "[[ 1. -1. -1. 0.40200272 -0.99999994]\n", + " [-0.12210429 0.62805295 0.96718436 -0.9937122 -0.2583933 ]\n", + " [ 0.99999815 -0.9999994 -0.99999744 -0.8594331 -0.99998796]\n", + " [ 0.99928296 -0.9999981 -0.9999059 0.98579615 -0.9220575 ]]\n" ] } ], @@ -181,12 +209,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Using `static_rnn()`" + "## `static_rnn()`을 사용하여 만들기" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -196,9 +224,19 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:From /home/haesun/anaconda3/envs/handson-ml/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use the retry module or similar alternatives.\n" + ] + } + ], "source": [ "reset_graph()\n", "\n", @@ -213,7 +251,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -222,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -236,19 +274,20 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 0.30741334, -0.32884315, -0.65428472, -0.93850589, 0.52089024],\n", - " [ 0.99122757, -0.95425421, -0.75180793, -0.99952078, 0.98202348],\n", - " [ 0.99992681, -0.99783254, -0.82473528, -0.9999963 , 0.99947774],\n", - " [ 0.99677098, -0.68750614, 0.84199691, 0.93039107, 0.8120684 ]], dtype=float32)" + "array([[ 0.30741337, -0.32884312, -0.6542847 , -0.9385059 , 0.52089024],\n", + " [ 0.9912275 , -0.95425415, -0.7518078 , -0.9995208 , 0.98202336],\n", + " [ 0.99992675, -0.99783254, -0.82473516, -0.99999636, 0.99947786],\n", + " [ 0.9967709 , -0.6875061 , 0.8419969 , 0.93039113, 0.81206834]],\n", + " dtype=float32)" ] }, - "execution_count": 11, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -259,19 +298,20 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 0.99998885, -0.99976051, -0.06679298, -0.99998039, 0.99982214],\n", - " [-0.65249437, -0.51520866, -0.37968954, -0.59225935, -0.08968385],\n", - " [ 0.99862403, -0.99715197, -0.03308626, -0.99915648, 0.99329019],\n", - " [ 0.99681675, -0.95981938, 0.39660636, -0.83076048, 0.79671967]], dtype=float32)" + "array([[ 0.99998885, -0.9997606 , -0.06679297, -0.9999803 , 0.99982214],\n", + " [-0.65249425, -0.5152086 , -0.37968948, -0.5922594 , -0.08968376],\n", + " [ 0.998624 , -0.99715203, -0.03308632, -0.9991566 , 0.9932902 ],\n", + " [ 0.99681675, -0.9598194 , 0.39660627, -0.8307605 , 0.7967197 ]],\n", + " dtype=float32)" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -282,7 +322,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -326,7 +366,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -336,7 +376,7 @@ "