write python code using keras to classify hand-writter digits from the MNIST dataset,
时间: 2023-02-14 21:51:27 浏览: 101
```python
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# flatten data
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
# normalize data
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# one-hot encode the labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# create model
model = Sequential()
model.add(Dense(512, input_shape=(784,), activation='relu'))
model.add(Dense(10, activation='softmax'))
# compile model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# train model
model.fit(X_train, y_train, epochs=5, batch_size=32)
# evaluate model
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Test accuracy:', test_acc)
```
这是一个使用 Keras 对 MNIST 数据集中手写数字进行分类的示例代码。它将加载数据、构建模型、编译模型、训练模型和评估模型的过程都包含在其中。
阅读全文