import keras
import time
from keras.utils import np_utils

start = time.time()
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
SHAPE = 28 * 28
CLASSES = 10
x_train = x_train.reshape(x_train.shape[0], SHAPE)
x_test = x_test.reshape(x_test.shape[0], SHAPE)
y_train = np_utils.to_categorical(y_train, CLASSES)
y_test = np_utils.to_categorical(y_test, CLASSES)

x_train = x_train.astype("float32")
x_test = x_test.astype("float32")
x_train /= 255
x_test /= 255

model = keras.models.Sequential([
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=1000, epochs=50)
evaluate = model.evaluate(x_test, y_test)
print(evaluate)
print("elapsed: ", time.time() - start)
posted on 2019-10-12 10:33  yytxdy  阅读(389)  评论(0编辑  收藏  举报