我希望你写一个基于MINIST数据集的神经网络,使用Pycharm,实现手写数字分类,不要解释,给出代码
时间: 2023-02-14 15:23:51 浏览: 138
基于pytorch搭建CNN神经网络实现MINIST数据集手写字识别python源码+模型.zip
```python
import tensorflow as tf
from tensorflow import keras
# 导入MINIST数据集
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# 数据预处理
x_train = x_train / 255.0
x_test = x_test / 255.0
# 构建模型
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练模型
model.fit(x_train, y_train, epochs=5)
# 测试模型
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)
```
阅读全文