12장 실행

This commit is contained in:
Haesun Park
2018-04-17 16:37:02 +09:00
parent 53bff95537
commit cdf7810446

View File

@@ -1,69 +1,78 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"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": [
"**Chapter 12 Distributed TensorFlow**"
"%load_ext watermark\n",
"%watermark -v -p numpy,sklearn,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 12._"
"**12장 분산 텐서플로**"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Setup"
"_이 노트북은 11장에 있는 모든 샘플 코드와 연습문제 해답을 가지고 있습니다._"
]
},
{
"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": true,
"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 +80,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 = \"distributed\"\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 +93,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Local server"
"# 로컬 서버"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
@@ -108,12 +109,8 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"c = tf.constant(\"Hello distributed TensorFlow!\")\n",
@@ -122,12 +119,8 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
@@ -144,22 +137,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Cluster"
"# 클러스터"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"cluster_spec = tf.train.ClusterSpec({\n",
@@ -176,12 +162,8 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"task_ps0 = tf.train.Server(cluster_spec, job_name=\"ps\", task_index=0)\n",
@@ -193,22 +175,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Pinning operations across devices and servers"
"# 여러 디바이스와 서버에 연산을 할당하기"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"reset_graph()\n",
@@ -225,12 +200,8 @@
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
@@ -248,12 +219,8 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"reset_graph()\n",
@@ -262,14 +229,14 @@
" ps_tasks=2,\n",
" ps_device=\"/job:ps\",\n",
" worker_device=\"/job:worker\")):\n",
" v1 = tf.Variable(1.0, name=\"v1\") # pinned to /job:ps/task:0 (defaults to /cpu:0)\n",
" v2 = tf.Variable(2.0, name=\"v2\") # pinned to /job:ps/task:1 (defaults to /cpu:0)\n",
" v3 = tf.Variable(3.0, name=\"v3\") # pinned to /job:ps/task:0 (defaults to /cpu:0)\n",
" s = v1 + v2 # pinned to /job:worker (defaults to task:0/cpu:0)\n",
" v1 = tf.Variable(1.0, name=\"v1\") # /job:ps/task:0 (defaults to /cpu:0) 에 할당\n",
" v2 = tf.Variable(2.0, name=\"v2\") # /job:ps/task:1 (defaults to /cpu:0) 에 할당\n",
" v3 = tf.Variable(3.0, name=\"v3\") # /job:ps/task:0 (defaults to /cpu:0) 에 할당\n",
" s = v1 + v2 # /job:worker (defaults to task:0/cpu:0) 에 할당\n",
" with tf.device(\"/task:1\"):\n",
" p1 = 2 * s # pinned to /job:worker/task:1 (defaults to /cpu:0)\n",
" p1 = 2 * s # /job:worker/task:1 (defaults to /cpu:0) 에 할당\n",
" with tf.device(\"/cpu:0\"):\n",
" p2 = 3 * s # pinned to /job:worker/task:1/cpu:0\n",
" p2 = 3 * s # /job:worker/task:1/cpu:0 에 할당\n",
"\n",
"config = tf.ConfigProto()\n",
"config.log_device_placement = True\n",
@@ -280,31 +247,24 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Readers"
"# 리더"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No more files to read\n",
"[array([[ 4.00000000e+00, 5.00000000e+00],\n",
" [ 1.00000000e+00, 8.62997533e-19]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[ 7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"[array([[ 4.0000000e+00, 5.0000000e+00],\n",
" [ 1.0000000e+00, -3.9627734e+03]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"No more training instances\n"
]
}
@@ -346,23 +306,19 @@
" while True:\n",
" sess.run(enqueue_instance)\n",
" except tf.errors.OutOfRangeError as ex:\n",
" print(\"No more files to read\")\n",
" print(\"더 이상 읽을 파일이 없습니다\")\n",
" sess.run(close_instance_queue)\n",
" try:\n",
" while True:\n",
" print(sess.run([minibatch_instances, minibatch_targets]))\n",
" except tf.errors.OutOfRangeError as ex:\n",
" print(\"No more training instances\")"
" print(\"더 이상 훈련 샘플이 없습니다\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"#coord = tf.train.Coordinator()\n",
@@ -374,31 +330,24 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Queue runners and coordinators"
"# QueueRunner와 Coordinator"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[array([[ 7., 8.],\n",
" [ 4., 5.]], dtype=float32), array([0, 1], dtype=int32)]\n",
"[array([[ 1.00000000e+00, 8.62997533e-19]], dtype=float32), array([0], dtype=int32)]\n",
"No more training instances\n"
"[array([[ 4.0000000e+00, 5.0000000e+00],\n",
" [ 1.0000000e+00, -3.9627734e+03]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"더 이상 훈련 샘플이 없습니다\n"
]
}
],
@@ -437,26 +386,22 @@
" while True:\n",
" print(sess.run([minibatch_instances, minibatch_targets]))\n",
" except tf.errors.OutOfRangeError as ex:\n",
" print(\"No more training instances\")"
" print(\"더 이상 훈련 샘플이 없습니다\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[array([[ 4.00000000e+00, 5.00000000e+00],\n",
" [ 1.00000000e+00, 8.62997533e-19]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[ 7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"No more training instances\n"
"[array([[ 4.0000000e+00, 5.0000000e+00],\n",
" [ 1.0000000e+00, -3.9627734e+03]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"더 이상 훈련 샘플이 없습니다\n"
]
}
],
@@ -495,28 +440,20 @@
" while True:\n",
" print(sess.run([minibatch_instances, minibatch_targets]))\n",
" except tf.errors.OutOfRangeError as ex:\n",
" print(\"No more training instances\")\n",
"\n"
" print(\"더 이상 훈련 샘플이 없습니다\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Setting a timeout"
"# 타임아웃 지정하기"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
@@ -526,7 +463,7 @@
"6.0\n",
"3.0\n",
"4.0\n",
"Timed out while dequeuing\n"
"dequeue 타임 아웃\n"
]
}
],
@@ -553,40 +490,24 @@
" try:\n",
" print(sess.run(output))\n",
" except tf.errors.DeadlineExceededError as ex:\n",
" print(\"Timed out while dequeuing\")\n"
" print(\"dequeue 타임 아웃\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
"collapsed": true
},
"source": [
"# Exercise solutions"
"# 연습문제 해답"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"**Coming soon**"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -605,7 +526,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.5.5"
},
"nav_menu": {},
"toc": {
@@ -619,5 +540,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}