keras手写数字识别

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)
原文地址:https://www.cnblogs.com/yytxdy/p/11660033.html