代码

i=tf.constant(0,dtype=tf.int32)
batch_len=tf.constant(10,dtype=tf.int32)
loop_cond = lambda a,b: tf.less(a,batch_len)
#yy=tf.Print(batch_len,[batch_len],"batch_len:")
yy=tf.constant(0)
loop_vars=[i,yy]
def _recurrence(i,yy):
c=tf.constant(2,dtype=tf.int32)
x=tf.multiply(i,c)
print_info=tf.Print(x,[x],"x:")
yy=yy+print_info
i=tf.add(i,1)
return i,yy
i,yy=tf.while_loop(loop_cond,_recurrence,loop_vars,parallel_iterations=1)#可以批处理
sess = tf.Session()
sess.run(yy)

输出信息

为什么会这样,因为执行sess.run(yy)的时候,会有数据流过循环体中的所有tf.Print节点,此时就会执行tf.Print中指定的输出。最关键的操作就是yy=yy+print_info

存在的问题(与Spyder有关)

在spyder中使用调试模式的时候,无法输出上面的信息。

上面的代码是使用‘python 测试程序__在循环中使用tf.print.py’的方式在命令行执行才会输出。

如何不断的输出tf.Print信息

除了上述使用yy=yy+print_info。

如果print_info是这样的,比如:

print_info=tf.Print(constructionErrorMatrix,[constructionErrorMatrix],"constructionErrorMatrix:")#专门为了调试用,输出相关信息。
tfPrint=tfPrint+tf.to_int32(print_info[0])#一种不断输出tf.Print的方式,注意tf.Print的返回值。
constructionErrorMatrix是一个(?,)类型的float64 Tensor。我们可以用上述代码,继续进行tf.Print的输出。

此外,tf.Print中的第二个参数[]中放入的内容,也必须是能够转为Tensor。否则会提示

TypeError: Tensors in list passed to 'data' of 'Print' Op have types [<NOT CONVERTIBLE TO TENSOR>] that are invalid.

比如,一个Tensor的shape中如果有“?,就不能转换为Tensor。对于这种不能Tensor,我们不能用get_shape()[i].value去获取?的维度,但是我们可以用tf.shape获取有数据流入以后的动态维度。就是?最终确定的维度。

10 tensorflow在循环体中用tf.print输出节点内容的更多相关文章

  1. 使用tf.print()打印tensor内容

    使用tf.Print()打印tensor内容,这是tensorflow中调试bug的一个手段,例子如下所示: import tensorflow as tf a = tf.Variable(tf.ra ...

  2. 【转载】 tf.Print() (------------ tensorflow中的print函数)

    原文地址: https://blog.csdn.net/weixin_36670529/article/details/100191674 ------------------------------ ...

  3. tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入

    一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...

  4. Tensorflow滑动平均模型tf.train.ExponentialMovingAverage解析

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 移动平均法相关知识 移动平均法又称滑动平均法.滑动平均模型法(Moving average,MA) 什么是移动平均法 移动平均法是用一组最近的实 ...

  5. Tensorflow之调试(Debug) && tf.py_func()

    Tensorflow之调试(Debug)及打印变量 tensorflow调试tfdbg 几种常用方法: 1.通过Session.run()获取变量的值 2.利用Tensorboard查看一些可视化统计 ...

  6. import tensorflow 报错: tf.estimator package not installed.

    import tensorflow 报错: tf.estimator package not installed. 解决方案1: 安装 pip install tensorflow-estimator ...

  7. python的print()输出

    1.普通的输出: print(str)#str是任意一个字符串,数字··· 2.格式化输出: print('1,2,%s,%d'%('asd',4)) 1,2,asd,4 与C语言有点类似 3.其它: ...

  8. 使用if判断字符串的长度是否等于10,根据结果进行不同的输出

    string1=' print(len(string1)) # 使用len()计算字符串长度 if not len(string1) ==10: print('字符串的长度不为10') else: p ...

  9. Windows 10 Tensorflow 2 gpu正式版安装和更新日志

    Windows 10 Tensorflow 2 gpu正式版安装和更新日志 Tensorflow 2.0.0 released on2019年10月1日星期二 Link: https://github ...

随机推荐

  1. 4-nginx-反向代理到tomcat及负载均衡

    反向代理相比于正向代理, 比如使用搬瓦工时, 就是位于客户端的正想代理, 而反向代理则是服务器端的代理, 主要用于实现请求分发, 负载均衡等功能 正向代理推荐一个: 搬瓦工, 比较好用.. 反向代理主 ...

  2. Mac 硬盘中各个文件夹详解

    打开Macintosh HD你会发现内中有四个文件夹(一般情况下,隐藏文件夹是不可见的,而且,可能会更多,比如安装xcode后会有developer文件夹). 分别有——应用程序(Applicatio ...

  3. haml参考大全

    原文来自: http://blackanger.blog.51cto.com/140924/47642   Haml是一种用来描述任何XHTML web document的标记语言,它是干净,简单的. ...

  4. JAVA-4NIO之Channel之间的数据传输

    转载:自并发编程网ifeve.com 在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(译者注:channel中文常译作通道)传输到另外一个c ...

  5. Kafka安装kafka-manager

    1 .下载kafka-manager 想要查看和管理Kafka,完全使用命令并不方便,我们可以使用雅虎开源的Kafka-manager,GitHub地址如下: https://github.com/y ...

  6. Qt5 编程基础

    Qt 是一个C++ GUI应用框架,Qt 具有良好的可移植性支持大多数桌面和移动操作系统并常用于嵌入式开发. Qt的发行版分为商业版和开源版,提供了Qt Creator作为轻量级IDE. Hello ...

  7. Codeforces 960G. Bandit Blues

    Description 你需要构造一个长度为 \(n\) 的排列 , 使得一个数作为前缀最大值的次数为 \(A\) , 作为后缀最大值的次数为 \(B\) , 求满足要求的排列个数 . 题面 Solu ...

  8. Percona-mysql server 5.5升级5.6

    http://blog.csdn.net/lqx0405/article/details/50162557   系统环境:      操作系统:CentOS_6.5(64)            My ...

  9. DOM-访问元素

    id:元素在文档中唯一标识符. title:有关元素的附加说明信息,一般通过工具提示条显示出来. lang:元素内容的语言编码,很少使用 dir:语言方向,职位“Ltr”(从左至右).Rtl(从右至左 ...

  10. SQL 修改表字段失败 解决方法

    OK  大功告成 !!!