import tensorflow as tf
const = tf.constant(2.0, name="const")
b = tf.Variable(2.0, name='b')
c = tf.Variable(1.0, name='c')
# now create some operations
d = tf.add(b, c, name='d')
e = tf.add(c, const, name='e')
a = tf.multiply(d, e, name='a')
# setup the variable initialisation
init_op = tf.global_variables_initializar()
with tf.Session() as sess:
# initialise the variables
sess.run(init_op)
# compute the output of the graph
print("Variable a is {}".format(a_out))
b = tf.placeholder(tf.float32, [None, 1], name='b')
a_out = sess.run(a, feed_dict={b: np.arange(0, 10)[:, np.newaxis]})
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Python optimisation variables
learning_rate = 0.5
epochs = 10
batch_size = 100
initializar
,因为它的书写方式不正确:init_op = tf.global_variables_initializer()
您正在告诉您打印未声明的a_out
,我想应该是这样
with tf.Session() as sess: sess.run(init_op) a_out = sess.run(a) print("Variable a is {}".format(a_out))
init_op = tf.global_variables_initializar()
中存在拼写错误,应该为init_op = tf.global_variables_initializer()
。其次,您在分配a_out之前使用它。