tensorflow使用CNN分析mnist手寫(xiě)體數(shù)字?jǐn)?shù)據(jù)集
更新時(shí)間:2020年06月17日 10:16:40 作者:Dillon2015
這篇文章主要介紹了tensorflow使用CNN分析mnist手寫(xiě)體數(shù)字?jǐn)?shù)據(jù)集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了tensorflow使用CNN分析mnist手寫(xiě)體數(shù)字?jǐn)?shù)據(jù)集,供大家參考,具體內(nèi)容如下
import tensorflow as tf import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels #把上述trX和teX的形狀變?yōu)閇-1,28,28,1],-1表示不考慮輸入圖片的數(shù)量,28×28是圖片的長(zhǎng)和寬的像素?cái)?shù), # 1是通道(channel)數(shù)量,因?yàn)镸NIST的圖片是黑白的,所以通道是1,如果是RGB彩色圖像,通道是3。 trX = trX.reshape(-1, 28, 28, 1) # 28x28x1 input img teX = teX.reshape(-1, 28, 28, 1) # 28x28x1 input img X = tf.placeholder("float", [None, 28, 28, 1]) Y = tf.placeholder("float", [None, 10]) #初始化權(quán)重與定義網(wǎng)絡(luò)結(jié)構(gòu)。 # 這里,我們將要構(gòu)建一個(gè)擁有3個(gè)卷積層和3個(gè)池化層,隨后接1個(gè)全連接層和1個(gè)輸出層的卷積神經(jīng)網(wǎng)絡(luò) def init_weights(shape): return tf.Variable(tf.random_normal(shape, stddev=0.01)) w = init_weights([3, 3, 1, 32]) # patch大小為3×3,輸入維度為1,輸出維度為32 w2 = init_weights([3, 3, 32, 64]) # patch大小為3×3,輸入維度為32,輸出維度為64 w3 = init_weights([3, 3, 64, 128]) # patch大小為3×3,輸入維度為64,輸出維度為128 w4 = init_weights([128 * 4 * 4, 625]) # 全連接層,輸入維度為 128 × 4 × 4,是上一層的輸出數(shù)據(jù)又三維的轉(zhuǎn)變成一維, 輸出維度為625 w_o = init_weights([625, 10]) # 輸出層,輸入維度為 625, 輸出維度為10,代表10類(lèi)(labels) # 神經(jīng)網(wǎng)絡(luò)模型的構(gòu)建函數(shù),傳入以下參數(shù) # X:輸入數(shù)據(jù) # w:每一層的權(quán)重 # p_keep_conv,p_keep_hidden:dropout要保留的神經(jīng)元比例 def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden): # 第一組卷積層及池化層,最后dropout一些神經(jīng)元 l1a = tf.nn.relu(tf.nn.conv2d(X, w, strides=[1, 1, 1, 1], padding='SAME')) # l1a shape=(?, 28, 28, 32) l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # l1 shape=(?, 14, 14, 32) l1 = tf.nn.dropout(l1, p_keep_conv) # 第二組卷積層及池化層,最后dropout一些神經(jīng)元 l2a = tf.nn.relu(tf.nn.conv2d(l1, w2, strides=[1, 1, 1, 1], padding='SAME')) # l2a shape=(?, 14, 14, 64) l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # l2 shape=(?, 7, 7, 64) l2 = tf.nn.dropout(l2, p_keep_conv) # 第三組卷積層及池化層,最后dropout一些神經(jīng)元 l3a = tf.nn.relu(tf.nn.conv2d(l2, w3, strides=[1, 1, 1, 1], padding='SAME')) # l3a shape=(?, 7, 7, 128) l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # l3 shape=(?, 4, 4, 128) l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]]) # reshape to (?, 2048) l3 = tf.nn.dropout(l3, p_keep_conv) # 全連接層,最后dropout一些神經(jīng)元 l4 = tf.nn.relu(tf.matmul(l3, w4)) l4 = tf.nn.dropout(l4, p_keep_hidden) # 輸出層 pyx = tf.matmul(l4, w_o) return pyx #返回預(yù)測(cè)值 #我們定義dropout的占位符——keep_conv,它表示在一層中有多少比例的神經(jīng)元被保留下來(lái)。生成網(wǎng)絡(luò)模型,得到預(yù)測(cè)值 p_keep_conv = tf.placeholder("float") p_keep_hidden = tf.placeholder("float") py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden) #得到預(yù)測(cè)值 #定義損失函數(shù),這里我們?nèi)匀徊捎胻f.nn.softmax_cross_entropy_with_logits來(lái)比較預(yù)測(cè)值和真實(shí)值的差異,并做均值處理; # 定義訓(xùn)練的操作(train_op),采用實(shí)現(xiàn)RMSProp算法的優(yōu)化器tf.train.RMSPropOptimizer,學(xué)習(xí)率為0.001,衰減值為0.9,使損失最小; # 定義預(yù)測(cè)的操作(predict_op) cost = tf.reduce_mean(tf.nn. softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost) predict_op = tf.argmax(py_x, 1) #定義訓(xùn)練時(shí)的批次大小和評(píng)估時(shí)的批次大小 batch_size = 128 test_size = 256 #在一個(gè)會(huì)話中啟動(dòng)圖,開(kāi)始訓(xùn)練和評(píng)估 # Launch the graph in a session with tf.Session() as sess: # you need to initialize all variables tf. global_variables_initializer().run() for i in range(100): training_batch = zip(range(0, len(trX), batch_size), range(batch_size, len(trX)+1, batch_size)) for start, end in training_batch: sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end], p_keep_conv: 0.8, p_keep_hidden: 0.5}) test_indices = np.arange(len(teX)) # Get A Test Batch np.random.shuffle(test_indices) test_indices = test_indices[0:test_size] print(i, np.mean(np.argmax(teY[test_indices], axis=1) == sess.run(predict_op, feed_dict={X: teX[test_indices], p_keep_conv: 1.0, p_keep_hidden: 1.0})))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PyTorch使用Tricks:Dropout,R-Dropout和Multi-Sample?Dropout方式
這篇文章主要介紹了PyTorch使用Tricks:Dropout,R-Dropout和Multi-Sample?Dropout方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09python使用正則表達(dá)式匹配txt特定字符串(有換行)
這篇文章主要給大家介紹了關(guān)于python使用正則表達(dá)式匹配txt特定字符串的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12python使用beautifulsoup4爬取酷狗音樂(lè)代碼實(shí)例
這篇文章主要介紹了python使用beautifulsoup4爬取酷狗音樂(lè)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12