国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

tensorflow學習筆記之mnist的卷積神經網絡實例

2020-02-22 23:40:13
字體:
來源:轉載
供稿:網友

mnist的卷積神經網絡例子和上一篇博文中的神經網絡例子大部分是相同的。但是CNN層數要多一些,網絡模型需要自己來構建。

程序比較復雜,我就分成幾個部分來敘述。

首先,下載并加載數據:

import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)   #下載并加載mnist數據x = tf.placeholder(tf.float32, [None, 784])            #輸入的數據占位符y_actual = tf.placeholder(tf.float32, shape=[None, 10])      #輸入的標簽占位符

定義四個函數,分別用于初始化權值W,初始化偏置項b, 構建卷積層和構建池化層。

#定義一個函數,用于初始化所有的權值 Wdef weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial)#定義一個函數,用于初始化所有的偏置項 bdef bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) #定義一個函數,用于構建卷積層def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')#定義一個函數,用于構建池化層def max_pool(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')

接下來構建網絡。整個網絡由兩個卷積層(包含激活層和池化層),一個全連接層,一個dropout層和一個softmax層組成。

#構建網絡x_image = tf.reshape(x, [-1,28,28,1])     #轉換輸入數據shape,以便于用于網絡中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:         #訓練100次,驗證一次  train_acc = accuracy.eval(feed_dict={x:batch[0], y_actual: batch[1], keep_prob: 1.0})  print 'step %d, training accuracy %g'%(i,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 %g"%test_acc            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大石桥市| 昭平县| 镇原县| 台中县| 涟水县| 科尔| 马公市| 达孜县| 阜城县| 东兰县| 芷江| 金溪县| 黑龙江省| 承德市| 夏津县| 文山县| 新营市| 北票市| 额尔古纳市| 四子王旗| 玉田县| 新河县| 奉节县| 广水市| 田阳县| 南澳县| 岚皋县| 陆河县| 东台市| 监利县| 黔南| 莱阳市| 四平市| 凌云县| 盘锦市| 綦江县| 邵阳县| 泗水县| 天台县| 鄂伦春自治旗| 新安县|