#图片的长和宽的像素数
(x_train,y_train),(x_test,y_test)=mnist.load_data(path="D:\\")
#如果方便访问亚马逊云,可以用(x_train,y_train),(x_test,y_test)=
#mnist.load_data( )来自动下载数据。如果已经从教材资源平台将数据下载
#到本地,#假设保存在D盘根目录下,就使用上述命令形式。需要注意的是,
#为了和linux兼容,windows下的路径反斜杠"\"都要写成"\\"另外,
#上述命令将数据分为训练数据和验证数据。无论是训练集还是验证集,都令x
#是输入数据,y是输出数据
ifK.image_data_format()=='channels_first':
x_train=x_train.reshape(x_train.shape[0],1,img_rows,img_cols)
x_test=x_test.reshape(x_test.shape[0],1,img_rows,img_cols)
input_shape=(1,img_rows,img_cols)
else:
x_train=x_train.reshape(x_train.shape[0],img_rows,img_cols,1)
x_test=x_test.reshape(x_test.shape[0],img_rows,img_cols,1)
input_shape=(img_rows,img_cols,1)
#因为Theano和TensorFlow定义的图片格式不同,这里针对不同的后台对
#数据进行处理,读者暂且可以不用深究
x_train=x_train.astype('float32')
x_test=x_test.astype('float32')
x_train/=255
x_test/=255
print('x_trainshape:',x_train.shape)
print(x_train.shape[0],'trainsamples')
print(x_test.shape[0],'testsamples')
#上述命令给出训练集和测试集的维度,输出如下:
#x_trainshape:(60000,28,28,1)
#60000trainsamples
#10000testsamples
y_train=.to_categorical(y_train,num_classes)
y_test=.to_categorical(y_test,num_classes)
#上述命令将训练数据和验证数据的类别转化成keras支持的格式
#通过以下代码构建深度网络,使用2D卷积层,池化层、Dropout层、压平
#层等
model=Sequential()
(Conv2D(32,kernel_size=(3,3),
activation='relu',
input_shape=input_shape))
(Conv2D(64,(3,3),activation='relu'))
(MaxPooling2D(pool_size=(2,2)))
