tensorflow Optimizer.minimize()和gradient clipping

Posted esoteric

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow Optimizer.minimize()和gradient clipping相关的知识,希望对你有一定的参考价值。

在tensorflow中通常使用下述方法对模型进行训练

# 定义Optimizer
opt = tf.train.AdamOptimizer(lr)
# 定义train
train = opt.minimize(loss)

for i in range(100):
    sess.run(train)

train指向的是tf.Graph中关于训练的节点,其中opt.minimize(loss)相当不直观,它相当于

# 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).  

# Ask the optimizer to apply the gradients.
opt.apply_gradients(grads_and_vars)

即建立了求梯度的节点和optimizer根据梯度对变量进行修改的节点

因此,可以通过下述方法对梯度进行修改

grads_and_vars = opt.compute_gradients(loss, <list of variables>)
capped_grads_and_vars = [(MyCapper(grad), var) for grad, var in grads_and_vars]
opt.apply_gradients(capped_grads_and_vars)

举两个例子

# tf.clip_by_value(
#     t,
#     clip_value_min,
#     clip_value_max,
#     name=None
# )

grads_and_vars = opt.compute_gradients(loss)
capped_grads_and_vars = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in grads_and_vars]
opt.apply_gradients(capped_grads_and_vars)
# tf.clip_by_global_norm(
#     t_list,
#     clip_norm,
#     use_norm=None,
#     name=None
# )
# Returns:
#     list_clipped: A list of Tensors of the same type as list_t.
#     global_norm: A 0-D (scalar) Tensor representing the global norm.

opt = tf.train.AdamOptimizer(lr)
grads, vars = zip(*opt.compute_gradients(loss))
grads, _ = tf.clip_by_global_norm(grads, 5.0)
train = opt.apply_gradients(zip(grads, vars))

 

以上是关于tensorflow Optimizer.minimize()和gradient clipping的主要内容,如果未能解决你的问题,请参考以下文章

如何让 Tensorflow Profiler 在 Tensorflow 2.5 中使用“tensorflow-macos”和“tensorflow-metal”工作

python [test tensorflow] test tensorflow installation #tensorflow

关于tensorflow的显存占用问题

java调用tensorflow训练好的模型

tensorflow新手必看,tensorflow入门教程,tensorflow示例代码

tensorflow 如何在线训练模型