paddle深度学习基础之训练调试与优化
前言
上一节咱们讨论了四种不同的优化算法,这一节,咱们讨论训练过程中的优化问题。本次代码修改模型全是在卷积神经网络
文章目录
网络结构
优化思路
- 计算分类准确率,观测模型训练效果
- 检查模型训练过程,通过输出训练过程中的某些参数或者中间结果,识别潜在问题
- 加入校验或测试,更好评价模型效果
- 加入正则化项,避免模型过拟合
- 可视化分析
一、计算模型的分类准确率
通过计算训练的准确度,能够比较直接的反应模型的精准程度。
在paddle框架中,我们可以使用自带的准确率计算方法:
fluit.layers.accuracy(prediction,lable)
第一个参数是预测值,第二个参数是实际标签值。下面是代码中需要修改的地方:
def forward(self, inputs,label):
conv1 = self.conv1(inputs)
pool1 = self.pool1(conv1)
conv2 = self.conb2(pool1)
pool2 = self.pool2(conv2)
pool2 = fluid.layers.reshape(pool2, [pool2.shape[0], -1])
outputs = self.linear(pool2)
if label is not None:#添加
acc = fluid.layers.accuracy(input=outputs,label=label)#添加
return outputs,acc
else:
return outputs
输出结果:
epoch: 0, batch: 0, loss is: [2.796657], acc is [0.04]
epoch: 0, batch: 200, loss is: [0.50403804], acc is [0.88]
epoch: 0, batch: 400, loss is: [0.2659506], acc is [0.92]
epoch: 1, batch: 0, loss is: [0.22079289], acc is [0.92]
epoch: 1, batch: 200, loss is: [0.23240374], acc is [0.92]
epoch: 1, batch: 400, loss is: [0.16370663], acc is [0.95]
epoch: 2, batch: 0, loss is: [0.37291032], acc is [0.92]
epoch: 2, batch: 200, loss is: [0.23772442], acc is [0.92]
epoch: 2, batch: 400, loss is: [0.18071894], acc is [0.95]
epoch: 3, batch: 0, loss is: [0.15938215], acc is [0.95]
epoch: 3, batch: 200, loss is: [0.21112804], acc is [0.92]
epoch: 3, batch: 400, loss is: [0.05794979], acc is [0.99]
epoch: 4, batch: 0, loss is: [0.24466723], acc is [0.93]
epoch: 4, batch: 200, loss is: [0.14045799], acc is [0.96]
epoch: 4, batch: 400, loss is: [0.12366832], acc is [0.94]
二、检查模型训练过程
在我们训练模型时,时常会出现结果和我们预期有很大差距。此时,我们就想了解训练过程中数据的变化过程。恰巧,paddle深度学习框架支持这些功能,我们一起去看看如何做的:
class MNIST(fluid.dygraph.Layer):
def __init__(self):
super(MNIST, self).__init__()
# self.linear1 = Linear(input_dim=28*28,output_dim=10,act=None)
# self.linear2 = Linear(input_dim=10,output_dim=10,act='sigmoid')
# self.linear3 = Linear(input_dim=10,output_dim=1,act='sigmoid')
self.conv1 = Conv2D(num_channels=1, num_filters=20, filter_size=5, stride=1, padding=2, act='relu')
self.pool1 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
self.conb2 = Conv2D(num_channels=20, num_filters=20, filter_size=5, stride=1, padding=2, act='relu')
self.pool2 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
self.linear = Linear(input_dim=980, output_dim=10, act='softmax')
def forward(self, inputs,label,check_shape=False,check_content=