Tensorflow 的求梯度函数:

[db, dW, dx] = tf.gradient(C, [b, w, x])

在调试时用处较大。

实例:

import tensorflow as tf
import numpy as np
w1 = tf.Variable(2.0)
w2 = tf.Variable(3.0)
a = tf.multiply(w1,w2)
g = tf.get_default_graph()
with g.gradient_override_map({"Sign":"Identity"}):
    clip = tf.sign(a)
gradients = tf.gradients(clip, xs=[w1, w2])
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(gradients))

 

>>[3.0,2.0]

此外,更加深度的应用见:

https://zhuanlan.zhihu.com/p/23060519 

详细的解释了tensorflow中梯度的累计以及异步实现

相关文章:

  • 2022-01-30
  • 2021-12-20
  • 2021-04-11
  • 2021-08-01
猜你喜欢
  • 2021-07-16
  • 2021-12-08
  • 2021-11-06
  • 2021-06-12
  • 2021-08-01
  • 2021-07-31
相关资源
相似解决方案