import keras
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
from keras import models
from keras import layers
from keras.utils import to_categorical
model = models.Sequential()
model.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
model.add(layers.Dense(10,activation='softmax'))
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
#对训练数据进行维度变换(60000,28,28)-->(60000,784)
#对训练数据进行类型转换 uint8([0,255])--> float32([0,1])
x_train = x_train.reshape((60000,28*28))
x_train = x_train.astype('float32')/255
#对测试数据进行维度变换(60000,28,28)-->(60000,784)
#对测试数据进行类型转换 uint8([0,255])--> float32([0,1])
x_test = x_test.reshape((10000,28*28))
x_test = x_test.astype('float32')/255
#对标签转换为one-shot形式
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model.fit(x_train,y_train,batch_size=128,epochs=10)
test_loss,test_acc = model.evaluate(x_test,y_test)
print(test_loss)
print(test_acc)