TensorFlow调试和修复问题

本文概述

  • 调试tf-learn估算器和实验
  • 借助tfdbg调试Keras模型
  • 使用tfdbg调试tf-slim
  • 使用TF-Slim进行调试培训
  • 调试评估
  • 远程运行会话的脱机调试
  • 调试远程tf.Session
  • C ++和其他语言
  • 调试远程运行的tf学习估计器和实验
要恢复该问题, 请编辑debug_mnist.Py, 更改唯一行:
diff =-(y_*tf.log(y))

运动熵的数值稳定实现:
diff= tf.losses.sparse_softmax_cross_entropy(label=y_, logits=lgits)

使用-debug标志重新运行, 如下所示:
python -m tensorflow.python.debug.example.debug_mnist --debug

在tfdbg> 提示符下, 输入以下给定命令:
run -f has_inf_or_nan

声明没有将张量标记为包含nan或inf值, 以及保持和防止卡住的准确性。
调试tf-learn估算器和实验实验是在tf.Contrib.Examine中进行的组装, 其程度要比Estimator更好。它提供了一个单一的教育界面, 并将其与模型进行比较。为了调试对测试对象的teaching()和valuate()调用, 我们可以在调用其构造函数时使用关键字参数train_monitors和eval_hooks。
例:
从tensorflow.python导入调试为tf_debug
hooks = [tf_debug.LocalCLIDebug()]ex = experiment.Experiment(classifier, eval_input_fn=iris_input_fn, train_input_fn=iris_input_fn, train_steps=FLAGS.train_steps, eval_delay_secs=0, eval_steps=1, train_monitors=hooks, eval_hooks=hooks)ex.train()accuracy_score = ex.evaluate()["accuracy"]For building and running the debug_tflearn_iris example inside the experiment mode:python -m tensorflow.python.debug.example.debug_tflearn_iris\--use_experiment --debug

LocalCLIDebugHook还允许我们配置watch_fn, 它可以用来灵活地指定要在一个session.Run()调用中监视的张量, 就像fetches和feed_dict不同状态的特征一样。
借助tfdbg调试Keras模型要将TFDBG与Keras一起使用, 请允许Keras后端使用TFDBG包装的咨询项目。要在调试过程中使用CLI包装器:
import tensorflow as tffrom tensorflow.python import debug as tf_debugfrom Keras import backend as keras_backendkeras_backend.set_session(tf_debug.LocalCLIDebugWrapperSession(tf.Session()))# Defining the Keras model below.model.fit(...) # This will break into TFDBG CLI.

使用tfdbg调试tf-slimTFDBG支持使用tf-slim调试培训和评估。指导和评估要求略有不同的调试工作流程。
使用TF-Slim进行调试培训TFDBG在tf-slender的帮助下支持TensorFlow调试培训。培训和评估使TensorFlow调试工作流有些特殊。
import tensorflow as tffrom tensorflow.python import debug as tf_debug#. Code for creating the graph.tf.contrib.slim.learning.train(train_op, logdir, number_of_steps=10, session_wrapper=tf_debug.LocalCLIDebugWrapper)

调试评估要调试学校系统, 请将LocalCLIDebugWrapperSession提供给slender.Mastering.Educate()的session_wrapper参数。
import tensorflow as tf# .Code which creates the graph and the evaltf.contrib.slim.evaluation.evaluate_once('', checkpoint_path, logdir, eval_op=my_eval_op, final_op=my_value_op, hooks=[tf_debug.LocalCLIDebugHook()])

远程运行会话的脱机调试要在实例中执行TensorFlow版本调试, 我们可以使用tfdbg的offline_analyzer二进制文件。它在转储的事实目录上运行。较低级别的会话API和较高级别的Estimator和测试API均会执行此操作。
调试远程tf.Session如果我们与tf进行互动而没有延迟。使用python中的会话API, 我们可以通过使用方法tfdbg来配置我们称为你的session.Run()技术的RunOptions原型。
For instance:import debug as tf_debug # ... Code where the session and graphrun_options = tf.RunOptions()tf_debug.watch_graph(run_options, session.graph, session.run(fetches, feed_dict=feeds, options=run_options)# specify different directories for many run() calls.debug_urls=["file:///shared/storage/location/tfdb_dumps_1"])

在我们可以终端访问的环境中(例如, 附近的一台笔记本电脑可以使用上面的代码准确地进入共享车库的位置), 我们可以通过以下方式加载和检查共享存储中的Selloff目录中的记录: tfdbg的offline_analyzer二进制文件。
例:
探索Tensorflow架构和重要术语
python -m tensorflow.python.debug.cli.offline_analyzer \--dump_dir=/shared/storage/location/tfdb_dumps_1

该会话提供了一种更容易的方式来生成可以离线分析的文档系统转储。要应用它, 请将我们的咨询结果包装在tf_debug.DumpingDebugWrapperSession中。
例:
import debug as tf_debugsess = tf_debug.DumpingDebugWrapperSession(sess, "/shared/storage/location/tfdbg_dumps_1/", watch_fn=my_watch_fn)

watch_fn参数接受一个Callable, 它允许我们将张量配置为观察不同的协商。Run()调用, 类似于fetches和feed_dict对run()名称和状态的函数。
C ++和其他语言如果我们的版本代码是用C ++或其他语言编写的, 则可以另外修改RunOptionsto的debug_options主题以调试可以脱机检查的转储。有关更多信息, 请参见原型定义。
调试远程运行的tf学习估计器和实验我们可以使用非交互式DumpingDebugHook。
import debug as tf_debughooks = [tf_debug.DumpingDebugHook("/shared/storage/location/tfdbg_dumps_1")]

【TensorFlow调试和修复问题】然后, 可以以相同的方式使用此挂钩, 因为该文件前面已描述了LocalCLIDebugHook示例。在评估器或实验的评估发生时, tfdbg创建具有以下调用示例的目录:/ shared / garage / place / tfdbg_dumps_1 / run_。每个列表对应一个会话。成为suit()或compare()调用基础的Run()名称。我们可以加载目录并在命令行界面中以脱机方式使用tfdbg提供的offline_analyzer对其进行检查。
python -m tensorflow.python.debug.cli.offlineanalyzer \ dump_dir="/shared/storage/location/tfdbg_dumps_1/run_< epoch_timestamp_microsec> _< uuid> "

    推荐阅读