学习笔记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
学习笔记TF063:TensorFlow Debugger的更多相关文章
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
- 【学习笔记】tensorflow基础
目录 认识Tensorflow Tensorflow特点 下载以及安装 Tensorflow初体验 Tensorflow进阶 图 op 会话 Feed操作 张量 变量 可视化学习Tensorboard ...
- Google TensorFlow 学习笔记一 —— TensorFlow简介
"TensorFlow is an Open Source Software Library for Machine INtenlligence" 本笔记参考tensorflow. ...
- 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字
TensorFlow实现Softmax Regression(回归)识别手写数字.MNIST(Mixed National Institute of Standards and Technology ...
- 学习笔记TF048:TensorFlow 系统架构、设计理念、编程模型、API、作用域、批标准化、神经元函数优化
系统架构.自底向上,设备层.网络层.数据操作层.图计算层.API层.应用层.核心层,设备层.网络层.数据操作层.图计算层.最下层是网络通信层和设备管理层.网络通信层包括gRPC(google Remo ...
- 学习笔记CB013: TensorFlow、TensorBoard、seq2seq
tensorflow基于图结构深度学习框架,内部通过session实现图和计算内核交互. tensorflow基本数学运算用法. import tensorflow as tf sess = tf.S ...
- 学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
TensorFlow Serving https://tensorflow.github.io/serving/ . 生产环境灵活.高性能机器学习模型服务系统.适合基于实际数据大规模运行,产生多个模型 ...
- 学习笔记TF062:TensorFlow线性代数编译框架XLA
XLA(Accelerated Linear Algebra),线性代数领域专用编译器(demain-specific compiler),优化TensorFlow计算.即时(just-in-time ...
随机推荐
- mybatis调用存储过程,获取返回的游标
将调用存储过程参数放入map中,由于返回的游标中包含很多参数,所以再写一个resultmap与之对应,类型为hashmap.设置返回的jdbcType=CURSOR,resultMap设置为id对应的 ...
- python学习1-1
# 可以支持多个用户登录 (提示,通过列表存多个账户信息) uname = ['wps', 'opp' ] pword = ['] time = 0 while time < 3: u_name ...
- 201671010142 2017-2 《java第十一章学习感悟》
事件处理基础 事件源,事件监听器,事件监听器 监听器接口的实现,监听器对象所属类必须实现与事件源相对应的接口,即必须提供接口中方法的实现. 适配器类 当程序用户试图关闭一个框架窗口时,Jframe对 ...
- float和position谁好?
float从字面上的意思就是浮动,float能让元素从文档流中抽出,它并不占文档流的空间,典型的就是图文混排中文字环绕图片的效果了.不过需要注意的是清除浮动是我们可能需要注意的地方.而position ...
- MySQL从本地向数据库导入数据
本文来自:https://www.cnblogs.com/lettuce-u/p/10715795.html(自己收藏看) 在localhost中准备好了一个test数据库和一个pet表: mysql ...
- Vue语法学习第一课——插值
学习关于Vue的插值语法 ① 文本值 : "Mustache"语法,即双大括号 <span>Message:{{msg}}</span> 注:双大括号中的m ...
- WordCount(java)
github项目链接 https://gitee.com/huwenli/Wc.git 1.项目简介 WordCount的需求可以概括为:对程序设计语言源文件统计字符数.单词数.行数,统计结果以指定格 ...
- SQL Server DDL
1:向表中添加字段 Alter table [表名] add [列名] 类型 2: 删除字段 Alter table [表名] drop column [列名] 3: 修改表中字段类型 (可以修 ...
- .NET 并行计算和并发10-lock锁
class Program { private static List<int> intlist; static void Main(string[] args) { intlist = ...
- 安装完成IIS后找不到IIS Admin Service
系统版本: Windows Server 2008 R2 Enterprise 64-bit IIS版本:IIS 7.5 问题: 在系统运行里面输入"组件服务",依次点开组件服务- ...