好的,下面是一个使用 TensorFlow 实现的 CNN 模型来识别 MNIST 数据集的代码示例:
import tensorflow as tf# 导入 MNIST 数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# 输入图像为 28x28 的灰度图像
input_shape = (28, 28, 1)
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# 将像素值缩放到 0 到 1 之间
x_train, x_test = x_train / 255.0, x_test / 255.0
# 建立模型
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape),
tf.keras.laye