tensorflow 中的tf.gradients 与 tf.stop_gradient() 函数

转载网址:http://blog.csdn.net/u012436149/article/details/53905797

gradient

tensorflow中有一个计算梯度的函数tf.gradients(ys, xs),要注意的是,xs中的x必须要与ys相关,不相关的话,会报错。
代码中定义了两个变量w1w2, 但res只与w1相关

#wrong
import tensorflow as tf

w1 = tf.Variable([[1,2]])
w2 = tf.Variable([[3,4]])

res = tf.matmul(w1, [[2],[1]])

grads = tf.gradients(res,[w1,w2])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    re = sess.run(grads)
    print(re)


错误信息
TypeError: Fetch argument None has invalid type

# right
import tensorflow as tf

w1 = tf.Variable([[1,2]])
w2 = tf.Variable([[3,4]])

res = tf.matmul(w1, [[2],[1]])

grads = tf.gradients(res,[w1])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    re = sess.run(grads)
    print(re)
#  [array([[2, 1]], dtype=int32)]



tf.stop_gradient()

阻挡节点BP的梯度

import tensorflow as tf

w1 = tf.Variable(2.0)
w2 = tf.Variable(2.0)

a = tf.multiply(w1, 3.0)
a_stoped = tf.stop_gradient(a)

# b=w1*3.0*w2
b = tf.multiply(a_stoped, w2)
gradients = tf.gradients(b, xs=[w1, w2])
print(gradients)
#输出
#[None, <tf.Tensor 'gradients/Mul_1_grad/Reshape_1:0' shape=() dtype=float32>]



可见,一个节点stop之后,这个节点上的梯度,就无法再向前BP了。由于w1变量的梯度只能来自a节点,所以,计算梯度返回的是None

a = tf.Variable(1.0)
b = tf.Variable(1.0)

c = tf.add(a, b)

c_stoped = tf.stop_gradient(c)

d = tf.add(a, b)

e = tf.add(c_stoped, d)

gradients = tf.gradients(e, xs=[a, b])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(gradients))
#输出 [1.0, 1.0]



虽然 c节点被stop了,但是a,b还有从d传回的梯度,所以还是可以输出梯度值的。

import tensorflow as tf

w1 = tf.Variable(2.0)
w2 = tf.Variable(2.0)
a = tf.multiply(w1, 3.0)
a_stoped = tf.stop_gradient(a)

# b=w1*3.0*w2
b = tf.multiply(a_stoped, w2)

opt = tf.train.GradientDescentOptimizer(0.1)

gradients = tf.gradients(b, xs=tf.trainable_variables())

tf.summary.histogram(gradients[0].name, gradients[0])# 这里会报错,因为gradients[0]是None
#其它地方都会运行正常,无论是梯度的计算还是变量的更新。总觉着tensorflow这么设计有点不好,
#不如改成流过去的梯度为0
train_op = opt.apply_gradients(zip(gradients, tf.trainable_variables()))

print(gradients)
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(train_op))
    print(sess.run([w1, w2]))



  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: `tf.gradients()`是TensorFlow用于计算梯度的函数,它的用法如下: ``` grads = tf.gradients(target, sources, colocate_gradients_with_ops=False) ``` 其,`target`是需要计算梯度的Tensor或Operation,`sources`是需要计算`target`对其梯度的Tensor或Operation的列表(可以是一个Tensor或Operation,也可以是一个Tensor或Operation的列表),`colocate_gradients_with_ops`是一个布尔值,用于指定计算梯度的操作是否应该与源操作放在同一个设备上(默认为`False`)。 `tf.gradients()`的返回值`grads`是一个与`sources`相同长度的列表,其每个元素是对应源Tensor或Operation的梯度。如果某个源Tensor或Operation的梯度不存在,则对应的元素为`None`。 需要注意的是,`tf.gradients()`只能计算标量对Tensor或Operation的梯度,因此如果`target`不是标量,则需要通过将`target`的多个元素相加来将其转换为标量。另外,`tf.gradients()`计算出的梯度通常需要通过调用优化器来更新模型参数。 ### 回答2: tf.gradients 是 TensorFlow 的一个函数,用于计算某个张量对于某些其他张量的梯度。梯度是一个向量,表示了张量在每个维度上的变化率。 该函数的输入参数包括目标张量和某个可训练的张量列表。目标张量是需要计算梯度的张量,而可训练的张量列表是计算梯度所需要的参考张量。 tf.gradients 函数利用 TensorFlow 的自动微分技术(即反向传播算法)来计算张量的梯度。在计算过程TensorFlow 会自动构建计算图并追踪所有相关操作,然后根据链式法则计算梯度。 通过调用 tf.gradients 函数,我们可以获得目标张量对于参考张量的梯度。得到的梯度可以被用于更新模型参数、计算损失函数关于模型参数的梯度等。 总之,tf.gradients 函数TensorFlow 扮演着重要的角色,用于计算张量的梯度。它使得我们可以方便地进行自动微分,并利用梯度来优化模型和进行训练。 ### 回答3: tf.gradients是TensorFlow的一个函数,用于计算某个目标张量相对于输入张量的梯度。梯度可以理解为目标张量对输入张量的变化率。 tf.gradients函数接收两个参数:目标张量和输入张量(或一组输入张量)。它返回一个与输入张量维度相同的列表,表示目标张量相对于每个输入张量的梯度。 例如,假设有一个简单的线性模型y = mx + b,其m和b是可训练的变量,x是输入张量。我们想要计算目标张量y相对于输入张量x的梯度。可以使用以下代码: import tensorflow as tf # 定义模型参数 m = tf.Variable(2.0) b = tf.Variable(1.0) # 定义输入张量 x = tf.constant(3.0) # 定义目标张量 y = tf.multiply(m, x) + b # 计算梯度 grads = tf.gradients(y, x) # 创建会话并运行计算图 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) gradients = sess.run(grads) print(gradients) 运行上述代码,可以得到目标张量y相对于输入张量x的梯度,结果为[2.0]。这表示当输入张量x增加1个单位时,目标张量y增加2个单位。 通过使用tf.gradients函数,我们可以自动计算模型各个参数相对于目标张量的梯度,进而使用梯度下降等优化算法来更新模型参数,使其不断逼近最优解。这是深度学习非常重要的一个操作,有助于实现模型的训练和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值