TensorFlow:为什么我得到不同的结果,即使我在同一个地方运行此代码两次没有updata任何参数?

时间:2018-04-03 09:07:19

标签: tensorflow

我在mnist_inference.py中构建了一个CNN模型,我想每100步计算一次精度。但我发现它的工作正常。经过长时间的调试,我发现当我计算y的值时结果发生了变化。起初,我认为因为我计算y时参数是自动更新的。但不是!我发现参数没有变化。 那么如何计算模型的准确性呢? 这是我的代码:mycode

1 个答案:

答案 0 :(得分:2)

这段代码

y = mnist_inference.inference(x, True, regularizer)

使用dropouts创建模型:

def inference(input_tensor, train, regularizer):

    # code fragment

    with tf.variable_scope('layer5-fc1'):
        fc1_weights = tf.get_variable("weight", [nodes, FC_SIZE],
                                      initializer = tf.truncated_normal_initializer(stddev = 0.1))

        if regularizer != None:
            tf.add_to_collection('losses', regularizer(fc1_weights))
        fc1_biases = tf.get_variable('bias', [FC_SIZE], initializer = tf.constant_initializer(0.1))
        fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights)+fc1_biases)

        # enables dropout!
        if train:
            fc1 = tf.nn.dropout(fc1, 0.5)

因此,您启用了辍学,这会导致您观察到随机性。

您需要在计算准确性时禁用丢失。更高级别tf.layers.dropout具有相应的参数(可以是张量)。