学习笔记TF063:TensorFlow Debugger

TensorFlow Debugger(tfdbg),TensorFlow专用调试器。用断点、计算机图形化展现实时数据流,可视化运行TensorFlow图形内部结构、状态。有助训练推理调试模型错误。https://www.tensorflow.org/programmers_guide/debugger

常见错误类型,非数字(nan)、无限值(inf)。tfdbg命令行界面(command line interface,CLI)。

Debugger示例。错误运行MNIST训练,通过TensorFlow Debugger找到出错地方,改正。https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/debug/examples/debug_mnist.py

先直接执行。

python -m tensorflow.python.debug.examples.debug_mnist

准确率第一次训练上千,后面保持较低水平。 TensorFlow Debugger,在每次调用run()前后基于终端用户界面(UI),控制执行、检查图内部状态。

from tensorflow.python import debug as tf_debug
sess = tr.debug.LocalCLIDebugWrapperSession(sess)
sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)

张量值注册过滤器has_inf_on_nan,判断图中间张量是否有nan、inf值。 开启调试模式(debug)。

python -m tensorflow.python.debug.examples.debug_mnist -debug
python debug_mnist.py --debug=True

运行开始UI(run-start UI),在tfdbg>后输入交互式命令,run()进入运行结束后UI(run-end UI)。连续运行10次

tfdbg>run -t 10

找出图形第一个nan或inf值

tfdbg> run -f has_inf_or_nan

第一行灰底字表示tfdbg在调用run()后立即停止,生成指定过滤器has_inf_or_nan中间张量。第4次调用run(),36个中间张量包含inf或nan值。首次出现在cross_entropy/Log:0。单击图中cross_entropy/Log:0,单击下划线node_info菜单项,看节点输入张量,是否有0值。

tfdbg>pt softmax/Softmax:0

用ni命令-t标志追溯 ni -t cross_entropy/Log 问题代码

diff = -(y_ * tf.log(y))

修改,对tf.log输入值裁剪

diff = y_ * tf.log(tf.clip_by_value(y, 1e-8, 1.0))

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")
  logits = nn_layer(hidden, HIDDEN_SIZE, NUM_LABELS, "output", tf.identity)
  y = tf.nn.softmax(logits)
  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 the built-in, numerically-stable implementation to fix this
    # issue:
    #   diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=logits)
    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)
  # 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)

远程调试。tfdbg offline_analyzer。设置本地、远程机器能访问共享目录。debug_utils.watch_graph函数设置运行时参数选项。运行session.run(),中间张量、运行时图像转储到共享目录。本地终端用tfdbg offline_analyzer加载、检查共享目录数据。

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

源码

from tensorflow.python.debug.lib import debug_utils
# 构建图,生成session对象,省略
run_options = tf.RunOptions()
      debug_utils.watch_graph(
          run_options,
          sess.graph,
         # 共享目录位置
          # 多个客户端执行run,应用多个不同共享目录
          debug_urls=["file://home/somebody/tfdbg_dumps_1"])
session.run(fetches, feed_dict=feeds, options=run_options)

或用会话包装器函数DumpingDebugWrapperSession在共享目录产生训练累积文件。

from tensorflow.python.debug.lib import debug_utils
sess = tf_debug.DumpingDebugWrapperSession(sess, "/home/somebody/tfdbg_dumps_1", watch_fn=my_watch_fn)

参考资料: 《TensorFlow技术解析与实战》

欢迎推荐上海机器学习工作机会,我的微信:qingxingfengzi

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏生信技能树

生物信息学技能面试题(第4题)-多个同样的行列式文件合并起来

相信用过htseq-count的朋友都知道,它是分开对每个样本计算所有的基因表达量,所以会生成一个个独立的文件,我用perl脚本模仿它的结果如下: $ head...

3117
来自专栏木子昭的博客

创意终端影集左侧效果图:

左侧效果图: ? ? ? ? ? 实现思路: 通过python的PIL库,将彩色转黑白(256种灰度),创建字符集,建立字符集与灰度的映射 ? 动图 把照片裁成...

3397
来自专栏Jerry的SAP技术分享

CRM Transaction处理中的权限控制

1. 检查Authorization object CRM_ORD_OP:

17910
来自专栏ytkah

利用excel数据透视表实现快速统计相关数据

  昨天ytkah在做数据报表时需要做一些具体统计数字:公司每天都有人申请铅笔、笔记本等一些文具用品,现在想要统计每天申请铅笔多少支、笔记本多少本,如下图所示,...

28410
来自专栏大壮

iOS音频(基础篇)-常用的音频格式

1673
来自专栏大数据风控

R文本挖掘-中文分词Rwordseg

我们的数据分析工作,不仅仅有对数据的分析,还有对文字资料整合的统计分析。在进行词频统计之前,有一项必须要做的工作就是中文的分词。 语料库的处理 语料库 语料...

2326
来自专栏sunseekers

哪些你知道或不知道的css,在这里或许都齐全 css编码技巧 css小技巧

暑假实习的时候带我的师傅,告诉我要注重基础,底层实现原理。才能在日新月异的技术行业站住脚跟,以不变应万变,万丈高楼平地起,所以我们应该不断的去学习,去交流。交流...

401
来自专栏数据小魔方

R语言可视化——关于ggplot所支持的数据地图素材类型

做了这么多数据地图,是时候该总结一些心得和理念了,今天这篇讨论ggplot2所支持的数据地图素材格式。 library("plyr") library("dp...

3254
来自专栏LeoXu的博客

借助 iText 用代码在 PDF 中创建空白签名域

722
来自专栏技术记录

FFMPEG指令

FFmpeg是一个用于音视频处理的自由软件,被广泛用于音视频开发。FFmpeg功能强大,本文主要介绍如何使用FFmpeg命令行工具进行简单的视频处理。 安装FF...

1.1K16

扫码关注云+社区