首先是不含隱層的神經網絡, 輸入層是784個神經元 輸出層是10個神經元
代碼如下
#coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #載入數據集 mnist = input_data.read_data_sets("MNIST_data", one_hot=True) #每個批次的大小 batch_size = 100 #計算一共有多少個批次 n_batch = mnist.train.num_examples // batch_size #定義兩個placeholder x = tf.placeholder(tf.float32, [None,784]) #輸入圖像 y = tf.placeholder(tf.float32, [None,10]) #輸入標簽 #創建一個簡單的神經網絡 784個像素點對應784個數 因此輸入層是784個神經元 輸出層是10個神經元 不含隱層 #最后准確率在92%左右 W = tf.Variable(tf.zeros([784,10])) #生成784行 10列的全0矩陣 b = tf.Variable(tf.zeros([1,10])) prediction = tf.nn.softmax(tf.matmul(x,W)+b) #二次代價函數 loss = tf.reduce_mean(tf.square(y-prediction)) #使用梯度下降法 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化變量 init = tf.global_variables_initializer() #結果存放在布爾型列表中 #argmax能給出某個tensor對象在某一維上的其數據最大值所在的索引值 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction,1)) #求准確率 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess: sess.run(init) for epoch in range(21): #21個epoch 把所有的圖片訓練21次 for batch in range(n_batch): # batch_xs,batch_ys = mnist.train.next_batch(batch_size) sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels}) print ("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
結果如下
Iter 0,Testing Accuracy 0.8304 Iter 1,Testing Accuracy 0.8704 Iter 2,Testing Accuracy 0.8821 Iter 3,Testing Accuracy 0.8876 Iter 4,Testing Accuracy 0.8932 Iter 5,Testing Accuracy 0.8968 Iter 6,Testing Accuracy 0.8995 Iter 7,Testing Accuracy 0.9019 Iter 8,Testing Accuracy 0.9033 Iter 9,Testing Accuracy 0.9048 Iter 10,Testing Accuracy 0.9065 Iter 11,Testing Accuracy 0.9074 Iter 12,Testing Accuracy 0.9084 Iter 13,Testing Accuracy 0.909 Iter 14,Testing Accuracy 0.9094 Iter 15,Testing Accuracy 0.9112 Iter 16,Testing Accuracy 0.9117 Iter 17,Testing Accuracy 0.9128 Iter 18,Testing Accuracy 0.9127 Iter 19,Testing Accuracy 0.9132 Iter 20,Testing Accuracy 0.9144
接下來是含一個隱層的神經網絡,輸入層是784個神經元,兩個隱層都是100個神經元,輸出層是10個神經元,迭代500次,最后准確率在88%左右,汗。。。。准確率反而降低了,慢慢調參吧
#coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #載入數據集 mnist = input_data.read_data_sets("MNIST_data", one_hot=True) #每個批次的大小 batch_size = 50 #計算一共有多少個批次 n_batch = mnist.train.num_examples // batch_size #定義兩個placeholder x = tf.placeholder(tf.float32, [None,784]) #輸入圖像 y = tf.placeholder(tf.float32, [None,10]) #輸入標簽 #定義神經網絡中間層 Weights_L1 = tf.Variable(tf.random_normal([784,100])) biase_L1 = tf.Variable(tf.zeros([1,100])) Wx_plus_b_L1 = tf.matmul(x, Weights_L1)+biase_L1 L1 = tf.nn.tanh(Wx_plus_b_L1) #使用正切函數作為激活函數 Weights_L2 = tf.Variable(tf.random_normal([100,100])) biase_L2 = tf.Variable(tf.zeros([1,100])) Wx_plus_b_L2 = tf.matmul(L1, Weights_L2)+biase_L2 L2 = tf.nn.tanh(Wx_plus_b_L2) #使用正切函數作為激活函數 #定義神經網絡輸出層 Weights_L3 = tf.Variable(tf.random_normal([100,10])) biase_L3 = tf.Variable(tf.zeros([1,10])) Wx_plus_b_L3 = tf.matmul(L2,Weights_L3) + biase_L3 prediction = tf.nn.tanh(Wx_plus_b_L3) #二次代價函數 loss = tf.reduce_mean(tf.square(y-prediction)) #使用梯度下降法 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化變量 init = tf.global_variables_initializer() #結果存放在布爾型列表中 #argmax能給出某個tensor對象在某一維上的其數據最大值所在的索引值 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction,1)) #求准確率 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess: sess.run(init) for epoch in range(500): for batch in range(n_batch): batch_xs,batch_ys = mnist.train.next_batch(batch_size) sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels}) print ("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
Iter 487,Testing Accuracy 0.8847 Iter 488,Testing Accuracy 0.8853 Iter 489,Testing Accuracy 0.878 Iter 490,Testing Accuracy 0.8861 Iter 491,Testing Accuracy 0.8863 Iter 492,Testing Accuracy 0.8784 Iter 493,Testing Accuracy 0.8855 Iter 494,Testing Accuracy 0.8787 Iter 495,Testing Accuracy 0.881 Iter 496,Testing Accuracy 0.8837 Iter 497,Testing Accuracy 0.8817 Iter 498,Testing Accuracy 0.8837 Iter 499,Testing Accuracy 0.8866