127、TensorFlow 计算图执行(二)

import tensorflow as tf
# Define a placeholder that expects a vector of three floating-point values
# and a computation that depends on it
x = tf.placeholder(tf.float32, shape=[3])
y = tf.square(x)

with tf.Session() as sess:
    # Feeding a value changes the result that is returned when you evaluate y
    print(sess.run(y, {x:[1.0, 2.0, 3.0]}))  # => "[1.0,4.0,9.0]"
    print(sess.run(y, {x:[0.0, 0.0, 5.0]}))  # => "[0.0,0.0,25.0]"
    
    # Raises "tf.errors.InvalidArgumentError" , because you must feed a value for
    # a 'tf.placeholder()' when evaluating a tensor that depends on it
    sess.run(y)
    
    # Raise 'ValueError', because the shape of 37.0 does not match the shape
    # of placeholder x
    sess.run(y, {x:37.0})

下面是输出的结果:

2018-02-17 11:11:35.215329: 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.]
[  0.   0.  25.]

 

posted @ 2018-02-17 11:13  香港胖仔  阅读(208)  评论(0编辑  收藏  举报