欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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分類的任務,10種label,所以只需要初始化10個b
pred = tf.nn.softmax(tf.matmul(x, w) + b) # 前向傳播的預測值
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=[1])) # 交叉熵損失函數(shù)
optm = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.equal()對比預測值的索引和真實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 # 每進行一次迭代選擇100個樣本
display_step = 5
# SESSION
sess = tf.Session() # 定義一個Session
sess.run(init) # 在sess里run一下初始化操作
# MINI-BATCH LEARNING
for epoch in range(training_epochs): # 每一個epoch進行循環(huán)
    avg_cost = 0. # 剛開始損失值定義為0
    num_batch = int(mnist.train.num_examples/batch_size)
    for i in range(num_batch): # 每一個batch進行選擇
        batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 通過next_batch()就可以一個一個batch的拿數(shù)據(jù),
        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) # run一下用梯度下降進行求解,通過placeholder把x,y傳進來
        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個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次跑一下模型,最終,在測試集上可以達到92.2%的準確率,雖然還不錯,但是還達不到實用的程度。手寫數(shù)字的識別的主要應用場景是識別銀行支票,如果準確率不夠高,可能會引起嚴重的后果。

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

插一些知識點,關于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ù)字MNIST數(shù)據(jù)集的詳細內(nèi)容,更多關于Softmax邏輯回歸MNIST數(shù)據(jù)集手寫識別的資料請關注腳本之家其它相關文章!

相關文章

  • 一款Python工具制作的動態(tài)條形圖(強烈推薦!)

    一款Python工具制作的動態(tài)條形圖(強烈推薦!)

    有時為了方便看數(shù)據(jù)的變化情況,需要畫一個動態(tài)圖來看整體的變化情況,下面這篇文章主要給大家介紹了一款Python工具制作的動態(tài)條形圖的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • python實現(xiàn)圖片插入文字

    python實現(xiàn)圖片插入文字

    這篇文章主要為大家詳細介紹了python實現(xiàn)圖片插入文字,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 解決pytorch trainloader遇到的多進程問題

    解決pytorch trainloader遇到的多進程問題

    這篇文章主要介紹了解決pytorch trainloader遇到的多進程問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • nlp計數(shù)法應用于PTB數(shù)據(jù)集示例詳解

    nlp計數(shù)法應用于PTB數(shù)據(jù)集示例詳解

    這篇文章主要為大家介紹了nlp計數(shù)法應用于PTB數(shù)據(jù)集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • 卷積神經(jīng)網(wǎng)絡CharCNN實現(xiàn)中文情感分類任務

    卷積神經(jīng)網(wǎng)絡CharCNN實現(xiàn)中文情感分類任務

    這篇文章主要為大家介紹了卷積神經(jīng)網(wǎng)絡CharCNN實現(xiàn)中文情感分類任務詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • python基于Tkinter實現(xiàn)人員管理系統(tǒng)

    python基于Tkinter實現(xiàn)人員管理系統(tǒng)

    這篇文章主要為大家詳細介紹了python基于Tkinter實現(xiàn)人員管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 對python特殊函數(shù) __call__()的使用詳解

    對python特殊函數(shù) __call__()的使用詳解

    今天小編就為大家分享一篇對python特殊函數(shù) __call__()的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • pycham查看程序執(zhí)行的時間方法

    pycham查看程序執(zhí)行的時間方法

    今天小編就為大家分享一篇pycham查看程序執(zhí)行的時間方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • pycharm運行按鈕灰色問題及解決

    pycharm運行按鈕灰色問題及解決

    這篇文章主要介紹了pycharm運行按鈕灰色問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 在Django中創(chuàng)建第一個靜態(tài)視圖

    在Django中創(chuàng)建第一個靜態(tài)視圖

    這篇文章主要介紹了在Django中創(chuàng)建第一個靜態(tài)視圖的方法,與其他編程語言的開始一樣,以Hello world作為示例,需要的朋友可以參考下
    2015-07-07

最新評論