深度学习测试用例说明¶
本目录包含神经网络和深度学习组件的 pytest 测试用例。
📁 目录结构¶
Text Only
深度学习/tests/
├── conftest.py # pytest配置和共享fixtures
├── test_neural_network.py # 神经网络测试
└── README.md # 本文件
🚀 快速开始¶
安装依赖¶
运行测试¶
Bash
# 运行所有测试
pytest -v
# 只运行NumPy实现的测试(不需要PyTorch)
pytest -v -k "not PyTorch"
# 运行PyTorch测试
pytest -v -k "PyTorch"
📊 测试覆盖¶
test_neural_network.py¶
NumPy 实现的神经网络组件¶
层 (Layers) - LinearLayer: 全连接层 - 前向传播 - 反向传播 - 梯度计算
激活函数 (Activation Functions) - ReLU: 修正线性单元 - Sigmoid: S 型函数 - Tanh: 双曲正切 - Softmax: 归一化指数函数
损失函数 (Loss Functions) - MSELoss: 均方误差 - CrossEntropyLoss: 交叉熵
完整网络 - SimpleNeuralNetwork: 简单前馈网络
PyTorch 组件测试¶
层测试 - nn.Linear: 全连接层 - nn.Conv2d: 卷积层 - nn.MaxPool2d: 最大池化层 - nn.BatchNorm2d: 批归一化层
激活函数测试 - nn.ReLU - nn.Sigmoid - nn.Softmax
损失函数测试 - nn.MSELoss - nn.CrossEntropyLoss - nn.BCELoss
训练测试 - XOR 问题训练 - 梯度流验证 - 梯度累积测试
🔧 测试标记¶
📝 实现说明¶
NumPy 实现¶
测试文件中包含从零实现的神经网络组件,适合理解底层原理:
Python
# 全连接层前向传播
output = np.dot(x, self.weights) + self.bias
# ReLU激活
output = np.maximum(0, x)
# Sigmoid激活
output = 1 / (1 + np.exp(-x))
# Softmax激活
exp_x = np.exp(x - np.max(x))
output = exp_x / np.sum(exp_x)
PyTorch 对比¶
NumPy 实现与 PyTorch 结果对比验证正确性。
⚠️ 注意事项¶
- 部分测试需要 PyTorch
- 如果未安装 PyTorch ,相关测试会被自动跳过
- CUDA 测试需要 GPU 支持
作者: AI 学习教程 用途: 验证神经网络组件的正确性