python写cnn识别mnist
时间: 2024-12-19 14:15:26 浏览: 4
在Python中,使用卷积神经网络(Convolutional Neural Networks, CNN)来识别MNIST手写数字数据集是一个常见的机器学习项目。以下是一个简单的步骤概述:
1. **安装库**:
首先,确保已安装必要的库,如TensorFlow或PyTorch。可以使用pip命令安装:
```
pip install tensorflow keras (或) pip install torch torchvision
```
2. **导入模块**:
导入所需的模块,例如`tf.keras`(如果使用TensorFlow)或`torch.nn`(如果使用PyTorch):
```python
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# 或者
import torch
import torch.nn as nn
import torch.optim as optim
```
3. **加载MNIST数据**:
使用TensorFlow的`tf.keras.datasets.mnist`或PyTorch的`torchvision.datasets.MNIST`加载数据:
```python
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data() # TensorFlow
# 或者
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=..., download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=...)
```
4. **预处理数据**:
将图像归一化到0-1之间,并调整成合适的输入形状(通常是`(28, 28, 1)` for grayscale images):
```python
train_images = train_images / 255.0
test_images = test_images / 255.0
train_images = train_images[..., np.newaxis]
test_images = test_images[..., np.newaxis] # 添加额外维度
```
5. **构建CNN模型**:
定义一个包含卷积层、池化层、扁平化层以及全连接层的模型:
```python
# TensorFlow
model = tf.keras.models.Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# PyTorch
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(64 * 7 * 7, 128)
self.fc2 = nn.Linear(128, 10)
model = Net()
```
6. **编译和训练模型**:
设置损失函数(如交叉熵)、优化器(如Adam)和评估指标(如准确率):
```python
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# TensorFlow
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
# PyTorch
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
model.fit(dataloader=train_loader, epochs=10, val_dataloaders=test_loader)
```
7. **评估模型**:
使用测试数据集评估模型性能:
```python
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")
```
阅读全文