img_rows, img_cols=28, 28
if k.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)
inpx = (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)
inpx = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
输出类的描述:
由于模型的输出可以包含0到9.so之间的任何数字,因此我们需要输出10个类。要为10个类输出,请使用keras.utils.to_categorical
function,它将提供10列。在这10列中,只有一个值为1,其余9为零,输出的这一个值将表示数字的类别。
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
CNN模型中每层工作说明:
layer1是Conv2d层,它使用32个过滤器卷积图像,每个过滤器大小(3 * 3)。
layer2也是一个Conv2D层,它也用于卷积图像,并使用64个滤波器,每个滤波器的大小(3 * 3)。
layer3是MaxPooling2D层,它从大小(3 * 3)的矩阵中选择最大值。
layer4以0.5的速率显示Dropout。
layer5将从layer4获得的输出展平,并将此展平输出传递给layer6。
layer6是神经网络的隐藏层,包含250个神经元。
layer7是输出层,具有10个神经元,用于10类输出,使用softmax函数。








暂无数据