# LearnMNIST **Repository Path**: daydreamer0213/learn-MNIST ## Basic Information - **Project Name**: LearnMNIST - **Description**: 这是一个使用PyTorch实现的MNIST手写数字识别深度学习项目。项目构建了一个卷积神经网络(CNN)来识别0-9的手写数字图像。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-07-22 - **Last Updated**: 2025-10-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## MNIST 手写数字识别项目说明 这是一个使用PyTorch实现的MNIST手写数字识别深度学习项目。项目构建了一个卷积神经网络(CNN)来识别0-9的手写数字图像。 ### 项目概述 - **功能**: 使用深度学习模型识别MNIST手写数字数据集 - **框架**: 基于PyTorch深度学习框架 - **硬件支持**: 自动检测并使用GPU加速训练(如果可用) - **模型结构**: 2层卷积神经网络 ### 网络架构 [Net](file://e:\Git-demo\LearnMNIST\learn_MNIST.py#L60-L142)类定义了以下网络结构: 1. **卷积层**: - 第一层: [conv1](file://e:\Git-demo\LearnMNIST\learn_MNIST.py#L0-L0) - 输入通道1,输出通道32,5x5卷积核 - 第二层: [conv2](file://e:\Git-demo\LearnMNIST\learn_MNIST.py#L0-L0) - 输入通道32,输出通道64,5x5卷积核 2. **池化层**: - 两个2x2最大池化层,分别跟在两个卷积层后 3. **正则化**: - [dropout](file://e:\Git-demo\LearnMNIST\learn_MNIST.py#L0-L0)层 - 50%的dropout率防止过拟合 4. **全连接层**: - [fc1](file://e:\Git-demo\LearnMNIST\learn_MNIST.py#L0-L0) - 输入特征1024,输出特征128 - [fc2](file://e:\Git-demo\LearnMNIST\learn_MNIST.py#L0-L0) - 输入特征128,输出特征10(对应0-9数字) 5. **激活函数**: - ReLU激活函数用于隐藏层 - LogSoftmax用于输出层 ### 数据处理 - **数据集**: MNIST手写数字数据集(自动下载) - **预处理**: - 转换为Tensor格式 - 标准化处理(均值0.1307,标准差0.3081) - **数据加载**: - 训练集: 批大小64,随机打乱 - 测试集: 批大小1000,不打乱 ### 训练配置 - **损失函数**: 负对数似然损失(`NLLLoss`) - **优化器**: 随机梯度下降(`SGD`) - 学习率: 0.01 - 动量: 0.5 - **训练轮数**: 10个epoch ### 使用方法 直接运行[learn_MNIST.py](file://e:\Git-demo\LearnMNIST\learn_MNIST.py)脚本即可: ```bash python learn_MNIST.py ``` 程序将自动: 1. 检查并使用可用设备(CUDA/CPU) 2. 下载MNIST数据集 3. 构建和训练模型 4. 显示训练进度和测试准确率 5. 绘制训练和测试损失曲线 ### 输出结果 训练过程中会显示: - 每100批次的训练损失 - 每个epoch后的测试准确率 - 最终的训练和测试损失曲线图 ### 系统要求 - Python 3.x - PyTorch - torchvision - matplotlib > 注意: 首次运行时会自动下载MNIST数据集到`./data`目录