您的位置:首页 > 其它

tensorflow55 《TensorFlow技术解析与实战》16 TensorFlow Debugger

2017-06-10 18:21 726 查看

01 环境

# 《TensorFlow技术解析与实战》15 TensorFlow线性代数编译框架XLA
# win10 Tensorflow-gpu1.2.0-rc0 python3.5.3
# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
# https://github.com/tensorflow/tensorflow/blob/v1.2.0-rc0/tensorflow/python/debug/examples/debug_mnist.py # tensorflow/python/debug/examples目录下还有debug_errors.py、debug_fibonacci.py、debug_tflearn_iris.py3个测试文件可以研究。


debug_mnist.py中用到了readline函数,需要安装readline库。如果没有安装readline库,运行debug_mnist.py时,会提示
ImportError: No module named 'readline'
错误。

使用
pip3 search readline
找到readline版本,采用pip3 install readline 安装readline,会提示readline不支持windows系统。阅读
pip3 search readline
结果,发现anyreadline比较合适(0.1.1版本)。安装
pip3 install anyreadline
后,运行 debug_mnist.py正常。

02 测试TensorFlow Debugger使用

启用TensorFlow Debugger,需要在run()之前加如下3行代码:

from tensorflow.python import debug as tf_debug
sess = tf_debug.LocalCLIDebugWrapperSession(sess)
# 注册过滤器has_inf_or_nan,它能判断出图中任何中间张量中是否有inf和nan
sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)


02.01 运行
python debug_mnist.py
,结果如下:



# debug_mnist.py
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0 #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Demo of the tfdbg curses CLI: Locating the source of bad numerical values.

The neural network in this demo is larged based on the tutorial at:
tensorflow/examples/tutorials/mnist/mnist_with_summaries.py

But modifications are made so that problematic numerical values (infs and nans)
appear in nodes of the graph during training.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python import debug as tf_debug

IMAGE_SIZE = 28
HIDDEN_SIZE = 500
NUM_LABELS = 10
RAND_SEED = 42

def main(_):
# Import data
mnist = input_data.read_data_sets(FLAGS.data_dir,
one_hot=True,
fake_data=FLAGS.fake_data)

def feed_dict(train):
if train or FLAGS.fake_data:
xs, ys = mnist.train.next_batch(FLAGS.train_batch_size,
fake_data=FLAGS.fake_data)
else:
xs, ys = mnist.test.images, mnist.test.labels

return {x: xs, y_: ys}

sess = tf.InteractiveSession()

# Create the MNIST neural network graph.

# Input placeholders.
with tf.name_scope("input"):
x = tf.placeholder(
tf.float32, [None, IMAGE_SIZE * IMAGE_SIZE], name="x-input")
y_ = tf.placeholder(tf.float32, [None, NUM_LABELS], name="y-input")

def weight_variable(shape):
"""Create a weight variable with appropriate initialization."""
initial = tf.truncated_normal(shape, stddev=0.1, seed=RAND_SEED)
return tf.Variable(initial)

def bias_variable(shape):
"""Create a bias variable with appropriate initialization."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
"""Reusable code for making a simple neural net layer."""
# Adding a name scope ensures logical grouping of the layers in the graph.
with tf.name_scope(layer_name):
# This Variable will hold the state of the weights for the layer
with tf.name_scope("weights"):
weights = weight_variable([input_dim, output_dim])
with tf.name_scope("biases"):
biases = bias_variable([output_dim])
with tf.name_scope("Wx_plus_b"):
preactivate = tf.matmul(input_tensor, weights) + biases

activations = act(preactivate)
return activations

hidden = nn_layer(x, IMAGE_SIZE**2, HIDDEN_SIZE, "hidden")
y = nn_layer(hidden, HIDDEN_SIZE, NUM_LABELS, "softmax", act=tf.nn.softmax)

with tf.name_scope("cross_entropy"):
# The following line is the culprit of the bad numerical values that appear
# during training of this graph. Log of zero gives inf, which is first seen
# in the intermediate tensor "cross_entropy/Log:0" during the 4th run()
# call. A multiplication of the inf values with zeros leads to nans,
# which is first in "cross_entropy/mul:0".
#
# You can use clipping to fix this issue, e.g.,
#   diff = y_ * tf.log(tf.clip_by_value(y, 1e-8, 1.0))

diff = y_ * tf.log(y)
with tf.name_scope("total"):
cross_entropy = -tf.reduce_mean(diff)

with tf.name_scope("train"):
train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(
cross_entropy)

with tf.name_scope("accuracy"):
with tf.name_scope("correct_prediction"):
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
with tf.name_scope("accuracy"):
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

sess.run(tf.global_variables_initializer())

if FLAGS.debug:
sess = tf_debug.LocalCLIDebugWrapperSession(sess, ui_type=FLAGS.ui_type)
sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)

# Add this point, sess is a debug wrapper around the actual Session if
# FLAGS.debug is true. In that case, calling run() will launch the CLI.
for i in range(FLAGS.max_steps):
acc = sess.run(accuracy, feed_dict=feed_dict(False))
print("Accuracy at step %d: %s" % (i, acc))

sess.run(train_step, feed_dict=feed_dict(True))

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument(
"--max_steps",
type=int,
default=10,
help="Number of steps to run trainer.")
parser.add_argument(
"--train_batch_size",
type=int,
default=100,
help="Batch size used during training.")
parser.add_argument(
"--learning_rate",
type=float,
default=0.025,
help="Initial learning rate.")
parser.add_argument(
"--data_dir",
type=str,
default="/tmp/mnist_data",
help="Directory for storing data")
parser.add_argument(
"--ui_type",
type=str,
default="curses",
help="Command-line user interface type (curses | readline)")
parser.add_argument(
"--fake_data",
type="bool",
nargs="?",
const=True,
default=False,
help="Use fake MNIST data for unit testing")
parser.add_argument(
"--debug",
type="bool",
nargs="?",
const=True,
default=False,
help="Use debugger to track down bad values during training")
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)


02.02 进入debug调试界面

运行
python debug_mnist.py --debug=True




02.03
run -f has_inf_or_nan



02.04
ni -t cross_entropy/Log



tfdbg> ni -t cross_entropy/Log
Node cross_entropy/Log

Op: Log
Device: /job:localhost/replica:0/task:0/gpu:0

1 input(s) + 0 control input(s):
1 input(s):
[Softmax] softmax/Softmax

3 recipient(s) + 0 control recipient(s):
3 recipient(s):
[Shape] train/gradients/cross_entropy/mul_grad/Shape_1
[Mul] cross_entropy/mul
[Mul] train/gradients/cross_entropy/mul_grad/mul

Traceback of node construction:
0: debug_mnist.py
Line:     177
Function: <module>
Text:     "tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)"

1: C:\Python35\lib\site-packages\tensorflow\python\platform\app.py
Line:     48
Function: run
Text:     "_sys.exit(main(_sys.argv[:1] + flags_passthrough))"

2: debug_mnist.py
Line:     105
Function: main
Text:     "diff = y_ * tf.log(y)"

3: C:\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py
Line:     1118
Function: log
Text:     "result = _op_def_lib.apply_op("Log", x=x, name=name)"

4: C:\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py
Line:     767
Function: apply_op
Text:     "op_def=op_def)"

5: C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py
Line:     2506
Function: create_op
Text:     "original_op=self._default_original_op, op_def=op_def)"

6: C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py
Line:     1269
Function: __init__
Text:     "self._traceback = _extract_stack()"


如果所示:与debug_mnist.py相关的记录只有一条,并且指明是105行的”diff = y_ * tf.log(y)”。分析后,修改该语句为:
diff = y_ * tf.log(tf.clip_by_value(y, 1e-8, 1.0))
。重新运行。

02.05
python debug_mnist.py



02.06 tfdbg help

tfdbg> help

TTTTTT FFFF DDD  BBBB   GGG
TT   F    D  D B   B G
TT   FFF  D  D BBBB  G  GG
TT   F    D  D B   B G   G
TT   F    DDD  BBBB   GGG

Upcoming run:
======================================
Session.run() call #1:

Fetch(es):
accuracy/accuracy/Mean:0

Feed dict(s):
input/x-input:0
input/y-input:0
======================================

Select one of the following commands to proceed ---->
run:
Execute the run() call with debug tensor-watching
run -n:
Execute the run() call without debug tensor-watching
run -t <T>:
Execute run() calls (T - 1) times without debugging, then execute run() once more with debugging and drop back to the CLI
run -f <filter_name>:
Keep executing run() calls until a dumped tensor passes a given, registered filter (conditional breakpoint mode)
Registered filter(s):
* has_inf_or_nan
invoke_stepper:
Use the node-stepper interface, which allows you to interactively step through nodes involved in the graph run() call and inspect/modify their values

For more details, see help..

help
Aliases: h

Print this help message.

invoke_stepper
Aliases: s

Invoke stepper (cont, step, breakpoint, etc.)

optional arguments:
-h, --help  show this help message and exit

run
Aliases: r

Run through, with or without debug tensor watching.

optional arguments:
-h, --help            show this help message and exit
-t TIMES, --times TIMES
How many Session.run() calls to proceed with.
-n, --no_debug        Run through without debug tensor watching.
-f TILL_FILTER_PASS, --till_filter_pass TILL_FILTER_PASS
Run until a tensor in the graph passes the specified
filter.
--node_name_filter NODE_NAME_FILTER
Regular-expression filter for node names to be watched
in the run, e.g., loss, reshape.*
--op_type_filter OP_TYPE_FILTER
Regular-expression filter for op type to be watched in
the run, e.g., (MatMul|Add), Variable.*
--tensor_dtype_filter TENSOR_DTYPE_FILTER
Regular-expression filter for tensor dtype to be
watched in the run, e.g., (float32|float64), int.*
-p, --profile         Run and profile TensorFlow graph execution.

run_info
Aliases: ri

Display information about this Session.run() call.

optional arguments:
-h, --help  show this help message and exit


03 远程调试方法(未测试)

采用tfdbg的offiline_analyzer。设置一个本地和远程主机都能访问的共享目录,如:/home/somebody/tfdbg_dumps_1。然后,通过debug_utils.watch_graph函数设置运行时的参数选项。在运行session.run()时就会将中间张量和运行时的图像转储到共享目录中。方法如下:

from tensorflow.python.debug import debug_utils
# 此处代码如:构建图,生成session对象,略
run_options = tf.RunOptions()
debug_utils.watch_graph(
run_options,
session.graph,
debug_utils=[file:///home/somebody/tfdbg_dumps_1]) # 共享目录位置
# 这里如果用多个客户端执行run,应该使用多个不同的共享目录
session.run(fetches, feed_dict=feeds, options=run_options)


这样,在本地终端上就可以使用tfdbg的offline_analyzer来加载和检查共享目录中的数据。方法如下:

python -m tensorflow.python.debug.cli.offline_analyzer --dump_dir=/home/somebody/tfdbg_dumps_1


另外一种更简单和灵活的方法是使用回话的包装器函数DumpingDebugWrapperSession来在共享目录中产生训练中的累积文件。如下:

from tensorflow.python.debug import debug_utils
sess = tf_debug.DumpingDebugWrapperSession(sess, "/home/somebody/tfdbg_dumps_1/", watch_fn=my_watch_fn)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: