1 '''Trains a simple deep NN on the MNIST dataset.     3 Gets to 98.40% test accuracy after 20 epochs     4 (there is *a lot* of margin for parameter tuning).     5 2 seconds per epoch on a K520 GPU.    11 from __future__ 
import print_function
    14 from keras.datasets 
import mnist
    15 from keras.models 
import Sequential
    16 from keras.layers 
import Dense, Dropout, Softmax
    17 from keras.optimizers 
import RMSprop, SGD, Adam
    18 from keras 
import regularizers
    19 from keras.constraints 
import MaxNorm
    26 (x_train, y_train), (x_test, y_test) = mnist.load_data()
    28 x_train = x_train.reshape(60000, 784)
    29 x_test = x_test.reshape(10000, 784)
    30 x_train = x_train.astype(
'float32')
    31 x_test = x_test.astype(
'float32')
    34 print(x_train.shape[0], 
'train samples')
    35 print(x_test.shape[0], 
'test samples')
    38 y_train = keras.utils.to_categorical(y_train, num_classes)
    39 y_test = keras.utils.to_categorical(y_test, num_classes)
    42 model.add(
Dense(100, activation=
'relu', input_shape=(784,),use_bias=
False, kernel_regularizer=regularizers.l2(l2)))
    44 model.add(
Dense(100, activation=
'relu', use_bias=
False,kernel_regularizer=regularizers.l2(l2)))
    46 model.add(
Dense(num_classes, activation=
'softmax', use_bias=
False,kernel_regularizer=regularizers.l2(l2)))
    51             loss=
'categorical_crossentropy',
    53             optimizer=Adam(lr=0.001),
    57 history = model.fit(x_train, y_train,
    58                     batch_size=batch_size,
    61                     validation_data=(x_test, y_test))
    62 score = model.evaluate(x_test, y_test, verbose=0)
    63 print(
'Test loss:', score[0])
    64 print(
'Test accuracy:', score[1])
    66 model.save_weights(
'dnn_weights.h5')
    67 model.save(
'dnn_model.h5', include_optimizer=
False)
    69 json_string = model.to_json()
    70 with open(
'dnn_model.json', 
'w') 
as file:
    71     file.write(json_string)