首页 > 解决方案 > 张量流中的前向函数

问题描述

我正在学习神经网络并阅读一段代码来用 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_weightget_bias用于随机生成参数。似乎每次调用前向函数(预测)时,都会重新生成参数。

我只是不明白为什么生成参数的函数调用应该写在前向函数内部(而不是外部),以及它如何在模型的预测过程中真正起作用?

标签: pythontensorflow

解决方案


似乎每次调用前向函数(预测)时,都会重新生成参数。

这是 Tensorflow 和标准编程之间的最大区别,一开始可能会非常令人费解:Tensorflow 是基于图形的,所以在这种情况下,“forward”函数只被称为 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”函数只是定义了如何初始化。


推荐阅读