4.4MNIST手写体数字图片识别

  • 下载数据。
    每个手写体数字图像在两份文件中都被首尾拼接为一个28*28=784维的像素向量,而且每个像素都使用【0,1】之间的灰度值来显示手写笔画的明暗程度。

  • 搭建模型。
    我们将采用多种基于skflow工具包的模型完成大规模手写体数字图片识别的任务。这些模型包括:线性回归器、全连接并包含三个隐层的深度神经网络(DNN)以及一个较复杂但是性能强大的卷积神经网络(CNN)。

import pandas as pd

train=pd.read_csv('/Users/daqi/Documents/ipython/test/MNIST/train.csv')
#查验训练样本数量为42000条;数据维度为785。
train.shape

(42000, 785)

test=pd.read_csv('/Users/daqi/Documents/ipython/test/MNIST/test.csv')
#查验训练样本数量为28000条;数据维度为784。
test.shape

(28000, 784)

#将训练集中的数据特征与对应标记分离
y_train=train['label']
X_train=train.drop('label',1)

#准备测试特征
X_test=test

import tensorflow as tf
import skflow

#使用skflow中已经封装好的基于tensorflow搭建的线性分类器TensorFlowLinearClassifier进行学习预测
classifier=skflow.TensorFlowLinearClassifier(n_classes=10,batch_size=100,steps=1000,learning_rate=0.01)

classifier.fit(X_train,y_train)

Step #99, avg. train loss: 7.92963
Step #199, avg. train loss: 3.11331
Step #299, avg. train loss: 2.59313
Step #399, avg. train loss: 2.20776
Step #500, epoch #1, avg. train loss: 1.75313
Step #600, epoch #1, avg. train loss: 1.65065
Step #700, epoch #1, avg. train loss: 1.63542
Step #800, epoch #1, avg. train loss: 1.48731
Step #900, epoch #2, avg. train loss: 1.23449
Step #1000, epoch #2, avg. train loss: 1.27328
Out[12]:
TensorFlowLinearClassifier(batch_size=100, class_weight=None,
clip_gradients=5.0, config=None, continue_training=False,
learning_rate=0.01, n_classes=10, optimizer='Adagrad',
steps=1000, verbose=1)

linear_y_predict=classifier.predict(X_test)

linear_submission=pd.DataFrame({'ImageId':range(1,28001),'Label':linear_y_predict})
linear_submission.to_csv('/Users/daqi/Documents/ipython/test/MNIST/linear_submission.csv')

#使用skflow中已经封装好的基于tensorflow搭建的全连接深度神经网络TensorFlowDNNClassifier进行学习预测。
classifier=skflow.TensorFlowDNNClassifier(hidden_units=[200,50,10],n_classes=10,steps=5000,learning_rate=0.01,batch_size=50)
classifier.fit(X_train,y_train)

Step #4000, epoch #4, avg. train loss: 1.14965
Step #4100, epoch #4, avg. train loss: 1.12858
Step #4200, epoch #5, avg. train loss: 1.13715
Step #4300, epoch #5, avg. train loss: 1.05097
Step #4400, epoch #5, avg. train loss: 1.04512
Step #4500, epoch #5, avg. train loss: 1.02332
Step #4600, epoch #5, avg. train loss: 0.99978
Step #4700, epoch #5, avg. train loss: 0.98281
Step #4800, epoch #5, avg. train loss: 0.96837
Step #4900, epoch #5, avg. train loss: 0.95128
Step #5000, epoch #5, avg. train loss: 0.96353
Out[23]:
TensorFlowDNNClassifier(batch_size=50, class_weight=None, clip_gradients=5.0,
config=None, continue_training=False, dropout=None,
hidden_units=[200, 50, 10], learning_rate=0.01, n_classes=10,
optimizer='Adagrad', steps=5000, verbose=1)

dnn_y_predict=classifier.predict(X_test)

dnn_submission=pd.DataFrame({'ImageId':range(1,28001),'Label':dnn_y_predict})
dnn_submission.to_csv('/Users/daqi/Documents/ipython/test/MNIST/dnn_submission.csv',index=False)

#使用Tensorflow中的算子自行搭建更为复杂的卷积神经网络,并使用skflow的程序接口从事MNIST数据的学习与预测。
def max_pool_2x2(tensor_in):
    return tf.nn.max_pool(tensor_in,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

def conv_model(X,y):
    X=tf.reshape(X,[-1,28,28,1])
    with tf.variable_scope('conv_layer1'):
        h_conv1=skflow.ops.conv2d(X,n_filters=32,filter_shape=[5,5],bias=True,activation=tf.nn.relu)
        h_pool1=max_pool_2x2(h_conv1)
    with tf.variable_scope('conv_layer2'):
        h_conv2=skflow.ops.conv2d(h_pool1,n_filters=64,filter_shape=[5,5],bias=True,activation=tf.nn.relu)
        h_pool2=max_pool_2x2(h_conv2)
        h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])
    h_fcl=skflow.ops.dnn(h_pool2_flat,[1024],activation=tf.nn.relu,dropout=0.5)
    return skflow.models.logistic_regression(h_fcl,y)

classifier=skflow.TensorFlowEstimator(model_fn=conv_model,n_classes=10,batch_size=100,steps=20000,learning_rate=0.001)

classifier.fit(X_train,y_train)

Step #19000, epoch #45, avg. train loss: 0.01151
Step #19100, epoch #45, avg. train loss: 0.01212
Step #19200, epoch #45, avg. train loss: 0.01072
Step #19300, epoch #45, avg. train loss: 0.01236
Step #19400, epoch #46, avg. train loss: 0.01132
Step #19500, epoch #46, avg. train loss: 0.01367
Step #19600, epoch #46, avg. train loss: 0.01267
Step #19700, epoch #46, avg. train loss: 0.00997
Step #19800, epoch #47, avg. train loss: 0.01001
Step #19900, epoch #47, avg. train loss: 0.01003
Step #20000, epoch #47, avg. train loss: 0.00917
Out[51]:
TensorFlowEstimator(batch_size=100, class_weight=None, clip_gradients=5.0,
config=None, continue_training=False, learning_rate=0.001,
model_fn=<function conv_model at 0x11ef26bf8>, n_classes=10,
optimizer='Adagrad', steps=20000, verbose=1)

#这里务必请读者朋友在实战中注意,不要将所有的测试样本交给模型进行预测。由于Tensorflow会同时对所有测试样本进行矩阵计算,一次对28000个测试图片进行计算会消耗大量的内存和计算资源。这里所采取的是逐批次地对样本进行预测,最后拼接全部预测结果。
conv_y_predict=[]
import numpy as np
for i in np.arange(100,28001,100):
    conv_y_predict=np.append(conv_y_predict,classifier.predict(X_test[i-100:i]))
conv_submission=pd.DataFrame({'ImageId':range(1,28001),'Label':np.int32(conv_y_predict)})
conv_submission.to_csv('/Users/daqi/Documents/ipython/test/MNIST/conv_submission.csv',index=False)

推荐阅读更多精彩内容