#控制每个训练批次的数据大小的参数,读者可以调整并观察会产生什么变化
num_classes=10
#分类器类别数量。因为要识别0到9共10个手写数字,所以类别是10
#如果识别手写小写字母,则分类就是26
epochs=12
#训练周期,读者可以调整并观察效果,训练周期越大,训练时间越长
img_rows,img_cols=28,28
#图片的长和宽的像素数
(x_train,y_trai)=mnist。load_data(path="D:mnist。npz")
#如果方便访问亚马逊云,可以用(x_train,y_trai)=
#mnist。load_data()来自动下载数据。如果已经从教材资源平台将数据下载
#到本地,#假设保存在D盘根目录下,就使用上述命令形式。需要注意的是,
#为了和linux兼容,windows下的路径反斜杠""都要写成""另外,
#上述命令将数据分为训练数据和验证数据。无论是训练集还是验证集,都令x
#是输入数据,y是输出数据
ifK。image_data_format()=='els_first':
x_train=x_trairain。shape[_cols)
&。reshape(x_test。shape[_cols)
input_shape=(1,img_rows,img_cols)
else:
x_train=x_trairain。shape[_cols,1)
&。reshape(x_test。shape[_cols,1)
input_shape=(img_rows,img_cols,1)
#因为Theano和TensorFlow定义的图片格式不同,这里针对不同的后台对
#数据进行处理,读者暂且可以不用深究
x_train=x_train。astype('float32')
&。astype('float32')
x_train=255
&=255
print('x_trainshape:',x_train。shape)
print(x_train。shape[0],'trainsamples')
pri。shape[0],'testsamples')
#上述命令给出训练集和测试集的维度,输出如下:
#x_trainshape:(60000,28,28,1)
#60000trainsamples
#10000testsamples
y_traiils。to_categorical(y_train,num_classes)