lenet pytorch
时间: 2023-10-17 15:07:04 浏览: 110
LeNet是一个经典的卷积神经网络模型,由Yann LeCun等人在1998年提出,用于手写数字识别任务。在PyTorch中,可以通过构建一个继承自`nn.Module`的LeNet类来实现LeNet模型。以下是一个简单的LeNet实现示例:
```python
import torch.nn as nn
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, kernel_size=5)
self.pool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = nn.Conv2d(6, 16, kernel_size=5)
self.pool2 = nn.MaxPool2d(kernel_size=2)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(nn.functional.relu(self.conv1(x)))
x = self.pool2(nn.functional.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.fc3(x)
return x
```
阅读全文