from keras.datasets import mnist
import numpy as np
from keras.utils import np_utils
from keras.layers import Dense,Activation
from keras.models import Sequential
from keras.optimizers import RMSprop
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0],-1)/255 # normalization
x_test = x_test.reshape(x_test.shape[0],-1)/255
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
# built model
model = Sequential([
Dense(32,input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax')
])
# define optimizer
rmsprop = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0)
model.compile(loss='categorical_crossentropy',optimizer=rmsprop,metrics=['accuracy'])
# training
print('training...')
model.fit(x_train,y_train,epochs=2,batch_size=32)
# testing
print('testing...')
cost,accuracy = model.evaluate(x_test,y_test)
print('cost = ',cost)
print('accuracy = ',accuracy)