代码

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. jQuery中的100个技巧(译)

    1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. <sc ...

  2. centos7上安装redis

    关闭防火墙:systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewall开机启 ...

  3. 【数组】Minimum Path Sum

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  4. Android 开发工具类 14_ JsonTools

    天气 JSON 数据解析 package com.example.weather_json.tools; import java.util.ArrayList; import java.util.Li ...

  5. Camtasia Studio的下载

    不多说,直接上干货! 具体还没写 https://www.techsmith.com/video-editor.html 欢迎大家,加入我的微信公众号:大数据躺过的坑        人工智能躺过的坑 ...

  6. springboot-26-springboot 集成rabbitmq

    rabbitmq是基于AMQP规范的一个消息代理, 它可以兼容jms, 支持其他语言, 并且可以跨平台 1, 安装 1) 普通安装 度娘: 2) docker 安装 sudo docker run - ...

  7. Cacheable redis 宕机

    使用Cacheable注解Redis方法时,如果Redis服务器挂了,就直接抛出异常了, java.net.ConnectException: Connection refused: connect ...

  8. MongoDB之数据库管理

    前面补充过把MongoDB作为服务,今天主要了解数据库管理,在操作之前先把MongoDB服务打开. 一.显示数据库清单 如果想查看数据库列表可以使用show dbs. 二.切换数据库 通过使用Mong ...

  9. IOS应用图标尺寸

    Table 41-1Size (in pixels) of custom icons and images Asset iPhone 6 Plus (@3x) iPhone 6 and iPhone ...

  10. 问题集录--Java高级软件工程师面试考纲(转)

    如果要应聘高级开发工程师职务,仅仅懂得Java的基础知识是远远不够的,还必须懂得常用数据结构.算法.网络.操作系统等知识.因此本文不会讲解具体的技术,笔者综合自己应聘各大公司的经历,整理了一份大公司对 ...