1. 程式人生 > >卷積神經網絡識別手寫數字實例

卷積神經網絡識別手寫數字實例

取出 return hot lte git tensor on() port cross

卷積神經網絡識別手寫數字實例:

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


# 定義一個初始化權重的函數
def weight_variables(shape):
    w = tf.Variable(tf.random_normal(shape=shape,mean=0.0,stddev=1.0))
    return w


# 定義一個初始化偏置的函數
def bias_variables(shape):
    b = tf.Variable(tf.constant(0.0,shape=shape))
    
return b def model(): ‘‘‘ 自定義的卷積模型 :return: ‘‘‘ # 1.準備數據的占位符 x [None,784] y_ture [None,10] with tf.variable_scope(data): x = tf.placeholder(tf.float32,[None,784]) y_true = tf.placeholder(tf.int32,[None,10]) # 2. 一卷積層 卷積:5*5*1,,32個,strides=1 激活:tf.nn.relu 池化
with tf.variable_scope(conv1): # 隨機初始化權重 偏置[32] w_conv1 = weight_variables([5,5,1,32]) b_conv1 = bias_variables([32]) # 對x進行形狀的改變[None,784] [None,28,28,1] x_reshape = tf.reshape(x,[-1,28,28,1]) # [None,28,28,1]---->[None,28,28,32] x_relu1 = tf.nn.relu(tf.nn.conv2d(x_reshape,w_conv1,strides=[1,1,1,1],padding=
SAME) + b_conv1) # 池化 2*2 , strides2 [None,28,28,32]---->[None,14,14,32] x_pool1 = tf.nn.max_pool(x_relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding=SAME) # 3. 二卷積層 卷積:5*5*32 64個filter,strides=1 激活:tf.nn.relu 池化: with tf.variable_scope(conv2): # 隨機初始化權重 權重:[5,5,32,64] 偏置[64] w_conv2 = weight_variables([5,5,32,64]) b_conv2 = bias_variables([64]) # 卷積,激活,池化計算 # [None,14,14,32]---->[None,14,14,64] x_relu2 = tf.nn.relu(tf.nn.conv2d(x_pool1,w_conv2,strides=[1,1,1,1],padding=SAME) + b_conv2) # 池化 2*2 strides 2,[None,14,14,64]--->[None,7,7,64] x_pool2 = tf.nn.max_pool(x_relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding=SAME) # 4. 全連接層 [None,7,7,64]--->[None,7*7*64]*[7*7*64,10] + [10] = [None,10] with tf.variable_scope(conv2): # 隨機初始化權重和偏置 w_fc = weight_variables([7*7*64,10]) b_fc = bias_variables([10]) # 修改形狀 [None,7,7,64] --->[None,7*7*64] x_fc_reshape = tf.reshape(x_pool2,[-1,7*7*64]) # 進行矩陣運算得出每個樣本的10個結果 y_predict = tf.matmul(x_fc_reshape,w_fc) + b_fc return x,y_true,y_predict def conv_fc(): # 1. 獲取真實數據 mnist = input_data.read_data_sets(./data/mnist/,one_hot=True) # 2. 定義模型,得出輸出 x,y_true,y_predict = model() # 進行交叉熵損失計算 # 3. 求出所有樣本的損失,然後求平均值 with tf.variable_scope(soft_cross): # 求平均交叉熵損失 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=y_predict)) # 4. 梯度下降求出損失 with tf.variable_scope(optimizer): train_op = tf.train.GradientDescentOptimizer(0.0001).minimize((loss)) # 5. 計算準確率 with tf.variable_scope(acc): equal_list = tf.equal(tf.argmax(y_true,1),tf.argmax(y_predict,1)) # equal_list None個樣本 [1,0,1,0,0,0,1,1,...] accracy = tf.reduce_mean(tf.cast(equal_list,tf.float32)) # 定義一個初始化變量的op init_op = tf.global_variables_initializer() # 開啟會話運行 with tf.Session() as sess: sess.run(init_op) # 循環去訓練 for i in range(1000): # 取出真實存在的特征值和目標值 mnist_x,mnist_y = mnist.train.next_batch(50) # 運行train_op訓練 sess.run(train_op,feed_dict={x:mnist_x,y_true:mnist_y}) print(訓練第%d步,準確率為:%f % (i,sess.run(accracy,feed_dict={x:mnist_x,y_true:mnist_y}))) return None if __name__ == __main__: conv_fc()

卷積神經網絡識別手寫數字實例