我才刚刚开始学习张量流,并为MNIST编写了一个运动模型。因此,我正在学习一本书,但仍然存在问题,请您帮我一下吗?

以下是我的代码,其中包含问题描述,非常感谢!

x = tf.placeholder(tf.float32,[None,INPUT_NODE],name='input')
y_ = tf.placeholder(tf.float32,[None,OUTPUT_NODE],name='output')
weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE,LAYER1_NODE],stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1,shape=[LAYER1_NODE]))
weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE,OUTPUT_NODE],stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1,shape=[OUTPUT_NODE]))


下一个y =()...在不使用移动平均模型的情况下定义正向传播。

y = inference(x,None,weights1,biases1,weights2,biases2)
global_step = tf.Variable(0,trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())


下一个average_y =()...使用移动平均模型定义正向传播。

average_y = inference(x,variable_averages,weights1,biases1,weights2,biases2)

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.arg_max(y_,1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
regularization = regularizer(variable_averages.average(weights1)) +\
                 regularizer(variable_averages.average(weights2))
loss = cross_entropy_mean + regularization
learning_rate = tf.train.exponential_decay(
    LEARNING_RATE_BASE,
    global_step,
    mnist.train.num_examples / BATCH_SIZE,
    LEARNING_RATE_DECAY
)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)
train_op = tf.group(train_step,variables_averages_op)


问题是当我使用average_y计算准确性时,训练似乎根本无济于事:

经过0个训练步骤后,验证的准确性为0.0742

经过1000个训练步骤,验证合格率为0.0924

经过2000培训步骤,验证合格率为0.0924

当我使用y而不是average_y时,一切都很好。这确实使我感到困惑:

经过0个训练步骤后,验证的准确性为0.0686

经过1000个训练步骤,验证合格率为0.9716

经过2000培训步骤,验证合格率为0.9768

#correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
correct_prediction = tf.equal(tf.arg_max(average_y,1),tf.arg_max(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    validate_feed = {
        x:mnist.validation.images,
        y_:mnist.validation.labels
    }
    test_feed={
        x:mnist.test.images,
        y_:mnist.test.labels
    }
    for i in range(TRAINING_STEPS):
        if i%1000 == 0:
            validate_acc = sess.run(accuracy,feed_dict=validate_feed)
            print("After %d training steps, acc in validatation is %g"%(i,validate_acc))
        xs,ys = mnist.train.next_batch(BATCH_SIZE)
        sess.run([train_op,global_step],feed_dict={x:xs,y_:ys})
    test_acc = sess.run(accuracy,feed_dict=test_feed)
    print("After %d training steps, acc in test is %g" % (TRAINING_STEPS, test_acc))

最佳答案

从代码段中,您正在针对y logits而不是average_y训练分类损失,因此实际上并未训练具有指数移动平均值的推理图

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.arg_max(y_,1))

关于python - Tensorflow:培训不能提高准确性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50829631/

10-13 03:07