1. 程式人生 > >Tensorflow--MNIST手寫資料集全連線層分類

Tensorflow--MNIST手寫資料集全連線層分類

手寫資料集分類一般都被用來當做tensorflow入門的教程。當然啦,神經網路一般分為全連線層(FC),卷積層(CNN)和序列模型(RNN),這裡先用全連線層做一個分類。

我就把之前寫的程式碼貼上來吧。

# 用tensorflow 匯入資料
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# 權值初始化
def weight_variable(shape):
    # 用正態分佈來初始化權值
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    # 本例中用relu啟用函式,所以用一個很小的正偏置較好
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


# input_layer
X_ = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])

# FC1
W_fc1 = weight_variable([784, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(X_, W_fc1) + b_fc1)

# FC2
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_pre = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
# 1.損失函式:cross_entropy
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_pre)) # y_ 中只有標籤所在的那一類是 1, 其餘全部都是0.
# 2.優化函式:AdamOptimizer, 優化速度要比 GradientOptimizer 快很多
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

# 3.預測結果評估
# 預測值中最大值(1)即分類結果,是否等於原始標籤中的(1)的位置。argmax()取最大值所在的下標
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.arg_max(y_, 1))  
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 開始執行
sess.run(tf.global_variables_initializer())
# 這大概迭代了不到 10 個 epoch, 訓練準確率已經達到了0.98
for i in range(5000):
    X_batch, y_batch = mnist.train.next_batch(batch_size=100)
    train_step.run(feed_dict={X_: X_batch, y_: y_batch})
    if (i+1) % 200 == 0:
        train_accuracy = accuracy.eval(feed_dict={X_: mnist.train.images, y_: mnist.train.labels})
        print "step %d, training acc %g" % (i+1, train_accuracy)
    if (i+1) % 1000 == 0:
        test_accuracy = accuracy.eval(feed_dict={X_: mnist.test.images, y_: mnist.test.labels})
        print "= " * 10, "step %d, testing acc %g" % (i+1, test_accuracy)