Tensorflow實現多層感知機Multi-layer Preceptron

# coding:utf-8
'''
使用Tensorflow訓練神經網絡的4個步驟
①定義算法公式 即神經網絡的forward時的計算
②定義損失函數和選擇優化器來優化loss
③訓練步驟
④對模型進行準確率評測

隱含層:解決XOR問題
神經網絡的隱藏層越多就可以對原特徵進行越抽象的變換 模型的擬合能力越強

Tensorflow實現多層感知機Multi-layer Preceptron
'''

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

mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
# 創建一個Tensorlfow默認的session 後面的操作就無需指定Session
sess = tf.InteractiveSession()

in_units = 784
h1_units = 300
# 將權重初始化爲截斷的正態分佈 標準差爲0.1 正態分佈給參數加一些噪聲 打破完全對稱且避免0梯度
w1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1))
b1 = tf.Variable(tf.zeros([h1_units]), dtype=tf.float32)
w2 = tf.Variable(tf.zeros([h1_units,10]), dtype=tf.float32)
b2 = tf.Variable(tf.zeros([10]), dtype=tf.float32)

x = tf.placeholder(tf.float32, [None, in_units])
keep_prob = tf.placeholder(tf.float32) #dropout的比率 通常訓練小於1 測試等於1

# 定義模型結構
hidden1 = tf.nn.relu(tf.matmul(x, w1) + b1) #激活函數爲relu的隱藏層
hidden1_drop = tf.nn.dropout(hidden1, keep_prob) #dropout防止過擬合
y = tf.nn.softmax(tf.matmul(hidden1_drop, w2) + b2) #softmax輸出層

# 定義損失函數和選擇優化器來優化loss
y_ = tf.placeholder(tf.float32, [None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) #交叉熵
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)

# 訓練步驟
tf.global_variables_initializer().run()
for i in range(3000):
	batch_xs, batch_ys = mnist.train.next_batch(100)
	train_step.run({x:batch_xs, y_:batch_ys, keep_prob:0.75})

# 對模型進行準確率評測
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0}))


沒有隱含層的softmax regression只能直接從圖像的像素點推斷是哪個數字,沒有特徵抽象的過程。

多層神經網絡依靠隱含層,可以組合高階特徵



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