深度學習入門:基於tensorflow 數字圖像識別nmist(單層和雙層網絡全連接)

值得學習的地方是:框架

一、數據

1、數據

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 這裏tensorflow自帶下載, 如果本地沒有, 自動下載並解析
#載入數據集
mnist = input_data.read_data_sets("../data/mnist_data",
                                  one_hot=True,
                                  reshape=True,) # 28, 28, 1 如果是true, 則爲28*28 1
print(mnist.train.num_examples)
print(mnist.test.num_examples)
print(mnist.train.images[0].shape)
print(mnist.train.num_examples) # 樣本個數
print(mnist.train.images.shape) # 樣本形狀
Extracting ../data/mnist_data\train-images-idx3-ubyte.gz
Extracting ../data/mnist_data\train-labels-idx1-ubyte.gz
Extracting ../data/mnist_data\t10k-images-idx3-ubyte.gz
Extracting ../data/mnist_data\t10k-labels-idx1-ubyte.gz
55000
10000
(784,)
55000
(55000, 784)

首先交叉熵的計算函數

2、普通神經網絡單層 0-9分類預測

在這裏插入圖片描述

# 2 函數用於清除默認圖形堆棧並重置全局默認圖形
tf.reset_default_graph()

# 輸入
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])

# 參數
W1 = tf.Variable(tf.random_normal([784,10], seed=180))
b1 = tf.Variable(tf.zeros(10))

# 正向
out1 = tf.matmul(x, W1)+b1
pred2 = tf.nn.softmax(out1)

# 反向 
# 交叉熵運算, 然後在取平均值
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y,logits = out1)  # 這裏一定要注意, 是輸出層前面的輸出
# loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred2), reduction_indices=1)) # 正經的交叉熵計算公式, 
                      
# 優化器, 最小損失函數
learning_rate=0.01 
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

#通過當前結果計算精度
correct_prediction = tf.equal(tf.argmax(pred2,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float32"))

epochs=25
batch_size=100
# sess = tf.Session()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    total_batch = int(mnist.train.num_examples/batch_size)
    print("總共有batch",total_batch,)

    for epoch in range(epochs): # 循環總次數
        avg_loss = 0
        for batch in range(total_batch):
            batch_input, batch_labels = mnist.train.next_batch(batch_size) #每次訓練的批數
            # 優化器優化
            _,train_loss = sess.run([optimizer,loss],feed_dict={x:batch_input,
                                                                y:batch_labels})
            # 計算loss值
            avg_loss += (train_loss/total_batch)

            # 顯示訓練數據
            # if i%total_batch == 0:
        train_accuracy = accuracy.eval(session=sess,feed_dict={x:batch_input,y:batch_labels})
        test_accuracy = accuracy.eval(session=sess,feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print(f"epoch={epoch} , training accuracy={train_accuracy:0.3f},avg_loss={avg_loss:0.3f}, test_accuracy={test_accuracy:0.3f}")

總共有batch 550
epoch=0 , training accuracy=0.220,avg_loss=8.379, test_accuracy=0.268
epoch=1 , training accuracy=0.440,avg_loss=4.287, test_accuracy=0.463
epoch=2 , training accuracy=0.630,avg_loss=2.905, test_accuracy=0.573
epoch=3 , training accuracy=0.630,avg_loss=2.294, test_accuracy=0.633
epoch=4 , training accuracy=0.570,avg_loss=1.954, test_accuracy=0.674
epoch=5 , training accuracy=0.730,avg_loss=1.735, test_accuracy=0.700
epoch=6 , training accuracy=0.710,avg_loss=1.582, test_accuracy=0.722
epoch=7 , training accuracy=0.740,avg_loss=1.467, test_accuracy=0.738
epoch=8 , training accuracy=0.780,avg_loss=1.378, test_accuracy=0.750
epoch=9 , training accuracy=0.760,avg_loss=1.306, test_accuracy=0.760
epoch=10 , training accuracy=0.770,avg_loss=1.246, test_accuracy=0.770
epoch=11 , training accuracy=0.740,avg_loss=1.195, test_accuracy=0.776
epoch=12 , training accuracy=0.850,avg_loss=1.151, test_accuracy=0.784
epoch=13 , training accuracy=0.760,avg_loss=1.113, test_accuracy=0.790
epoch=14 , training accuracy=0.860,avg_loss=1.079, test_accuracy=0.796
epoch=15 , training accuracy=0.840,avg_loss=1.049, test_accuracy=0.801
epoch=16 , training accuracy=0.780,avg_loss=1.022, test_accuracy=0.806
epoch=17 , training accuracy=0.810,avg_loss=0.998, test_accuracy=0.809
epoch=18 , training accuracy=0.780,avg_loss=0.975, test_accuracy=0.812
epoch=19 , training accuracy=0.850,avg_loss=0.955, test_accuracy=0.816
epoch=20 , training accuracy=0.810,avg_loss=0.936, test_accuracy=0.819
epoch=21 , training accuracy=0.790,avg_loss=0.918, test_accuracy=0.820
epoch=22 , training accuracy=0.780,avg_loss=0.902, test_accuracy=0.823
epoch=23 , training accuracy=0.770,avg_loss=0.887, test_accuracy=0.826
epoch=24 , training accuracy=0.860,avg_loss=0.872, test_accuracy=0.827

3、普通神經網絡雙層 0-9分類預測

# 輸入
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])

# 參數
W1 = tf.Variable(tf.random_normal([784,50]))
b1 = tf.Variable(tf.zeros(50, tf.float32))
W2 = tf.Variable(tf.random_normal([50,10]))
b2 = tf.Variable(tf.zeros(10, tf.float32))

# 正向
z1    = tf.nn.relu(tf.matmul(x, W1)+b1)
# pred2 = tf.nn.softmax(tf.matmul(z1, W2)+b2)
pred2 = (tf.matmul(z1, W2)+b2)
# 反向 
#損失值。指定tf.nn.softmax_cross_entropy_with_logits模塊下交叉熵損失函數,labels爲數據中的真實值,logits爲預測值
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y,
                                                              logits = pred2))
# 優化器, 最小損失函數
learning_rate=0.01 
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

#通過當前結果計算精度
correct_prediction = tf.equal(tf.argmax(pred2,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float32"))

epochs=25
batch_size=100
# sess = tf.Session()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    total_batch = int(mnist.train.num_examples/batch_size)
    print("總共有batch",total_batch,)

    for epoch in range(epochs): # 循環總次數
        avg_loss = 0
        for batch in range(total_batch):
            batch_input, batch_labels = mnist.train.next_batch(batch_size) #每次訓練的批數
            # 優化器優化
            _,train_loss = sess.run([optimizer,loss],feed_dict={x:batch_input,
                                                                y:batch_labels})
            # 計算loss值
            avg_loss += (train_loss/total_batch)

            # 顯示訓練數據
            # if i%total_batch == 0:
        train_accuracy = accuracy.eval(session=sess,feed_dict={x:batch_input,y:batch_labels})
        test_accuracy = accuracy.eval(session=sess,feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print(f"epoch={epoch} , training accuracy={train_accuracy:0.3f},avg_loss={avg_loss:0.3f}, test_accuracy={test_accuracy:0.3f}")
epoch=23 , training accuracy=0.960,avg_loss=0.063, test_accuracy=0.954
epoch=24 , training accuracy=0.990,avg_loss=0.061, test_accuracy=0.960

修改參數後的結果

學習率 0.001
relu,無:0.942,
relu,softmax:0.854
sigmod, softmax:0.855

學習率 0.01
relu,無:0.960
relu,softmax:0.868
sigmod, softmax:0.964

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章