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: f_jenkinsworkspace el-winMwindowsPY35 ensorflowcoreplatformcpu_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.]