tensorflow-gpu版TFLearn實(shí)現(xiàn)簡單MNIST手寫字識別3

Handwritten Number Recognition with TFLearn and MNIST

# Import Numpy, TensorFlow, TFLearn, and MNIST data
import numpy as np
import tensorflow as tf
import tflearn
import tflearn.datasets.mnist as mnist

Retrieving training and test data?

The MNIST data set already contains both training and test data. There are 55,000 data points of training data, and 10,000 points of test data.

Each MNIST data point has:

  1. an image of a handwritten digit and
  2. a corresponding label (a number 0-9 that identifies the image)

We'll call the images, which will be the input to our neural network, X and their corresponding labels Y.

We're going to want our labels as one-hot vectors, which are vectors that holds mostly 0's and one 1. It's easiest to see this in a example. As a one-hot vector, the number 0 is represented as [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], and 4 is represented as [0, 0, 0, 0, 1, 0, 0, 0, 0, 0].

Flattened data?

For this example, we'll be using flattened data or a representation of MNIST images in one dimension rather than two. So, each handwritten number image, which is 28x28 pixels, will be represented as a one dimensional array of 784 pixel values.

Flattening the data throws away information about the 2D structure of the image, but it simplifies our data so that all of the training data can be contained in one array whose shape is [55000, 784]; the first dimension is the number of training images and the second dimension is the number of pixels in each image. This is the kind of data that is easy to analyze using a simple neural network.

# Retrieve the training and test data
trainX, trainY, testX, testY = mnist.load_data(one_hot=True)
image.png

Visualizing the data

import matplotlib.pyplot as plt
%matplotlib inline

# Function for displaying a training image by it's index in the MNIST     set
def show_digit(index):
    label = trainY[index].argmax(axis=0)
    # Reshape 784 array into 28x28 image
    image = trainX[index].reshape([28,28])
    plt.title('Training data, index: %d,  Label: %d' % (index, label))
    plt.imshow(image, cmap='gray_r')
    plt.show()

    # Display the first (index 0) training image
    show_digit(3)
image.png

Building the network?

TFLearn lets you build the network by defining the layers in that network.

For this example, you'll define:

  1. The input layer, which tells the network the number of inputs it should expect for each piece of MNIST data.
  2. Hidden layers, which recognize patterns in data and connect the input to the output layer, and
  3. The output layer, which defines how the network learns and outputs a label for a given image.

Let's start with the input layer; to define the input layer, you'll define the type of data that the network expects. For example,

net = tflearn.input_data([None, 100])

would create a network with 100 inputs. The number of inputs to your network needs to match the size of your data. For this example, we're using 784 element long vectors to encode our input data, so we need 784 input units.

Adding layers?

To add new hidden layers, you use

net = tflearn.fully_connected(net, n_units, activation='ReLU')

This adds a fully connected layer where every unit (or node) in the previous layer is connected to every unit in this layer. The first argument net is the network you created in the tflearn.input_data call, it designates the input to the hidden layer. You can set the number of units in the layer with n_units, and set the activation function with the activation keyword. You can keep adding layers to your network by repeated calling tflearn.fully_connected(net, n_units).

Then, to set how you train the network, use:

net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1, loss='categorical_crossentropy')

Again, this is passing in the network you've been building. The keywords:

  • optimizer sets the training method, here stochastic gradient descent
  • learning_rate is the learning rate
  • loss determines how the network error is calculated. In this example, with categorical cross-entropy.

Finally, you put all this together to create the model with tflearn.DNN(net).

<textarea tabindex="0" style="padding: 0px; width: 1px; height: 1em; bottom: -1em; position: absolute;" spellcheck="false" wrap="off" autocorrect="off" autocapitalize="off"></textarea>

<pre class=" CodeMirror-line " role="presentation">Exercise: Below in the build_model() function, you'll put together the network using TFLearn. You get to choose how many layers to use, how many hidden units, etc.</pre>

<pre class=" CodeMirror-line " role="presentation">?</pre>

<pre class=" CodeMirror-line " role="presentation">Hint: The final output layer must have 10 output nodes (one for each digit 0-9). It's also recommended to use a softmax activation layer as your final output layer. </pre>

Exercise: Below in the build_model() function, you'll put together the network using TFLearn. You get to choose how many layers to use, how many hidden units, etc.

Hint: The final output layer must have 10 output nodes (one for each digit 0-9). It's also recommended to use a softmax activation layer as your final output layer.

# Define the neural network
def build_model():
    # This resets all parameters and variables, leave this here
    tf.reset_default_graph()
     #### Your code ####
    #Include the input layer, hidden layer(s), and set how you want to train the model
    #input
    net = tflearn.input_data([None, 784])
    #hidden
    net = tflearn.fully_connected(net, 128, activation='ReLU')
    net = tflearn.fully_connected(net, 20, activation='ReLU')
    # Output
    net = tflearn.fully_connected(net, 10, activation='softmax')    
    net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1,\
                         loss='categorical_crossentropy')
    # This model assumes that your network is named "net"    
    model = tflearn.DNN(net)
    return model

# Build the model
model = build_model()

Training the network?

Now that we've constructed the network, saved as the variable model, we can fit it to the data. Here we use the model.fit method. You pass in the training features trainX and the training targets trainY. Below I set validation_set=0.1 which reserves 10% of the data set as the validation set. You can also set the batch size and number of epochs with the batch_size and n_epoch keywords, respectively.

Too few epochs don't effectively train your network, and too many take a long time to execute. Choose wisely!

Training

model.fit(trainX, trainY, validation_set=0.1, show_metric=True,       batch_size=100, n_epoch=110)
image.png

Testing?

After you're satisified with the training output and accuracy, you can then run the network on the test data set to measure it's performance! Remember, only do this after you've done the training and are satisfied with the results.

A good result will be higher than 95% accuracy. Some simple models have been known to get up to 99.7% accuracy!

# Compare the labels that our model predicts with the actual labels

# Find the indices of the most confident prediction for each item. That     tells us the predicted digit for that sample.
predictions = np.array(model.predict(testX)).argmax(axis=1)

# Calculate the accuracy, which is the percentage of times the   predicated labels matched the actual labels
actual = testY.argmax(axis=1)
test_accuracy = np.mean(predictions == actual, axis=0)

# Print out the result
print("Test accuracy: ", test_accuracy)
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市绢彤,隨后出現(xiàn)的幾起案子玻佩,更是在濱河造成了極大的恐慌,老刑警劉巖淫茵,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)联喘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辙纬,“玉大人豁遭,你說我怎么就攤上這事∩剑” “怎么了堤框?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我蜈抓,道長启绰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任沟使,我火速辦了婚禮委可,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘腊嗡。我一直安慰自己着倾,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布燕少。 她就那樣靜靜地躺著卡者,像睡著了一般。 火紅的嫁衣襯著肌膚如雪客们。 梳的紋絲不亂的頭發(fā)上崇决,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機(jī)與錄音底挫,去河邊找鬼恒傻。 笑死,一個(gè)胖子當(dāng)著我的面吹牛建邓,可吹牛的內(nèi)容都是我干的盈厘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼官边,長吁一口氣:“原來是場噩夢啊……” “哼沸手!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起拒逮,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤罐氨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后滩援,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體栅隐,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年玩徊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了租悄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡恩袱,死狀恐怖泣棋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情畔塔,我是刑警寧澤潭辈,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布鸯屿,位于F島的核電站,受9級特大地震影響把敢,放射性物質(zhì)發(fā)生泄漏寄摆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一修赞、第九天 我趴在偏房一處隱蔽的房頂上張望婶恼。 院中可真熱鬧,春花似錦柏副、人聲如沸勾邦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽眷篇。三九已至,卻和暖如春锨推,著一層夾襖步出監(jiān)牢的瞬間铅歼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工换可, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人厦幅。 一個(gè)月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓沾鳄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親确憨。 傳聞我的和親對象是個(gè)殘疾皇子译荞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,346評論 0 10
  • 最近一段時(shí)間,因?yàn)榧矣胁√柵阕o(hù)休弃,需要長期和家人共處一室吞歼,原本關(guān)系不錯(cuò),但畢竟需要彼此習(xí)慣塔猾,因而也會(huì)有很多不適應(yīng)篙骡。 ...
    怡兒話書影閱讀 215評論 0 1
  • 雙眼相擁, 能從彼此的眼神中丈甸, 讀出驅(qū)逐孤獨(dú)的溫柔糯俗, 但找不回, 滾燙了臉頰的溫度睦擂。 在我空蕩的世界得湘, 獨(dú)自徘徊,...
    繹文閱讀 195評論 0 0
  • 一般要寫一個(gè)大的類似連載的東西首先都要介紹一下這個(gè)東西是什么顿仇,那我也簡單的介紹一下好了淘正。突然想寫一寫自己今年開始進(jìn)...
    三盞燈亮一盞閱讀 304評論 0 0