TensorFlow實戰(一)手寫數字識別

2021-08-13 11:02:08 字數 3562 閱讀 9307

tensorflow實戰:

第一章是基礎,沒什麼好看的,跳過出,第二章是說tensorflow和其他的模組,比如caffe等,caffe以前也說過,比較容易,但是配置比較麻煩(cpu的容易點,gpu比較麻煩)

第三章:簡單說一下安裝吧,就行在命令列輸入(前提是你已經有python)

pip install tensorflow

第乙個例子就是手寫數字的識別,準確率不是太高,92%左右,具體操作如下。

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("mnist_data/", one_hot=true)

print(mnist.train.images.shape, mnist.train.labels.shape)

print(mnist.test.images.shape, mnist.test.labels.shape)

print(mnist.validation.images.shape, mnist.validation.labels.shape)

import tensorflow as tf

sess = tf.interactivesession()

x = tf.placeholder(tf.float32, [none, 784])

w = tf.variable(tf.zeros([784, 10]))

b = tf.variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, w) + b)

y_ = tf.placeholder(tf.float32, [none, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.gradientdescentoptimizer(0.5).minimize(cross_entropy)

tf.global_variables_initializer().run()

for i in range(1000):

batch_xs, batch_ys = mnist.train.next_batch(100)

train_step.run()

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(accuracy.eval())

上面是整體**:

我們會對其一步一步介紹:

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("mnist_data/", one_hot=true)

print(mnist.train.images.shape, mnist.train.labels.shape)
#把資料的測試資料集顯示出來,結果是(55000, 784) (55000, 10),表示有55000個測試,28*28,10個標籤

print(mnist.test.images.shape, mnist.test.labels.shape)

#(10000, 784) (10000, 10)

print(mnist.validation.images.shape, mnist.validation.labels.shape)

#(5000, 784) (5000, 10)

28*28寫成784的原因:

答:這裡丟棄了的二 維結構方面的資訊,只 是把一張變成乙個很長的1維向量。讀者可能會問,的空間結構資訊不是很有價值嗎,為什麼我們要丟棄呢?因為這個資料集的分類任務比較簡單,同時也是我們使用tensorflow的第一次嘗試, 我們 不需要建立乙個太複雜的模型 ,所以簡化了問題,丟棄空間結構的資訊。後面的章節將使用卷積神經網路對空間結構資訊進行利用,並取得更高的準確率。我們將展開成l維向量時 ,順序並不重要,只要每一張都是用同樣的順序進行展開的 就可以。

import tensorflow as tf

sess = tf.interactivesession()

x = tf.placeholder(tf.float32, [none, 784])

#輸入資料的地方
w = tf.variable(tf.zeros([784, 10]))

b = tf.variable(tf.zeros([10]))

#tensorflow的輸入變數,這裡是初始化的輸入引數
y = tf.nn.softmax(tf.matmul(x, w) + b)
#輸入x是,輸出的量,softmax是多分類的激勵函式
y_ = tf.placeholder(tf.float32, [none, 10])
#輸入的真實資料的標籤。
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
#計算損失函式,這裡用的是交叉熵(有些用的是二範數),我其中一篇部落格講了它們的優缺點。
train_step = tf.train.gradientdescentoptimizer(0.5).minimize(cross_entropy)
#優化演算法
tf.global_variables_initializer().run()
#全域性引數初始化
for i in range(1000):

batch_xs, batch_ys = mnist.train.next_batch(100)

train_step.run()

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(accuracy.eval())

我的實驗結果準確率是91.8%

(一)手寫spring IOC容器

設計bean工廠介面 如何告訴他建立bean?建立什麼bean?bean工廠實現 畫完整uml類圖 編寫 測試總結 1.ioc是什麼?ioc inversion of control 控制反轉,也稱依賴倒置反轉。反 依賴物件的獲得權被反轉了,由自己建立,變為從ioc容器獲取,和自動注入。2.帶來什麼...

一手寫不出大市場

丁乙乙 手寫板?就是那種給老人用的?在回答記者關於 你是否會購買手寫板 時,一位20來歲的小夥子脫口而出這句話。怎麼會呢?記者真是從來沒有聽到這樣的理論。怎麼不是?我每天網上聊天,雖然打的是全拼,但也蠻快的。有什麼必要再去用手寫?只有那些連拼音也掌握不了的老人才會去用呢!短短幾句話,雖然難免有失偏頗...

手撕VUE原始碼 一 手寫乙個MVVM

class vue class observer observer data reactive key,value,data set newvalue class compiler compilertodata fregment else compilerelementnode node attr ...