mnist實例--用簡單的神經(jīng)網(wǎng)絡(luò)來訓(xùn)練和測試
mnist實例--卷積神經(jīng)網(wǎng)絡(luò)CNN
簡單神經(jīng)網(wǎng)絡(luò)沒有卷積功能,只有簡單的三層:輸入層瞬哼,隱藏層和輸出層婚肆。
數(shù)據(jù)從輸入層輸入,在隱藏層進行加權(quán)變換坐慰,最后在輸出層進行輸出旬痹。輸出的時候,我們可以使用softmax回歸讨越,輸出屬于每個類別的概率值两残。表示如下:
其中,x1,x2,x3為輸入數(shù)據(jù)把跨,經(jīng)過運算后人弓,得到三個數(shù)據(jù)屬于某個類別的概率值y1,y2,y3. 用簡單的公式表示如下:
在訓(xùn)練過程中,我們將真實的結(jié)果和預(yù)測的結(jié)果相比(交叉熵比較法)着逐,會得到一個殘差崔赌。公式如下:
y 是我們預(yù)測的概率值, y' 是實際的值。這個殘差越小越好耸别,我們可以使用梯度下降法健芭,不停地改變W和b的值,使得殘差逐漸變小秀姐,最后收斂到最小值慈迈。這樣訓(xùn)練就完成了,我們就得到了一個模型(W和b的最優(yōu)化值)省有。
簡單神經(jīng)網(wǎng)絡(luò)的訓(xùn)練例子(非卷積):
# -*- coding: utf-8 -*-
"""
@author: gongjia copy
"""
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_actual = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10])) #初始化權(quán)值W
b = tf.Variable(tf.zeros([10])) #初始化偏置項b
y_predict = tf.nn.softmax(tf.matmul(x,W) + b) #加權(quán)變換并進行softmax回歸痒留,得到預(yù)測概率
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_actual*tf.log(y_predict),reduction_indies=1)) #求交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) #用梯度下降法使得殘差最小
correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1)) #在測試階段,測試準確度計算
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) #多個批次的準確度均值
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(1000): #訓(xùn)練階段蠢沿,迭代1000次
batch_xs, batch_ys = mnist.train.next_batch(100) #按批次訓(xùn)練伸头,每批100行數(shù)據(jù)
sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys}) #執(zhí)行訓(xùn)練
if(i%100==0): #每訓(xùn)練100次,測試一次
print "accuracy:",sess.run(accuracy, feed_dict={x: mnist.test.images, y_actual: mnist.test.labels})
每訓(xùn)練100次舷蟀,測試一次恤磷,隨著訓(xùn)練次數(shù)的增加,測試精度也在增加野宜。訓(xùn)練結(jié)束后扫步,1W行數(shù)據(jù)測試的平均精度為91%左右,不是太高速缨,肯定沒有CNN高锌妻。
# -*- coding: utf-8 -*-
"""
@author: gongjia copy
"""
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #下載并加載mnist數(shù)據(jù)
x = tf.placeholder(tf.float32, [None, 784]) #輸入的數(shù)據(jù)占位符
y_actual = tf.placeholder(tf.float32, shape=[None, 10]) #輸入的標簽占位符
#定義一個函數(shù),用于初始化所有的權(quán)值 W
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
#定義一個函數(shù)旬牲,用于初始化所有的偏置項 b
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#定義一個函數(shù)仿粹,用于構(gòu)建卷積層
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
#定義一個函數(shù)搁吓,用于構(gòu)建池化層
def max_pool(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
#構(gòu)建網(wǎng)絡(luò)
x_image = tf.reshape(x, [-1,28,28,1]) #轉(zhuǎn)換輸入數(shù)據(jù)shape,以便于用于網(wǎng)絡(luò)中
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) #第一個卷積層
h_pool1 = max_pool(h_conv1) #第一個池化層
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) #第二個卷積層
h_pool2 = max_pool(h_conv2) #第二個池化層
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) #reshape成向量
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) #第一個全連接層
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) #dropout層
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_predict=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) #softmax層
cross_entropy = -tf.reduce_sum(y_actual*tf.log(y_predict)) #交叉熵
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy) #梯度下降法
correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) #精確度計算
sess=tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0: #訓(xùn)練100次,驗證一次
train_acc = accuracy.eval(feed_dict={x:batch[0], y_actual: batch[1], keep_prob: 1.0})
print('step',i,'training accuracy',train_acc)
train_step.run(feed_dict={x: batch[0], y_actual: batch[1], keep_prob: 0.5})
test_acc=accuracy.eval(feed_dict={x: mnist.test.images, y_actual: mnist.test.labels, keep_prob: 1.0})
print("test accuracy",test_acc)
('step', 18800, 'training accuracy', 0.079999998)
('step', 18900, 'training accuracy', 0.059999999)
('step', 19000, 'training accuracy', 0.1)
('step', 19100, 'training accuracy', 0.059999999)
('step', 19200, 'training accuracy', 0.12)
('step', 19300, 'training accuracy', 0.14)
('step', 19400, 'training accuracy', 0.079999998)
('step', 19500, 'training accuracy', 0.039999999)
('step', 19600, 'training accuracy', 0.16)
('step', 19700, 'training accuracy', 0.1)
('step', 19800, 'training accuracy', 0.079999998)
('step', 19900, 'training accuracy', 0.1)
('test accuracy', 0.097999997)