Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tensorflow中的MNIST上训练具有一个隐层的全连通网络_Python_Machine Learning_Tensorflow_Neural Network - Fatal编程技术网

Python 在Tensorflow中的MNIST上训练具有一个隐层的全连通网络

Python 在Tensorflow中的MNIST上训练具有一个隐层的全连通网络,python,machine-learning,tensorflow,neural-network,Python,Machine Learning,Tensorflow,Neural Network,我刚刚开始使用Tensorflow进行机器学习,在完成MNIST初学者教程后,我想通过插入一个隐藏层来提高这个简单模型的准确性。本质上,我决定直接复制Micheal Nielsen关于神经网络和深度学习的书的第一章中的网络架构(参见) 尼尔森的代码对我来说很好,但是,使用下面的Tensorflow代码,我没有得到可比的结果。如果我没有弄错的话,它应该完全实现尼尔森提出的模式: from tensorflow.examples.tutorials.mnist import input_data i

我刚刚开始使用Tensorflow进行机器学习,在完成MNIST初学者教程后,我想通过插入一个隐藏层来提高这个简单模型的准确性。本质上,我决定直接复制Micheal Nielsen关于神经网络和深度学习的书的第一章中的网络架构(参见)

尼尔森的代码对我来说很好,但是,使用下面的Tensorflow代码,我没有得到可比的结果。如果我没有弄错的话,它应该完全实现尼尔森提出的模式:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

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


def weight_variable(shape):
    initial = tf.random_normal(shape)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.random_normal(shape)
    return tf.Variable(initial)


x = tf.placeholder(tf.float32, [None, 784])

#hidden layer
W_fc1 = weight_variable([784, 30])
b_fc1 = bias_variable([30])
h_fc1 = tf.sigmoid(tf.matmul(x, W_fc1) + b_fc1)

#output layer
W_fc2 = weight_variable([30, 10])
b_fc2 = bias_variable([10])
y = tf.sigmoid(tf.matmul(h_fc1, W_fc2) + b_fc2)

y_ = tf.placeholder(tf.float32, [None, 10])
loss = tf.reduce_mean(tf.reduce_sum(tf.pow(y_ - y, 2), reduction_indices=[1])) #I also tried simply tf.nn.l2_loss(y_ - y)
train_step = tf.train.GradientDescentOptimizer(3.0).minimize(loss)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

def get_accuracy():
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    return sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})

for i in range(30):
    batch_xs, batch_ys = mnist.train.next_batch(10)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    print("Epoch {} accuracy: {:.2f}%".format(i+1, get_accuracy() * 100))
经过30个阶段的训练,我的准确率达到了17%。使用尼尔森的代码,经过一个时期的训练,我的准确率达到91%

很明显我错过了什么。我曾试图提高准确率,并通过延长培训时间将其提高到60%左右,但同一网络应该会给出类似的结果,即使它可能使用不同的后端代码。我也尝试过使用超参数,但并没有得到任何可比的结果


您是否发现我的代码中有任何缺陷?

正如suharshs所提到的,您的问题似乎是由于对“时代”一词的误解造成的。尽管不是严格意义上的情况,一个历元通常是整个训练数据集上的一次迭代。如果你再看一看尼尔森的代码,你会发现这反映在SGD方法中。单个历元涉及迭代整个训练数据,这些数据分为小批量。您的每个历元实际上都是一个小批量的大小,只有10个样本。

正如Suharhs所提到的,您的问题似乎是由于对历元一词的误解造成的。尽管不是严格意义上的情况,一个历元通常是整个训练数据集上的一次迭代。如果你再看一看尼尔森的代码,你会发现这反映在SGD方法中。单个历元涉及迭代整个训练数据,这些数据分为小批量。您的每个时代实际上都是一个小批量的大小,只有10个样本。

30个步骤并不多。那么,您只需浏览300个示例。尝试为for循环运行1000个步骤。我认为它将达到80%到90%。30步并不多。那么,您只需浏览300个示例。尝试为for循环运行1000个步骤。我认为会达到80%到90%。