Skip to content

Paddle Float Point exception的问题原因

Qiao Longfei edited this page Mar 13, 2018 · 5 revisions

原因分析

batch_size 太大可能造成梯度爆炸

https://github.com/PaddlePaddle/Paddle/issues/1189#issuecomment-273680299

这个FPE会随着batch size改变而改变的原因,是在这里 https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/trainer_config_helpers/optimizers.py#L375

Paddle在实现SGD的时候,有一个和公式不同的地方。

通常,一般的公式里面解释SGD的时候,都会将这个mini-batch中所有样本产生的Gradient加起来,然后除以样本数,计算出每一个样本的平均梯度,然后再用这个平均的梯度去更新神经网络。

在Paddle里面,并没有计算每条样本的平均梯度,而是使用所有样本的整体梯度去更新网络。

这么做的原因是,

如果想要用平均梯度的话,调整learning rate其实也就可以了。 现在流行的大部分优化算法都是adaptive的,梯度的大小并不那么重要,而方向却很重要。例如adam,rmsprop 如果需要除以样本数的话,多机的情况下,样本数还需要同步一遍。写起来比较麻烦。同时,在每个pass(epoch)结束的那个mini-batch里,样本数也会有一个变化。 正因为这样,在Paddle里面增大batch_size,就会导致Gradient的尺度变得更大,Value减去Gradient的时候减的更狠,所以更容易出现FPE。

https://github.com/PaddlePaddle/Paddle/issues/1961#issuecomment-298824097 From the last two lines of the log file, the cost is increased from about 590 to 2974, I think you encounter the gradient explosion problem. One possible solution is to use gradient clipping, but I am really sorry, currently, PaddlePaddle has a problem with it (it may not be activated even if you specify the clipping threshold), we are working on it.

Gradient explosion can be avoided by carefully tuning the training parameters, maybe it is affected by the weight initialization, the learning algorithm, the learning rate, the length of the training sample, batch size and so on. All these parameters can be tried.

https://github.com/PaddlePaddle/Paddle/issues/563 训练过程中经过多个pass之后会出现float point exception错误。

#46 #53 有提到可以通过更换模型,降低batcisize、降低学习速率或者优化算法可以解决这个问题。

经过实验发现降低batcisize(128变为50)、降低学习速率(adam由1e-3变为1e-4)并没有解决这个问题。 更换优化算法(由adam变为MomentumOptimizer(0.9))也还是会出现同样问题。

不知有没有什么别的办法可以解决这个float point exception问题

(注:训练样本大约50w条数据做序列分类,label大约8w个,90%数据序列长度小于10,集群版本paddle,训练了大约80个pass)

Clone this wiki locally