【问题标题】:Unable to generate accurate result from mnist dataset无法从 mnist 数据集生成准确的结果
【发布时间】:2017-06-22 11:06:38
【问题描述】:

我一直在练习机器学习,并且遇到了 mnist 教程。在学习的过程中,我制作了这段代码。

` 将张量流导入为 tf 从 tensorflow.examples.tutorials.mnist 导入 input_data 将 numpy 导入为 np

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

n_hidden_layer_1 = 500
n_hidden_layer_2 = 500
n_hidden_layer_3 = 500

n_classes = 10
batch_size = 100

x = tf.placeholder('float', shape = [None, 784])
y = tf.placeholder('float')

hidden_layer_1 = {
    'weights': tf.Variable(tf.random_normal(shape = [784, n_hidden_layer_1])),
    'bias': tf.Variable(tf.random_normal(shape = [n_hidden_layer_1]))
}

hidden_layer_2 = {
    'weights': tf.Variable(tf.random_normal(shape = [n_hidden_layer_1, n_hidden_layer_2])),
    'bias': tf.Variable(tf.random_normal(shape = [n_hidden_layer_2]))
}

hidden_layer_3 = {
    'weights': tf.Variable(tf.random_normal(shape = [n_hidden_layer_2, n_hidden_layer_3])),
    'bias': tf.Variable(tf.random_normal(shape = [n_hidden_layer_3]))
}

output_layer = {
    'weights': tf.Variable(tf.random_normal(shape = [n_hidden_layer_3, n_classes])),
    'bias': tf.Variable(tf.random_normal(shape = [n_classes]))
}

hidden_layer_1_output = tf.nn.relu(tf.add(tf.matmul(x, hidden_layer_1['weights']), hidden_layer_1['bias']))
hidden_layer_2_output = tf.nn.relu(tf.add(tf.matmul(hidden_layer_1_output, hidden_layer_2['weights']), hidden_layer_2['bias']))
hidden_layer_3_output = tf.nn.relu(tf.add(tf.matmul(hidden_layer_2_output, hidden_layer_3['weights']), hidden_layer_3['bias']))
final_output = tf.nn.relu(tf.add(tf.matmul(hidden_layer_3_output, output_layer['weights']), output_layer['bias']))

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=final_output, labels=y))
model = tf.train.AdamOptimizer().minimize(cost)

epochs = 10

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(epochs):
        epoch_loss = 0
        for _ in range(mnist.train.num_examples/batch_size):
            P,Q = mnist.train.next_batch(batch_size)
            _,c = sess.run([model, cost], feed_dict = {x:P, y:Q})
            epoch_loss+=c

        print("Epoch no:",i,"Epoch_loss:",epoch_loss)
    correct = tf.equal(tf.argmax(final_output,1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct, 'float'))    
    print("accuracy: ",accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))

生成的结果是

Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
('Epoch no:', 0, 'Epoch_loss:', 265771.25100541115)
('Epoch no:', 1, 'Epoch_loss:', 1310.440309047699)
('Epoch no:', 2, 'Epoch_loss:', 1262.8069067001343)
('Epoch no:', 3, 'Epoch_loss:', 1262.8069069385529)
('Epoch no:', 4, 'Epoch_loss:', 1262.8069067001343)
('Epoch no:', 5, 'Epoch_loss:', 1262.8069069385529)
('Epoch no:', 6, 'Epoch_loss:', 1262.8069067001343)
('Epoch no:', 7, 'Epoch_loss:', 1262.8069067001343)
('Epoch no:', 8, 'Epoch_loss:', 1262.8069064617157)
('Epoch no:', 9, 'Epoch_loss:', 1262.8069064617157)
('accuracy: ', 0.1008)

您能否告诉我这段代码中我的结果不准确的可能原因以及如何改进它?

【问题讨论】:

  • 我打赌它非常没有足够的时代。我认为你应该瞄准数百个。

标签: python machine-learning tensorflow mnist


【解决方案1】:

您的代码有几个问题:

  1. 删除 final_output 上的 relu 激活。 softmax_cross_entropy_with_logits 将对您的 final_output 应用 softmax 激活。

    final_output = tf.add(tf.matmul(hidden_layer_3_output, output_layer['weights']), output_layer['bias']) 
    
  2. 将权重的标准偏差设置为较低的值。

    'weights': tf.Variable(tf.random_normal(shape = [784, n_hidden_layer_1], stddev=0.005))
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 2019-08-27
    • 1970-01-01
    • 2019-05-14
    • 2020-04-14
    • 2017-06-03
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多