FtrlOptimizer

class paddle.fluid.optimizer.FtrlOptimizer(learning_rate, l1=0.0, l2=0.0, lr_power=-0.5, regularization=None, name=None)[源代码]

该接口实现FTRL (Follow The Regularized Leader) Optimizer.

FTRL 原始论文: ( https://www.eecs.tufts.edu/~dsculley/papers/ad-click-prediction.pdf)

\[\begin{split}&\qquad new\_accum=squared\_accum+grad^2\\\\ &\qquad if(lr\_power==−0.5):\\ &\qquad \qquad linear\_accum+=grad-\frac{\sqrt{new\_accum}-\sqrt{squared\_accum}}{learning\_rate*param}\\ &\qquad else:\\ &\qquad \qquad linear\_accum+=grad-\frac{new\_accum^{-lr\_power}-accum^{-lr\_power}}{learning\_rate*param}\\\\ &\qquad x=l1*sign(linear\_accum)−linear\_accum\\\\ &\qquad if(lr\_power==−0.5):\\ &\qquad \qquad y=\frac{\sqrt{new\_accum}}{learning\_rate}+(2*l2)\\ &\qquad \qquad pre\_shrink=\frac{x}{y}\\ &\qquad \qquad param=(abs(linear\_accum)>l1).select(pre\_shrink,0.0)\\ &\qquad else:\\ &\qquad \qquad y=\frac{new\_accum^{-lr\_power}}{learning\_rate}+(2*l2)\\ &\qquad \qquad pre\_shrink=\frac{x}{y}\\ &\qquad \qquad param=(abs(linear\_accum)>l1).select(pre\_shrink,0.0)\\\\ &\qquad squared\_accum+=grad^2\end{split}\]
参数:
  • learning_rate (float|Variable)- 全局学习率。
  • l1 (float,可选) - L1 regularization strength,默认值0.0。
  • l2 (float,可选) - L2 regularization strength,默认值0.0。
  • lr_power (float,可选) - 学习率降低指数,默认值-0.5。
  • regularization - 正则化器,例如 fluid.regularizer.L2DecayRegularizer
  • name (str, 可选) - 可选的名称前缀,一般无需设置,默认值为None。
抛出异常:
  • ValueError - 如果 learning_rate , rho , epsilon , momentum 为 None.

代码示例

import paddle
import paddle.fluid as fluid
import numpy as np

place = fluid.CPUPlace()
main = fluid.Program()
with fluid.program_guard(main):
    x = fluid.layers.data(name='x', shape=[13], dtype='float32')
    y = fluid.layers.data(name='y', shape=[1], dtype='float32')
    y_predict = fluid.layers.fc(input=x, size=1, act=None)
    cost = fluid.layers.square_error_cost(input=y_predict, label=y)
    avg_cost = fluid.layers.mean(cost)

    ftrl_optimizer = fluid.optimizer.Ftrl(learning_rate=0.1)
    ftrl_optimizer.minimize(avg_cost)

    fetch_list = [avg_cost]
    train_reader = paddle.batch(
        paddle.dataset.uci_housing.train(), batch_size=1)
    feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())
    for data in train_reader():
        exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list)

注意:目前, FtrlOptimizer 不支持 sparse parameter optimization。

minimize(loss, startup_program=None, parameter_list=None, no_grad_set=None, grad_clip=None)

通过更新parameter_list来添加操作,进而使损失最小化。

该算子相当于backward()和apply_gradients()功能的合体。

参数:
  • loss (Variable) – 用于优化过程的损失值变量
  • startup_program (Program) – 用于初始化在parameter_list中参数的startup_program
  • parameter_list (list) – 待更新的Variables组成的列表
  • no_grad_set (set|None) – 应该被无视的Variables集合
  • grad_clip (GradClipBase|None) – 梯度裁剪的策略

返回: (optimize_ops, params_grads),数据类型为(list, list),其中optimize_ops是minimize接口为网络添加的OP列表,params_grads是一个由(param, grad)变量对组成的列表,param是Parameter,grad是该Parameter对应的梯度值

返回类型: tuple