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.]
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步