在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候。也许你会说,这个很容易啊,直接print就可以了。其实不然,print只能打印输出shape的信息,而要打印输出tensor的值,需要借助class tf.Session, class tf.InteractiveSession。因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。

一 class tf.Session

运行tensorflow操作的类,其对象封装了执行操作对象和评估tensor数值的环境。这个我们之前介绍过,在定义好所有的数据结构和操作后,其最后运行。

  1. import tensorflow as tf
  2. # Build a graph.
  3. a = tf.constant(5.0)
  4. b = tf.constant(6.0)
  5. c = a * b
  6. # Launch the graph in a session.
  7. sess = tf.Session()
  8. # Evaluate the tensor `c`.
  9. print(sess.run(c))

二 class tf.InteractiveSession

顾名思义,用于交互上下文的session,便于输出tensor的数值。与上一个Session相比,其有默认的session执行相关操作,比如:Tensor.eval(), Operation.run()。Tensor.eval()是执行这个tensor之前的所有操作,Operation.run()也同理。

  1. import tensorflow as tf
  2. a = tf.constant(5.0)
  3. b = tf.constant(6.0)
  4. c = a * b
  5. with tf.Session():
  6. # We can also use 'c.eval()' here.
  7. print(c.eval())

tf tensor 输出的更多相关文章

  1. 10 tensorflow在循环体中用tf.print输出节点内容

    代码 i=tf.constant(0,dtype=tf.int32) batch_len=tf.constant(10,dtype=tf.int32) loop_cond = lambda a,b: ...

  2. typeError:The value of a feed cannot be a tf.Tensor object.Acceptable feed values include Python scalars,strings,lists.numpy ndarrays,or TensorHandles.For reference.the tensor object was Tensor...

    如上贴出了:错误信息和错误代码. 这个问题困扰了自己两天,报错大概是说输入的数据和接受的格式不一样,不能作为tensor. 后来问了大神,原因出在tf.reshape(),因为网络训练时用placeh ...

  3. 将Tensor输出到文件

    ) local file = io.open('/home/xbwang/Desktop/part2original','a') ,length do number = part2[j] file:w ...

  4. AI - TensorFlow - 张量(Tensor)

    张量(Tensor) 在Tensorflow中,变量统一称作张量(Tensor). 张量(Tensor)是任意维度的数组. 0阶张量:纯量或标量 (scalar), 也就是一个数值,例如,\'Howd ...

  5. 什么是Tensor

    https://blog.csdn.net/kansas_lh/article/details/79321234 tensor是tensorflow基础的一个概念——张量. Tensorflow用到了 ...

  6. TF常用知识

    命名空间及变量共享 # coding=utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt; ...

  7. tf中计算图 执行流程学习【转载】

    转自:https://blog.csdn.net/dcrmg/article/details/79028003 https://blog.csdn.net/qian99/article/details ...

  8. tf中的run()与eval()【转载】

    转自:https://blog.csdn.net/jiaoyangwm/article/details/79248535  1.eval() 其实就是tf.Tensor的Session.run() 的 ...

  9. tf.nn.rnn_cell.MultiRNNCell

    Class tf.contrib.rnn.MultiRNNCell 新版 Class tf.nn.rnn_cell.MultiRNNCell 构建多隐层神经网络 __init__(cells, sta ...

随机推荐

  1. css字体更小 css比12px更小的方法

    <span style="margin-top: 0;-webkit-transform-origin-x: 0;-webkit-transform: scale(0.90);&quo ...

  2. iOS 开发之环形倒计时进度条(虚线/实线)

    代码很简单,一看便知.这里为顺时针,若想要逆时针,clockwise改为0,还需更改起始角度和终点角度. 源码地址:https://github.com/LfyDragon/CountDown 直接上 ...

  3. 团队项目第二阶段个人进展——Day2

    一.昨天工作总结 冲刺第二天,基本完成了自己对第二阶段信息发布功能完善的规划 二.遇到的问题 不知道后端数据该如何封装处理 三.今日工作规划 先重新布局发布页面,并添加重置按钮

  4. c# 为什么要使用Array、ArrayList、List?

    c#也是一直在进化的,从数组进化到ArrayList,再进化到泛型就是个例子. static void Main(string[] args) { //数组的增删改查 //定义数组 ] { ,,,, ...

  5. innodb_fast_shutdown的内幕

    Innodb_fast_shutdown告诉innodb在它关闭的时候该做什么工作.有三个值可以选择:1.  0表示在innodb关闭的时候,需要purge all, merge insert buf ...

  6. ELF文件结构描述

    ELF目标文件格式最前部ELF文件头(ELF Header),它包含了描述了整个文件的基本属性,比如ELF文件版本.目标机器型号.程序入口地址等.其中ELF文件与段有关的重要结构就是段表(Sectio ...

  7. 怎样在 fedora 28 上 打开 .jnlp 文件

    最近使用 iDrac 和 iLO 总是会使用到 .jnlp 文件, 为了方便,今天把设置过程记录下来. JNLP 文件,全名为 Java Network Launching Protocol 文件, ...

  8. The 10 Best Neighborhoods in Seattle

    https://www.seattlemet.com/articles/2015/4/24/the-10-best-neighborhoods-in-seattle-may-2015 By Darre ...

  9. python基本语法:

    http://www.runoob.com/python/python-basic-syntax.html

  10. [python]如何理解uiautomator里面的 child, child_by_text, sibling,及使用场景

    如何理解uiautomator里面的 child, child_by_text, sibling,我们借助android原生的uiautomatorviewer抓取的控件来进行理解 以如下图进行详细讲 ...