我正在学习神经网络,并阅读一段代码以使用mnist数据集训练神经网络。 在它的测试部分中,它在测试函数
中定义了输出变量:y = mnist_forward.forward(x, None)
def test(mnist):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
y = mnist_forward.forward(x, None)
saver = tf.train.Saver()
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
while True:
with tf.Session() as sess:
tf.initialize_all_variables().run()
ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
print("After %s train steps, test accuracy = %g" % (global_step, accuracy_score))
else:
print('No checkpoint file found')
return
time.sleep(TEST_INTERVAL_SECS)
但是,功能mnist_forward.forward
被定义为:
def forward(x, regularizer):
w1 = get_weight([INPUT_NODE, LAYER1_NODE], regularizer)
b1 = get_bias([LAYER1_NODE])
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
w2 = get_weight([LAYER1_NODE, OUTPUT_NODE], regularizer)
b2 = get_bias([OUTPUT_NODE])
y = tf.matmul(y1, w2) + b2
return y
函数get_weight
和get_bias
用于随机生成参数。似乎每次调用前向函数(进行预测)时,都会重新生成参数。
我只是不明白为什么生成参数的函数调用应该写在前向函数内部(而不是外部),以及它在模型的预测过程中如何真正起作用?
答案 0 :(得分:0)
似乎每次调用前向函数(进行预测)时,都会重新生成参数。
这是Tensorflow与标准编程之间的巨大区别,乍一看可能令人费解:Tensorflow是基于图的,因此在这种情况下,仅将“前进”功能称为ONCE来定义图。
然后Tensorflow会自动找出图中的哪个节点来计算其他一些节点,但是它永远不会再次调用forward函数。
您的代码中真正了解内部情况的重要几行如下:
# Define the graph: create all variables, how to initialize them
# and how to compute the output depending on the input
# CAUTION: VARIABLE TENSORS CURRENTLY HOLD NO VALUES THEY ARE ONLY DEFINED
y = mnist_forward.forward(x, None)
...
# Add operations to the graph to compute the accuracy metric
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
...
# Initialize all variables using the initializers defined previously
tf.initialize_all_variables().run()
...
# Restore all variables to saved values
# CAUTION: REPLACES ALL INITIALIZED VALUES TO THESE STORED ONES
saver.restore(sess, ckpt.model_checkpoint_path)
...
# Runs the graph
accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
我只是不明白为什么函数调用生成参数...
正如您在注释中看到的那样,生成参数的是'tf.initialize_all_variables()。run()'调用,'forward'函数只是定义了HOW进行初始化。