卷积神经网络(CNN)对于图像特征的提取,训练识别有着非常出众的效果。现借助TensorFlow来实现一个四层的卷积神经网络来实现对于mnist数据集的训练识别。
数据读取
mnist数据集可以用train.csv(点此下载)
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
Training_iterations = 2500
Dropout = 0.5
Batch_size = 50
Validation_size = 2000
data = pd.read_csv('train.csv')
print('data({0[0]},{0[1]})'.format(data.shape))
print(data.head())
Learning_rate:学习率
Training_iterations :训练集迭代次数
Dropout :dropout参数,以50%的概率保留神经元
Batch_size :一个batch中样本的个数
Validation_size :测试集的大小
运行得:
数据归一化
mnist中的数据为28x28x1的黑白图片,其中像素点数值范围为0-255,为了训练的方便一般就是要将数据归一化的,所以除以255,归到0-1之间。
images = data.iloc[:,1:].values
images = images.astype(np.float)
images = images/255.0
def display(img):
one_image = img.reshape(28,28)
plt.axis('off')
plt.imshow(one_image,cmap=cm.binary)
display(images[1])
.iloc为通过行号来获取该行数据
.values将pandas.core.frame.DataFrame转换为numpy.ndarray类型
最后打印一张图看一看:
one-hot编码转换及数据集切分
#labels_flat = data.iloc[:,0].values
labels_flat = data[[0]].values.ravel()
labels_count = np.unique(labels_flat).shape[0]
#one-hot coding
'''
def dense_to_one_hot(labels_dense,num_classes):
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels)*num_classes
label_one_hot = np.zeros([num_labels,num_classes])
label_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return label_one_hot
labels = dense_to_one_hot(labels_flat,10)
'''
labels = pd.get_dummies(labels_flat).values
validation_images = images[:Validation_size]
validation_labels = labels[:Validation_size]
train_images = images[Validation_size:]
train_labels = labels[Validation_size:]
.ravel()将二维的矩阵变成一维数组,如将[[1,2,3]]转换为[1,2,3]
将label标签通过pd.get_dummies或者自己写的函数(dense_to_one_hot)来转变成one-hot编码的格式
构建网络
为了避免在建立模型时反复做初始化操作,我们将创建权重参数、创建偏置参数、创建卷积层,创建池化层写成函数形式方便后面调用
def weight_variable(shape):
initial = tf.truncated_normal(shape,stddev = 0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1,shape = shape)
return tf.Variable(initial)
def conv2d(x,w):
return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
通过输出关系创建各个层,注意卷积层因为有pandding输出宽高不变,池化后宽高减半
x = tf.placeholder('float',shape=[None,784])
y_ = tf.placeholder('float',shape=[None,labels_count])
w_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
image = tf.reshape(x,[-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(image,w_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
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)
h_pool2 = max_pool_2x2(h_conv2)
w_fc1 = weight_variable([7*7*64,1024])
b_fc1 = bias_variable([1024])
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)
keep_prob = tf.placeholder('float')
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
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)
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(Learning_rate).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,'float'))
predict = tf.argmax(y,1)
训练
写一个函数用来一个batch一个batch来去训练样本,送入模型进行训练。
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]
np.random.shuffle(perm)对perm做打乱顺序操作
global:python中如果想要为一个定义在函数外的变量赋值,那么你就得告诉python这个变量名是全局的。我们使用global语句完成这一功能。没有global语句,是不可能为定义在函数外的变量赋值的。
assert:python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假。assert断言语句用来测试表示式,其返回值为假,就会触发异常。
init = tf.global_variables_initializer()
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})
启动图开始训练: