TensorFlow--交互式使用--tf.InteractiveSession()

用tf.Session()创建会话时只有在会话中run某个张量才能得到这个张量的运算结果,而交互式环境下如命令行、IPython,想要执行一行就得到结果,这就需要用到tf.InteractiveSession(),并且可以使用Tensor.eval() 得到张量的具体值。

import tensorflow as tf
sess = tf.InteractiveSession()

a = tf.truncated_normal([1,5],stddev=0.1) # 创建一个1x5的随机张量

print(a)   # >> Tensor("truncated_normal_1:0", shape=(1, 5), dtype=float32)
print(a.eval()) # >> array([[ 0.0331782 , -0.0733152 , 0.01366828, 0.01550526, 0.06196761]],dtype=float32)

  

 

posted @ 2020-02-23 16:19  panday  阅读(553)  评论(0编辑  收藏  举报