查看tensor数值

不能直接用print的原因:

print只能打印输出shape的信息,而要打印输出tensor的值,需要借助 class tf.Session, class tf.InteractiveSession。

因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。

法一:

>>> import tensorflow as tf 
>>> a=tf.constant([  
...         [[1.0,2.0,3.0,4.0],  
...          [5.0,6.0,7.0,8.0],  
...          [8.0,7.0,6.0,5.0],  
...          [4.0,3.0,2.0,1.0]],  
...         [[4.0,3.0,2.0,1.0],  
...          [8.0,7.0,6.0,5.0],  
...          [1.0,2.0,3.0,4.0],  
...          [5.0,6.0,7.0,8.0]]  
...     ])

# Launch the graph in a session. 

>>> sess = tf.Session() 

# Evaluate the tensor `a`. 

>>> print(sess.run(a)) 
[[[ 1.  2.  3.  4.]
  [ 5.  6.  7.  8.]
  [ 8.  7.  6.  5.]
  [ 4.  3.  2.  1.]]

 [[ 4.  3.  2.  1.]
  [ 8.  7.  6.  5.]
  [ 1.  2.  3.  4.]
  [ 5.  6.  7.  8.]]]

法二:

>>> import tensorflow as tf 
>>> a=tf.constant([  
...         [[1.0,2.0,3.0,4.0],  
...          [5.0,6.0,7.0,8.0],  
...          [8.0,7.0,6.0,5.0],  
...          [4.0,3.0,2.0,1.0]],  
...         [[4.0,3.0,2.0,1.0],  
...          [8.0,7.0,6.0,5.0],  
...          [1.0,2.0,3.0,4.0],  
...          [5.0,6.0,7.0,8.0]]  
...     ])

# Launch the graph in a session. 

>>> with tf.Session():
...     print(a.eval())
... 

[[[ 1.  2.  3.  4.]
  [ 5.  6.  7.  8.]
  [ 8.  7.  6.  5.]
  [ 4.  3.  2.  1.]]

 [[ 4.  3.  2.  1.]
  [ 8.  7.  6.  5.]
  [ 1.  2.  3.  4.]
  [ 5.  6.  7.  8.]]]


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容