下面是keras官方的卷积神经网络在github上的例子。和原版的唯一区别是:mnist的数据因为在国外(由于翻墙的原因,报错Exception: URL fetch failure on https://s3.amazonaws.com/img-datasets/mnist.npz),可以像我一样,下载数据后,放在本地。
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
#把原版的网上的数据下载到本地
path='D://tmp//mnist.npz'
f = np.load(path)
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
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)
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)
更多请见:http://www.mark-to-win.com/tutorial/mydb_ConvoluNeural_ConvolutionNeuralHello.html