【问题标题】:Adding more layers to tensorflow MNIST tutorial makes accuracy drop and sometimes accuracy remains constant over iteration for batch在 tensorflow MNIST 教程中添加更多层会导致精度下降,有时精度在批次迭代中保持不变
【发布时间】:2019-02-19 00:23:06
【问题描述】:

我正在查看本教程以了解深度学习,他制作了一个带有一个隐藏层的简单神经网络。我做了同样的事情,它工作正常(准确度 94%),现在我增加了一层,它的准确度下降到(10%)我不知道为什么? 下面是我的代码

`import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data
sess = tf.InteractiveSession()
mnist  = input_data.read_data_sets("MNIST_data/",one_hot=True)

input_images = tf.placeholder(tf.float32,shape=[None,784])
target_labels = tf.placeholder(tf.float32,shape=[None,10])

hidden_nodes1 = 512
hidden_nodes2 = 256

    input_weights = tf.Variable(tf.truncated_normal([784,hidden_nodes1]))
    input_biases = tf.Variable(tf.zeros([hidden_nodes1]))


    hidden_weights1 = tf.Variable(tf.truncated_normal([hidden_nodes1,hidden_nodes2]))
    hidden_biases1 = tf.Variable(tf.zeros([hidden_nodes2]))

    hidden_weights2 = tf.Variable(tf.truncated_normal([hidden_nodes2,10]))
    hidden_biases2 = tf.Variable(tf.zeros([10]))


    input_layer = tf.matmul(input_images,input_weights)
    hidden_layer1 = tf.nn.relu(input_layer + input_biases)

    hidden_layer2 = tf.nn.relu(tf.matmul(hidden_layer1,hidden_weights1) + hidden_biases1)


    digits_weights = tf.matmul(hidden_layer2,hidden_weights2)+hidden_biases2



    loss_funtion = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=digits_weights,labels=target_labels))

    optimizer = tf.train.GradientDescentOptimizer(0.2).minimize(loss_funtion)

    correct_prediction = tf.equal(tf.argmax(digits_weights,1),tf.argmax(target_labels,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    tf.global_variables_initializer().run()
    for x in range(2000):
        batch = mnist.train.next_batch(100)
        optimizer.run(feed_dict={input_images:batch[0],target_labels:batch[1]})
        if ((x+1)%100==0):
            print("Training Epoc"+str(x+1))
            print("Accuracy"+str(accuracy.eval(feed_dict={input_images:mnist.test.images,target_labels:mnist.test.labels})))`

【问题讨论】:

    标签: tensorflow machine-learning neural-network deep-learning computer-vision


    【解决方案1】:

    您的代码实际上很好。但是,通过添加一个带有256 节点的新隐藏层,可以显着增加可学习参数的数量!本质上,您的模型架构已经变得太大了。这就是我的建议,您可以将节点数量从512 和256 减少到128 或最大256 两者。然后,使用低得多的学习率值,因为当前学习率太高,可能无法正确收敛到最小值(甚至可能发散)。所以,我会把它改成0.01 甚至更低。您可以尝试的另一件事是使用AdamOptimizer 而不是GradientDescentOptimizer。试试这些,代码应该可以正常工作!

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 2017-09-20
      • 1970-01-01
      • 2017-07-04
      • 2017-01-02
      • 2018-05-21
      • 1970-01-01
      • 2019-07-31
      • 2017-08-31
      相关资源
      最近更新 更多