tensorflow实现卷积神经网络经典案例--识别手写数字
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm %matplotlib inline
import tensorflow as tf
#参数设置
learning_rate=1e-4
training_iterations=2000
dropout=0.5
batch_size=50 #所有的训练样本分批训练的效率最高,每一批的训练样本数量就是batch validation_size=2000
image_to_display=10
数据准备:
data=pd.read_csv('.../train.csv')
images=data.iloc[:,1:].values
images=images.astype(np.float)
#对训练数据进行归一化处理,[0:255]=>[0.0:1.0] images=np.multiply(images,1.0/255.0) #print('images({0[0]},{0[1]})'.format(images.shape))可以查看数据格式,输出结果为'data(42000,784)'
image_size=images.shape[1] image_width=image_height=np.ceil(np.sqrt(image_size)).astype(np.uint8)
#定义显示图片的函数
def display(img):
#从784=>28*28
one_img=img.reshape(image_width,image_height)
plt.axis('off')
plt.imshow(one_img,cmap=cm.binary)
display(images[10]) #显示数据表格中第十条数据代表的数字,结果如下图
labels_flat=data.iloc[:,0].values.ravel() labels_count=np.unique(labels_flat).shape[0]
#定义one-hot编码函数
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)
#print(labels[10])的输出结果为'[0 0 0 0 0 0 0 0 1 0]',代表数据集中第10个数字的实际值为'8'
#将数据拆分成用于训练和验证两部分 validation_images=images[:validation_size]
#将训练的数据分为train和validation,validation的部分是为了对比不同模型参数的训练效果,如:learning_rate, training_iterations, dropout validation_labels=labels[:validation_size]
train_images = images[validation_size:]
train_labels = labels[validation_size:]
定义权重、偏差、卷积图层、池化图层:
#定义weight
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial)
#定义bias
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')