paddlepaddle(四)训练与预测验证

目录

1.内置高级API封装训练

1.1使用paddle.Model()封装模型

1.2用Model.prepare()配置模型

1.3用Model.fit()训练模型

1.4用Model.evaluate()评估模型

1.5用Model.predict()预测模型

2.基础API实现训练与预测

2.1初始化优化器、模型、超参数

 2.2训练

2.3用基础API验证模型

2.5预测


1.内置高级API封装训练

1.1使用paddle.Model()封装模型

  • network (paddle.nn.Layer): 是 paddle.nn.Layer 的一个实例

  • inputs (InputSpec|list|dict|None, 可选): network 的输入,可以是 InputSpec 的实例,或者是一个 InputSpec 的 list ,或者是格式为 {name: InputSpec} 的 dict ,或者为 None . 默认值为 None .

  • labels (InputSpec|list|None, 可选): network 的标签,可以是 InputSpec 的实例,或者是一个 InputSpec 的 list ,或者为 None. 默认值为 None .

# 定义网络结构(采用 Sequential组网方式 )
mnist = paddle.nn.Sequential(
    paddle.nn.Flatten(1, -1),
    paddle.nn.Linear(784, 512),
    paddle.nn.ReLU(),
    paddle.nn.Dropout(0.2),
    paddle.nn.Linear(512, 10)
)

model = paddle.Model(mnist)

1.2用Model.prepare()配置模型

# 为模型训练做准备,设置优化器,损失函数和精度计算方式
model.prepare(optimizer=paddle.optimizer.Adam(parameters=model.parameters()),
              loss=paddle.nn.CrossEntropyLoss(),
              metrics=paddle.metric.Accuracy())

1.3用Model.fit()训练模型

# 启动模型训练,指定训练数据集,设置训练轮次,设置每次数据集计算的批次大小,设置日志格式
model.fit(train_dataset,
          epochs=5,
          batch_size=64,
          verbose=1)

#####
Epoch 2/5
step 938/938 [==============================] - loss: 0.0555 - acc: 0.9689 - 22ms/step          
Epoch 3/5
step 938/938 [==============================] - loss: 0.0487 - acc: 0.9781 - 22ms/step          
Epoch 4/5
step 938/938 [==============================] - loss: 0.0061 - acc: 0.9837 - 22ms/step          
Epoch 5/5
step 938/938 [==============================] - loss: 0.0900 - acc: 0.9866 - 22ms/step 

1.4用Model.evaluate()评估模型

对于训练好的模型进行评估可以使用evaluate接口,事先定义好用于评估使用的数据集后,直接调用evaluate接口即可完成模型评估操作,结束后根据在preparelossmetric的定义来进行相关评估结果计算返回。

# 用 evaluate 在测试集上对模型进行验证
eval_result = model.evaluate(test_dataset, verbose=1)

####
Eval begin...
step 10000/10000 [==============================] - loss: 3.5763e-07 - acc: 0.9794 - 2ms/step          
Eval samples: 10000

1.5用Model.predict()预测模型

# 用 predict 在测试集上对模型进行测试
test_result = model.predict(test_dataset)
print(test_result[0:10])

#####
Predict begin...
step 10000/10000 [==============================] - 2ms/step          
Predict samples: 10000
[(array([[ -4.894136  ,  -5.6600327 ,  -3.0495958 ,   2.1171691 ,
        -11.302892  ,  -5.483228  , -14.45905   ,  11.745611  ,
         -3.6458166 ,  -0.17910889]], dtype=float32), 
array([[ -5.714625 ,   4.4638777,  13.364223 ,   2.4783947, -20.74056  ,
         -0.6489969,  -4.878751 , -15.684789 ,  -1.269482 , -15.168117 ]],
      dtype=float32), 
array([[-7.527398  ,  8.128369  , -3.2447886 , -5.72277   , -1.6012611 ,
        -4.228486  , -2.8881369 , -0.27592087, -1.8160758 , -8.265086  ]],
      dtype=float32), 
array([[ 9.048569 , -7.1954203, -2.1176505, -3.7064955, -7.256583 ,
        -1.7620262,  0.7889428, -2.5328965, -6.511542 , -1.1649128]],
      dtype=float32)]

2.基础API实现训练与预测

# 定义网络结构( 采用SubClass 组网 )
class Mnist(paddle.nn.Layer):
    def __init__(self):
        super(Mnist, self).__init__()
        self.flatten = paddle.nn.Flatten()
        self.linear_1 = paddle.nn.Linear(784, 512)
        self.linear_2 = paddle.nn.Linear(512, 10)
        self.relu = paddle.nn.ReLU()
        self.dropout = paddle.nn.Dropout(0.2)

    def forward(self, inputs):
        y = self.flatten(inputs)
        y = self.linear_1(y)
        y = self.relu(y)
        y = self.dropout(y)
        y = self.linear_2(y)
        return y

 

2.1初始化优化器、模型、超参数

# 用 DataLoader 实现数据加载
train_loader = paddle.io.DataLoader(train_dataset, batch_size=64, shuffle=True)
#初始化模型,设为train模式
mnist=Mnist()
mnist.train()
# 设置迭代次数
epochs = 5

# 设置优化器
optim = paddle.optimizer.Adam(parameters=mnist.parameters())
# 设置损失函数
loss_fn = paddle.nn.CrossEntropyLoss()

 2.2训练

核心步骤

 acc = paddle.metric.accuracy(predicts, y_data)
 loss.backward()
 optim.step()
 optim.clear_grad()
for epoch in range(epochs):
    for batch_id, data in enumerate(train_loader()):

        x_data = data[0]            # 训练数据
        y_data = data[1]            # 训练数据标签
        predicts = mnist(x_data)    # 预测结果

        # 计算损失 等价于 prepare 中loss的设置
        loss = loss_fn(predicts, y_data)

        # 计算准确率 等价于 prepare 中metrics的设置
        acc = paddle.metric.accuracy(predicts, y_data)

        # 下面的反向传播、打印训练信息、更新参数、梯度清零都被封装到 Model.fit() 中
        # 反向传播
        loss.backward()
        if (batch_id+1) % 900 == 0:
            print("epoch: {}, batch_id: {}, loss is: {}, acc is: {}".format(epoch, batch_id+1, loss.numpy(), acc.numpy()))

        # 更新参数
        optim.step()

        # 梯度清零
        optim.clear_grad()

#####
epoch: 0, batch_id: 900, loss is: [0.13427278], acc is: [0.96875]
...
epoch: 4, batch_id: 900, loss is: [0.05088746], acc is: [0.96875]

2.3用基础API验证模型

初始化数据和评估函数

# 加载测试数据集
test_loader = paddle.io.DataLoader(test_dataset, batch_size=64, drop_last=True)
loss_fn = paddle.nn.CrossEntropyLoss()

mnist.eval()

评估 

for batch_id, data in enumerate(test_loader()):

    x_data = data[0]            # 测试数据
    y_data = data[1]            # 测试数据标签
    predicts = mnist(x_data)    # 预测结果

    # 计算损失与精度
    loss = loss_fn(predicts, y_data)
    acc = paddle.metric.accuracy(predicts, y_data)

    # 打印信息
    if (batch_id+1) % 30 == 0:
        print("batch_id: {}, loss is: {}, acc is: {}".format(batch_id+1, loss.numpy(), acc.numpy()))

#######
batch_id: 30, loss is: [0.13358988], acc is: [0.96875]
batch_id: 60, loss is: [0.17515801], acc is: [0.9375]
batch_id: 90, loss is: [0.05868918], acc is: [0.96875]
batch_id: 120, loss is: [0.00206844], acc is: [1.]
batch_id: 150, loss is: [0.07185207], acc is: [0.984375]

2.5预测

# 加载测试数据集
test_loader = paddle.io.DataLoader(test_dataset, batch_size=64, drop_last=True)

mnist.eval()
for batch_id, data in enumerate(test_loader()):
    x_data = data[0]
    predicts = mnist(x_data)
    # 获取预测结果
print("predict finished")

你可能感兴趣的