學習筆記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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章