10九、TensorFlow计算张量的值

# 当计算图建立成功时
# 你就能够运行这个计算图,而后生成一个新的张量
# 而且获得这个张量指向的计算图中具体的数值
#这个功能在debug的时候很是有必要
#最简单得到张量具体值的方法是使用Tensor.eval method
import tensorflow as tf
constant = tf.constant([1, 2, 3])
#进行点乘,对应位置的元素相乘
tensor = constant * constant
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    #eval方法只有当session开启的时候才好用
    #Tensor.eval 方法返回一个python的numpy对象,和原来的张量有着相同的值
    print(tensor.eval())

下面是上面输出的张量的结果:html

2018-02-16 21:46:48.085119: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
[1 4 9]

 

转载于:https://www.cnblogs.com/weizhen/p/8450608.htmlpython