# 使用LeNet在MNIST数据集实现图像分类¶

## 一、环境配置¶

```import paddle

```
```2.3.0
```

## 二、数据加载¶

```from paddle.vision.transforms import Compose, Normalize

transform = Compose([Normalize(mean=[127.5], std=[127.5], data_format="CHW")])
# 使用transform对数据集做归一化
```

```import numpy as np
import matplotlib.pyplot as plt

train_data0, train_label_0 = train_dataset[0][0], train_dataset[0][1]
train_data0 = train_data0.reshape([28, 28])
plt.figure(figsize=(2, 2))
plt.imshow(train_data0, cmap=plt.cm.binary)
print("train_data0 label is: " + str(train_label_0))
```
```train_data0 label is: [5]
```

## 三、组网¶

```import paddle

def __init__(self):
super().__init__()
)
in_channels=6, out_channels=16, kernel_size=5, stride=1
)
in_features=16 * 5 * 5, out_features=120
)

def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.max_pool2(x)
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
x = F.relu(x)
x = self.linear3(x)
return x
```

## 四、方式1：基于高层API，完成模型的训练与预测¶

### 4.1 使用 `Model.fit`来训练模型¶

```from paddle.metric import Accuracy

learning_rate=0.001, parameters=model.parameters()
)

# 配置模型
```
```# 训练模型
model.fit(train_dataset, epochs=2, batch_size=64, verbose=1)
```

### 4.2 使用 `Model.evaluate` 来预测模型¶

```model.evaluate(test_dataset, batch_size=64, verbose=1)
```
```Eval begin...
step 157/157 [==============================] - loss: 4.2854e-04 - acc: 0.9841 - 7ms/step
Eval samples: 10000

{'loss': [0.00042853763], 'acc': 0.9841}
```

## 五、方式2：基于基础API，完成模型的训练与预测¶

### 5.1 模型训练¶

```import paddle.nn.functional as F

# 加载训练集 batch_size 设为 64
def train(model):
model.train()
epochs = 2
learning_rate=0.001, parameters=model.parameters()
)
for epoch in range(epochs):
x_data = data[0]
y_data = data[1]
predicts = model(x_data)
loss = F.cross_entropy(predicts, y_data)
# 计算损失
loss.backward()
if batch_id % 300 == 0:
print(
"epoch: {}, batch_id: {}, loss is: {}, acc is: {}".format(
epoch, batch_id, loss.numpy(), acc.numpy()
)
)
optim.step()

model = LeNet()
train(model)
```
```epoch: 0, batch_id: 0, loss is: [2.9878871], acc is: [0.140625]
epoch: 0, batch_id: 300, loss is: [0.22775462], acc is: [0.921875]
epoch: 0, batch_id: 600, loss is: [0.06251755], acc is: [0.984375]
epoch: 0, batch_id: 900, loss is: [0.1097075], acc is: [0.96875]
epoch: 1, batch_id: 0, loss is: [0.04311676], acc is: [0.984375]
epoch: 1, batch_id: 300, loss is: [0.00150577], acc is: [1.]
epoch: 1, batch_id: 600, loss is: [0.08764459], acc is: [0.96875]
epoch: 1, batch_id: 900, loss is: [0.14419323], acc is: [0.9375]
```

### 5.2 模型验证¶

```test_loader = paddle.io.DataLoader(
)

# 加载测试数据集
def test(model):
model.eval()
batch_size = 64
x_data = data[0]
y_data = data[1]
predicts = model(x_data)
# 获取预测结果
loss = F.cross_entropy(predicts, y_data)
if batch_id % 20 == 0:
print(
"batch_id: {}, loss is: {}, acc is: {}".format(
batch_id, loss.numpy(), acc.numpy()
)
)

test(model)
```
```batch_id: 0, loss is: [0.01201783], acc is: [1.]
batch_id: 20, loss is: [0.09013407], acc is: [0.984375]
batch_id: 40, loss is: [0.07025866], acc is: [0.96875]
batch_id: 60, loss is: [0.08602518], acc is: [0.984375]
batch_id: 80, loss is: [0.00779913], acc is: [1.]
batch_id: 100, loss is: [0.00508764], acc is: [1.]
batch_id: 120, loss is: [0.00401443], acc is: [1.]
batch_id: 140, loss is: [0.03930391], acc is: [0.96875]
```