tensorflow-gpu版实现简单MNIST手写字识别2

-- 环境:win10, jupyter notebook/pycharm, python3.x, tensorflow1.3.0-gpu

上章我们学习了tensorflow怎样运用算法太实现MNIST手写字的照片识别,这一节来建立一个简单模型,使得识别精度更高。

Import Numpy ,Tensorflow, TFLearn, and MNIST data

import numpy as np 
import tensorflow as tf
import tflearn
import tflearn.datasets.mnist as mnist

Retrieve the training and test data

x_train, y_train, x_test, y_test = mnist.load_data(one_hot = True)
数据加载

首先讲讲这句话的作用,matplotlib是最著名的Python图表绘制扩展库,它支持输出多种格式的图形图像,并且可以使用多种GUI界面库交互式地显示图表。使用%matplotlib命令可以将matplotlib的图表直接嵌入到Notebook之中,或者使用指定的界面库显示图表,它有一个参数指定matplotlib图表的显示方式。inline表示将图表嵌入到Notebook中。

其次,这个问题是说没有合理的GUI请求,所以要安装以下任何一种都可以,ipython-qtconsole就行。
IPython提供了许多魔法命令,使得在IPython环境中的操作更加得心应手。魔法命令都以%或者%%开头,以%开头的成为行命令,%%开头的称为单元命令。行命令只对命令所在的行有效,而单元命令则必须出现在单元的第一行,对整个单元的代码进行处理。

执行%magic可以查看关于各个命令的说明,而在命令之后添加?可以查看该命令的详细说明。

#Visualiziing 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 = y_train[index].argmax(axis=0)
    #Reshape 784 array into 28x28 image
    image = x_train[index].reshape([28,28])
    plt.title('Training data, indx:%d, Label:%d'%(index, label))
    plt.imshow(image, cmap='gray_r')
    plt.show()

#Display the first (index 0) training image
show_digit(0)
tensorflow-gpu版实现简单MNIST手写字识别2_第1张图片
带标签数据显示

Define the meural network

def build_model():
    #This resets all parameters and variables
    tf.reset_default_graph()
    #input layer ,Hidder layer(s),and set how you   want to train the model
    #input
    net = tflearn.input_data([None, 784])
    #hidder layer
    net = tflearn.fully_connected(net,128, activation = 'ReLU')
    #net = tflearn.fully_connected(net, 64, activation = 'ReLU')
    #net = tflearn.fully_connected(net, 32, activation = 'ReLU')
    net = tflearn.fully_connected(net, 20, activation = 'ReLU')
    #Output layer
    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

此神经网络模型包含一个输入层,两个全连接层激活函数为ReLu,一个输出层激活函数为softmax

Build the model

model = build_model()

training network

此处参数为调试后的最佳参数,可以修改了观察得到结果的不同,epoch不能太小,也不能太大,对自己网络训练效果好就得适当

model.fit(x_train,y_train, validation_set = 0.1, show_metric = True,batch_size =10,n_epoch = 11)
训练网络

tensorflow-gpu版实现简单MNIST手写字识别2_第2张图片
训练结束

Testting

# 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)
97%

你可能感兴趣的:(tensorflow-gpu版实现简单MNIST手写字识别2)