# -*- coding: utf-8 -*-'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibilityfrom keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
# Keras的底层库使用Theano或TensorFlowfrom keras import backend as K
batch_size = 128
nb_classes = 10
nb_epoch = 12# input image dimensions
img_rows, img_cols = 28, 28# number of convolutional filters to use
nb_filters = 32# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# 在如何表示一组彩色图片的问题上,Theano和TensorFlow发生了分歧.# ’th’模式,也即Theano模式会把100张RGB三通道的16×32(高为16宽为32)彩色图表示为下面这种形式(100,3,16,32),Caffe采取的也是这种方式。第0个维度是样本维,代表样本的数目,第1个维度是通道维,代表颜色通道数。后面两个就是高和宽了。# 而TensorFlow,即’tf’模式的表达形式是(100,16,32,3),即把通道维放在了最后。# 根据backend模式reshape输入数据if K.image_dim_ordering() ==