Optimizers 类进行迭代梯度修正

Training

Optimizers 类

Optimizer

The Optimizer base class provides methods to compute gradients for a loss and apply gradients to variables. A collection of subclasses implement classic optimization algorithms such as GradientDescent and Adagrad.

Optimizers这个基类提供函数用来计算梯度,并将梯度状态更新。该基类的一系类子类实现了一些经典的算法,比如说GradientDescent和Adagrad

class tf.train.Optimizer
  • 1

Base class for optimizers.

This class defines the API to add Ops to train a model. You never use this class directly, but instead instantiate one of its subclasses such as GradientDescentOptimizer, AdagradOptimizer, or MomentumOptimizer.

用于优化的基类定义了一系列API,通过这些API可以在训练模型时添加一些操作。例如:

Usage
# Create an optimizer with the desired parameters.
opt = GradientDescentOptimizer(learning_rate=0.1)

# Add Ops to the graph to minimize a cost by updating a list of variables.
# "cost" is a Tensor, and the list of variables contains variables.Variable
# objects.
opt_op = opt.minimize(cost, <list of variables>)
Execute opt_op to do one step of training:
opt_op.run()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Processing gradients before applying them.

Calling minimize() takes care of both computing the gradients and applying them to the variables. If you want to process the gradients before applying them you can instead use the optimizer in three steps: 
Compute the gradients with compute_gradients(). 
Process the gradients as you wish. 
Apply the processed gradients with apply_gradients(). 
可以通过minimize()函数来同时计算梯度并更新该梯度所对应的参数状态(其计算的就是对应的参数的梯度),若想先计算梯度,然后再将梯度对应参数状态更新,可以通过以下几步来使用:

* 利用 compute_gradients() 函数先计算梯度 
* 按照自己的需求来处理梯度 
* 调用apply_gradients() 函数来更新该梯度所对应的参数的状态。 
* 这样有一个好处是,梯度可以按照自己的需求来改动后再使用。 
例如:

# Create an optimizer.
opt = GradientDescentOptimizer(learning_rate=0.1)

# Compute the gradients for a list of variables.
grads_and_vars = opt.compute_gradients(loss, <list of variables>)

# grads_and_vars is a list of tuples (gradient, variable).  Do whatever you
# need to the 'gradient' part, for example cap them, etc.
capped_grads_and_vars = [(MyCapper(gv[0]), gv[1])) for gv in grads_and_vars]

# Ask the optimizer to apply the capped gradients.
opt.apply_gradients(capped_grads_and_vars)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值