这是使用 卷积神经网络(Convolutional Neural Network)对 MNIST 数据集进行手写数字识别的完整教程。通过本项目,您将了解如何构建、训练和部署一个经典的深度学习模型。
📚 项目简介
- 目标:实现99%以上准确率的手写数字分类
- 数据集:MNIST(含60,000张训练图像和10,000张测试图像)
- 技术栈:Python、TensorFlow/Keras、CNN架构
- 应用场景:数字识别、图像分类入门
🧰 环境准备
- 安装依赖:
pip install tensorflow matplotlib numpy
- 数据集获取:
- MNIST数据可通过
tensorflow.keras.datasets.mnist.load_data()
直接加载 - 或访问官方数据集页面下载
- MNIST数据可通过
🧠 模型构建
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
📈 训练过程
- 使用交叉熵损失函数与Adam优化器
- 训练轮数(epochs)建议设置为10
- 可视化训练曲线:
import matplotlib.pyplot as plt plt.plot(history.history['loss'], label='训练损失') plt.plot(history.history['accuracy'], label='训练准确率') plt.legend() plt.show()
🧪 测试与评估
- 测试集准确率可达 99.2%
- 可通过以下代码查看混淆矩阵:
from sklearn.metrics import confusion_matrix import seaborn as sns cm = confusion_matrix(y_test, predictions) sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
📁 项目扩展
更多深度学习项目请访问:/zh/projects/
如需查看完整代码实现,可点击GitHub仓库链接获取。