了解如何在Python(比如TensorFlow或PyTorch)中实现LeNet-5进行手写数字识别,提供具体的代码
时间: 2024-12-12 16:18:42 浏览: 25
在Python中,尤其是使用TensorFlow或PyTorch库来实现LeNet-5进行手写数字识别,大致步骤如下:
### 使用TensorFlow
```python
import tensorflow as tf
from tensorflow.keras import layers
# 构建LeNet-5模型
def create_lenet5(input_shape=(28, 28, 1)):
model = tf.keras.Sequential([
layers.Conv2D(6, (5, 5), activation='relu', input_shape=input_shape),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(16, (5, 5), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(), # 将二维数据转换为一维
layers.Dense(120, activation='relu'),
layers.Dense(84, activation='relu'),
layers.Dense(10, activation='softmax') # 输出层,10个节点代表0到9的类别
])
return model
# 加载并预处理MNIST数据
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train[..., tf.newaxis] # 添加额外的通道维度(对于灰度图像是1)
model = create_lenet5()
# 编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练模型
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
# 测试模型
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc}')
```
### 使用PyTorch
```python
import torch
import torch.nn as nn
import torchvision.transforms as transforms
# 创建LeNet-5类
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=2)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=2)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5) # Flatten the feature maps
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return F.log_softmax(x, dim=1)
# 定义预处理步骤
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
# 加载MNIST数据集
train_dataset = torchvision.datasets.MNIST(root='./data', train=True,
transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64,
shuffle=True)
model = LeNet5().to(device) # 设备选择(GPU或CPU)
criterion = nn.CrossEntropyLoss() # 损失函数
optimizer = torch.optim.SGD(model.parameters(), lr=0.001) # 优化器
# 训练过程略...
# ...
# 测试模型
test_loader = ... # 类似地加载测试数据
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Test Accuracy of the model on the 10000 test images: {100 * correct / total}%")
```
以上是基本的LeNet-5实现框架,根据具体需求,可能还需要调整超参数、添加数据增强等步骤。
阅读全文