最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFlow教程Softmax邏輯回歸識(shí)別手寫(xiě)數(shù)字MNIST數(shù)據(jù)集

 更新時(shí)間:2021年11月03日 17:02:05   作者:零尾  
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)的TensorFlow教程基于Softmax邏輯回歸識(shí)別手寫(xiě)數(shù)字的MNIST數(shù)據(jù)集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助

基于MNIST數(shù)據(jù)集的邏輯回歸模型做十分類(lèi)任務(wù)

沒(méi)有隱含層的Softmax Regression只能直接從圖像的像素點(diǎn)推斷是哪個(gè)數(shù)字,而沒(méi)有特征抽象的過(guò)程。多層神經(jīng)網(wǎng)絡(luò)依靠隱含層,則可以組合出高階特征,比如橫線、豎線、圓圈等,之后可以將這些高階特征或者說(shuō)組件再組合成數(shù)字,就能實(shí)現(xiàn)精準(zhǔn)的匹配和分類(lèi)。

import tensorflow as tf
import numpy as np
import input_data
print('Download and Extract MNIST dataset')
mnist = input_data.read_data_sets('data/', one_hot=True) # one_hot=True意思是編碼格式為01編碼
print("tpye of 'mnist' is %s" % (type(mnist)))
print("number of train data is %d" % (mnist.train.num_examples))
print("number of test data is %d" % (mnist.test.num_examples))
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
print("MNIST loaded")

"""
print("type of 'trainimg' is %s"    % (type(trainimg)))
print("type of 'trainlabel' is %s"  % (type(trainlabel)))
print("type of 'testimg' is %s"     % (type(testimg)))
print("type of 'testlabel' is %s"   % (type(testlabel)))
print("------------------------------------------------")
print("shape of 'trainimg' is %s"   % (trainimg.shape,))
print("shape of 'trainlabel' is %s" % (trainlabel.shape,))
print("shape of 'testimg' is %s"    % (testimg.shape,))
print("shape of 'testlabel' is %s"  % (testlabel.shape,))

"""
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10]) # None is for infinite
w = tf.Variable(tf.zeros([784, 10])) # 為了方便直接用0初始化,可以高斯初始化
b = tf.Variable(tf.zeros([10])) # 10分類(lèi)的任務(wù),10種label,所以只需要初始化10個(gè)b
pred = tf.nn.softmax(tf.matmul(x, w) + b) # 前向傳播的預(yù)測(cè)值
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=[1])) # 交叉熵?fù)p失函數(shù)
optm = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.equal()對(duì)比預(yù)測(cè)值的索引和真實(shí)label的索引是否一樣,一樣返回True,不一樣返回False
accr = tf.reduce_mean(tf.cast(corr, tf.float32))
init = tf.global_variables_initializer() # 全局參數(shù)初始化器
training_epochs = 100 # 所有樣本迭代100次
batch_size = 100 # 每進(jìn)行一次迭代選擇100個(gè)樣本
display_step = 5
# SESSION
sess = tf.Session() # 定義一個(gè)Session
sess.run(init) # 在sess里run一下初始化操作
# MINI-BATCH LEARNING
for epoch in range(training_epochs): # 每一個(gè)epoch進(jìn)行循環(huán)
    avg_cost = 0. # 剛開(kāi)始損失值定義為0
    num_batch = int(mnist.train.num_examples/batch_size)
    for i in range(num_batch): # 每一個(gè)batch進(jìn)行選擇
        batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 通過(guò)next_batch()就可以一個(gè)一個(gè)batch的拿數(shù)據(jù),
        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) # run一下用梯度下降進(jìn)行求解,通過(guò)placeholder把x,y傳進(jìn)來(lái)
        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y:batch_ys})/num_batch
    # DISPLAY
    if epoch % display_step == 0: # display_step之前定義為5,這里每5個(gè)epoch打印一下
        train_acc = sess.run(accr, feed_dict={x: batch_xs, y:batch_ys})
        test_acc = sess.run(accr, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print("Epoch: %03d/%03d cost: %.9f TRAIN ACCURACY: %.3f TEST ACCURACY: %.3f"
              % (epoch, training_epochs, avg_cost, train_acc, test_acc))
print("DONE")

迭代100次跑一下模型,最終,在測(cè)試集上可以達(dá)到92.2%的準(zhǔn)確率,雖然還不錯(cuò),但是還達(dá)不到實(shí)用的程度。手寫(xiě)數(shù)字的識(shí)別的主要應(yīng)用場(chǎng)景是識(shí)別銀行支票,如果準(zhǔn)確率不夠高,可能會(huì)引起嚴(yán)重的后果。

Epoch: 095/100 loss: 0.283259882 train_acc: 0.940 test_acc: 0.922

插一些知識(shí)點(diǎn),關(guān)于tensorflow中一些函數(shù)的用法

sess = tf.InteractiveSession()
arr = np.array([[31, 23,  4, 24, 27, 34],
                [18,  3, 25,  0,  6, 35],
                [28, 14, 33, 22, 30,  8],
                [13, 30, 21, 19,  7,  9],
                [16,  1, 26, 32,  2, 29],
                [17, 12,  5, 11, 10, 15]])
在tensorflow中打印要用.eval()
tf.rank(arr).eval() # 打印矩陣arr的維度
tf.shape(arr).eval() # 打印矩陣arr的大小
tf.argmax(arr, 0).eval() # 打印最大值的索引,參數(shù)0為按列求索引,1為按行求索引

以上就是TensorFlow教程Softmax邏輯回歸識(shí)別手寫(xiě)數(shù)字MNIST數(shù)據(jù)集的詳細(xì)內(nèi)容,更多關(guān)于Softmax邏輯回歸MNIST數(shù)據(jù)集手寫(xiě)識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python vtk讀取并顯示dicom文件示例

    Python vtk讀取并顯示dicom文件示例

    今天小編就為大家分享一篇Python vtk讀取并顯示dicom文件示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python使用urllib2模塊獲取gravatar頭像實(shí)例

    python使用urllib2模塊獲取gravatar頭像實(shí)例

    python使用urllib2模塊獲取gravatar頭像的實(shí)例,大家參考使用吧
    2013-12-12
  • 使用with torch.no_grad():顯著減少測(cè)試時(shí)顯存占用

    使用with torch.no_grad():顯著減少測(cè)試時(shí)顯存占用

    這篇文章主要介紹了使用with torch.no_grad():顯著減少測(cè)試時(shí)顯存占用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python self,cls,decorator的理解

    python self,cls,decorator的理解

    在python里面,self, cls 不是關(guān)鍵字,完全可以使用自己寫(xiě)的任意變量代替實(shí)現(xiàn)一樣的效果
    2009-07-07
  • 淺談tensorflow 中tf.concat()的使用

    淺談tensorflow 中tf.concat()的使用

    今天小編就為大家分享一篇淺談tensorflow 中tf.concat()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python圖像加噪聲的實(shí)現(xiàn)示例

    python圖像加噪聲的實(shí)現(xiàn)示例

    圖像加噪聲就是其中一種常見(jiàn)的處理方式,噪聲可以幫助提高圖像的真實(shí)性和復(fù)雜性,使得處理后的圖像更加接近真實(shí)的場(chǎng)景,本文主要介紹了python圖像加噪聲的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2023-08-08
  • python之NAN和INF值處理方式

    python之NAN和INF值處理方式

    這篇文章主要介紹了python之NAN和INF值處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解PyQt5信號(hào)與槽的幾種高級(jí)玩法

    詳解PyQt5信號(hào)與槽的幾種高級(jí)玩法

    這篇文章主要介紹了詳解PyQt5信號(hào)與槽的幾種高級(jí)玩法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python批量創(chuàng)建變量并賦值操作

    python批量創(chuàng)建變量并賦值操作

    這篇文章主要介紹了python批量創(chuàng)建變量并賦值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • python實(shí)發(fā)郵件實(shí)例詳解

    python實(shí)發(fā)郵件實(shí)例詳解

    在本篇文章里小編給大家整理的是關(guān)于python實(shí)發(fā)郵件的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。
    2019-11-11

最新評(píng)論

桦南县| 永年县| 惠安县| 淮北市| 崇明县| 炎陵县| 集贤县| 上高县| 岑巩县| 二连浩特市| 宁明县| 石楼县| 夏津县| 迁西县| 安庆市| 白城市| 瓦房店市| 泉州市| 即墨市| 罗源县| 安福县| 乐平市| 定陶县| 龙陵县| 呼图壁县| 莒南县| 揭阳市| 深水埗区| 湘潭县| 肥西县| 桓台县| 涿州市| 西安市| 庆云县| 泽州县| 富平县| 永平县| 巴青县| 绥中县| 象州县| 罗城|