1.读取数据和理解数据
1)库调用声明和参数设置
import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import tensorflow as tf
#设置学习率
LEARNING_RATE = 1e-4
#进行2500次迭代
TRAINING_ITERATIONS = 2500
#设置dropout保留率为50%
DROPOUT = 0.5
#每一个batch训练50张图片
BATCH_SIZE = 50
#使用2000张图片来进行测试
VALIDATION_SIZE = 2000
#图像最终输出为10个分类
IMAGE_TO_DISPLAY = 10
2)读取数据
# read training data from CSV file
data = pd.read_csv('train.csv')
print('data({0[0]},{0[1]})'.format(data.shape))
print (data.head())
显示结果如下。可以看出训练数据由42000个样本组成,每个样本包含一个label值和784个特征值。我们知道mnist数据集是28*28的图片,784就是这28*28个像素值。
data(42000,785)
label pixel0 pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 \
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0
3 4 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
pixel8 ... pixel774 pixel775 pixel776 pixel777 pixel778 \
0 0 ... 0 0 0 0 0
1 0 ... 0 0 0 0 0
2 0 ... 0 0 0 0 0
3 0 ... 0 0 0 0 0
4 0 ... 0 0 0 0 0
pixel779 pixel780 pixel781 pixel782 pixel783
0 0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
[5 rows x 785 columns]
3)获取特征数据,并对特征数据进行归一化处理
#选取除标签外所有的数据
images = data.iloc[:,1:].values
#数值转换
images = images.astype(np.float)
# 像素值为0-255之间的值,转换为0-1之间的值 [0:255] => [0.0:1.0]
images = np.multiply(images, 1.0 / 255.0)
image_size = images.shape[1]
# in this case all images are square
#根据训练数据获取图像的宽和高
image_width = image_height = np.ceil(np.sqrt(image_size)).astype(np.uint8)
4)对转换后的图片进行展示
def display(img):
# (784) => (28,28)
one_image = img.reshape(image_width,image_height)
plt.axis('off')
plt.imshow(one_image, cmap=cm.binary)
# output image
display(images[IMAGE_TO_DISPLAY])
训练数据中的一张图片展示如下:
2.对数据进行处理转换
1)计算所有的标签类别
#从原始数据中取出所有的标签值
labels_flat = data.iloc[:,0].values.ravel()
#获取标签类别数
labels_count = np.unique(labels_flat).shape[0]
print('labels_count => {0}'.format(labels_count))
计算结果为:
labels_count => 10
2)对标签值进行one_hot类别转换
# convert class labels from scalars to one-hot vectors
# 0 => [1 0 0 0 0 0 0 0 0 0]
# 1 => [0 1 0 0 0 0 0 0 0 0]
# ...
# 9 => [0 0 0 0 0 0 0 0 0 1]
def dense_to_one_hot(labels_dense, num_classes):
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
labels = dense_to_one_hot(labels_flat, labels_count)
labels = labels.astype(np.uint8)
3)分离出训练数据和测试数据
#取前VALIDATION_SIZE个样本为测试数据,其余为训练数据
validation_images = images[:VALIDATION_SIZE]
validation_labels = labels[:VALIDATION_SIZE]
train_images = images[VALIDATION_SIZE:]
train_labels = labels[VALIDATION_SIZE:]
3.卷积过程用到的函数定义
1)权重(weight)和偏置(bias)初始化
# weight initialization
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
# bias initialization
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
2)卷积和池化函数定义
tf.nn.conv2d参数解释
x:输入图像数据
W:filter
strides:tf在4维图像上进行计算,strides第一个参数表示在batchsize上的滑动,一般指定为1就可以,后边三个参数依次为宽、高、颜色通道上的滑动,一般只修改中间两个值,也就是在宽高上的滑动
strides的第四个参数1表示通道上的滑动
padding:取值为SAME表示周围用0进行填充,取值为VALID表示不进行填充
tf.nn.max_pool(value,kszie,strides,padding,name=None)参数解释
value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape
ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]
padding:和卷积类似,可以取'VALID' 或者'SAME'
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# pooling
# [[0,3],
# [4,2]] => 4
# [[0,1],
# [1,1]] => 1
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
4.卷积神经网络各层计算
1)第一个卷积、Relu和池化层计算
# input and output
x = tf.placeholder('float', shape=[None, image_size])
y_ = tf.placeholder('float', shape=[None, labels_count])
#第一个卷积层计算
#宽5 高5 channel为1 用多少个filter来计算,这里是32
W_conv1 = weight_variable([5, 5, 1, 32])
#执行卷积后得到了32个图,所以需要32个偏置
b_conv1 = bias_variable([32])
# (40000,784) => (40000,28,28,1)
image = tf.reshape(x, [-1,image_width , image_height,1])
#print (image.get_shape()) # =>(40000,28,28,1)
h_conv1 = tf.nn.relu(conv2d(image, W_conv1) + b_conv1)
#print (h_conv1.get_shape()) # => (40000, 28, 28, 32)
h_pool1 = max_pool_2x2(h_conv1)
#print (h_pool1.get_shape()) # => (40000, 14, 14, 32)
2)第二个卷积、Relu和池化层计算
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
#print (h_conv2.get_shape()) # => (40000, 14,14, 64)
h_pool2 = max_pool_2x2(h_conv2)
#print (h_pool2.get_shape()) # => (40000, 7, 7, 64)
3)全连接层计算
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
# (40000, 7, 7, 64) => (40000, 3136)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#print (h_fc1.get_shape()) # => (40000, 1024)
4)dropout处理
keep_prob = tf.placeholder('float')
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
5)损失值计算并进行预测
W_fc2 = weight_variable([1024, labels_count])
b_fc2 = bias_variable([labels_count])
y = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
#print (y.get_shape()) # => (40000, 10)
# cost function
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
# optimisation function
train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(cross_entropy)
# evaluation
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
# prediction function
#[0.1, 0.9, 0.2, 0.1, 0.1 0.3, 0.5, 0.1, 0.2, 0.3] => 1
predict = tf.argmax(y,1)
5.分批次进行训练和测试
epochs_completed = 0
index_in_epoch = 0
num_examples = train_images.shape[0]
# serve data by batches
def next_batch(batch_size):
global train_images
global train_labels
global index_in_epoch
global epochs_completed
start = index_in_epoch
index_in_epoch += batch_size
# when all trainig data have been already used, it is reorder randomly
if index_in_epoch > num_examples:
# finished epoch
epochs_completed += 1
# shuffle the data
perm = np.arange(num_examples)
np.random.shuffle(perm)
train_images = train_images[perm]
train_labels = train_labels[perm]
# start next epoch
start = 0
index_in_epoch = batch_size
assert batch_size <= num_examples
end = index_in_epoch
return train_images[start:end], train_labels[start:end]
# start TensorFlow session
init = tf.initialize_all_variables()
sess = tf.InteractiveSession()
sess.run(init)
# visualisation variables
train_accuracies = []
validation_accuracies = []
x_range = []
display_step=1
for i in range(TRAINING_ITERATIONS):
#get new batch
batch_xs, batch_ys = next_batch(BATCH_SIZE)
# check progress on every 1st,2nd,...,10th,20th,...,100th... step
if i%display_step == 0 or (i+1) == TRAINING_ITERATIONS:
train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 1.0})
if(VALIDATION_SIZE):
validation_accuracy = accuracy.eval(feed_dict={ x: validation_images[0:BATCH_SIZE],
y_: validation_labels[0:BATCH_SIZE],
keep_prob: 1.0})
print('training_accuracy / validation_accuracy => %.2f / %.2f for step %d'%(train_accuracy, validation_accuracy, i))
validation_accuracies.append(validation_accuracy)
else:
print('training_accuracy => %.4f for step %d'%(train_accuracy, i))
train_accuracies.append(train_accuracy)
x_range.append(i)
# increase display_step
if i%(display_step*10) == 0 and i:
display_step *= 10
# train on batch
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys, keep_prob: DROPOUT})
6.运行结果
training_accuracy / validation_accuracy => 0.24 / 0.14 for step 0
training_accuracy / validation_accuracy => 0.06 / 0.06 for step 1
training_accuracy / validation_accuracy => 0.20 / 0.32 for step 2
training_accuracy / validation_accuracy => 0.16 / 0.26 for step 3
training_accuracy / validation_accuracy => 0.24 / 0.26 for step 4
training_accuracy / validation_accuracy => 0.20 / 0.30 for step 5
training_accuracy / validation_accuracy => 0.36 / 0.40 for step 6
training_accuracy / validation_accuracy => 0.46 / 0.32 for step 7
training_accuracy / validation_accuracy => 0.28 / 0.40 for step 8
training_accuracy / validation_accuracy => 0.34 / 0.28 for step 9
training_accuracy / validation_accuracy => 0.34 / 0.46 for step 10
training_accuracy / validation_accuracy => 0.44 / 0.56 for step 20
training_accuracy / validation_accuracy => 0.60 / 0.66 for step 30
training_accuracy / validation_accuracy => 0.68 / 0.68 for step 40
training_accuracy / validation_accuracy => 0.74 / 0.82 for step 50
training_accuracy / validation_accuracy => 0.76 / 0.70 for step 60
training_accuracy / validation_accuracy => 0.82 / 0.80 for step 70
training_accuracy / validation_accuracy => 0.76 / 0.82 for step 80
training_accuracy / validation_accuracy => 0.86 / 0.84 for step 90
training_accuracy / validation_accuracy => 0.80 / 0.84 for step 100
training_accuracy / validation_accuracy => 0.96 / 0.90 for step 200
training_accuracy / validation_accuracy => 0.92 / 0.88 for step 300
training_accuracy / validation_accuracy => 0.88 / 0.88 for step 400
training_accuracy / validation_accuracy => 0.96 / 0.90 for step 500
training_accuracy / validation_accuracy => 0.92 / 0.90 for step 600
training_accuracy / validation_accuracy => 0.90 / 0.90 for step 700
training_accuracy / validation_accuracy => 0.94 / 0.90 for step 800
training_accuracy / validation_accuracy => 0.94 / 0.90 for step 900
training_accuracy / validation_accuracy => 0.94 / 0.92 for step 1000
training_accuracy / validation_accuracy => 0.94 / 0.94 for step 2000
training_accuracy / validation_accuracy => 0.98 / 0.94 for step 2499
从以上运行结果可以看出,迭代2500次后训练准确率已经达到98%,测试准确率也在94%了。
github代码路径:https://github.com/zhuwsh/mnist-master