问题描述
tf.floor
返回无梯度
这意味着 floor
操作之前的权重不会更新,对吗?
但我仍然需要梯度来更新权重
是不是很奇怪?有时我们使用floor
、ceil
...但它们不能提供渐变或者这是正确的操作
和 tf.cast
一样,梯度返回 none
tf.floor
return None gradient
it means the weights before floor
operation won't be update, right?
but I still need the gradient to update weights
Isn't it weird? sometimes we use floor
, ceil
... but they can't deliver gradient or this is right action
as well as tf.cast
, the gradient return none
这里有一个问题 #897 讨论梯度,但我不不明白为什么 return None 是好的
Here is an issue #897 to discuss the gradient, but I don't understand why return None is good
numa = tf.constant(5.3)
res1 = numa * 2
res2 = tf.floor(numa *2)
print tf.gradients(res1, numa)
# <type 'list'>: [<tf.Tensor 'gradients/mul_grad/Reshape:0' shape=() dtype=float32>]
print tf.gradients(res2, numa)
# None
我可以直接修改math_grad.py
吗?需要其他操作吗?
类似于删除 math_grad.pyc
?
Can I modify math_grad.py
directly? need other action?
something like delete math_grad.pyc
?
@ops.RegisterGradient("Floor")
def _FloorGrad(_, unused_grad):
# return [None] # before
return [unused_grad] # I want to modify
推荐答案
TensorFlow 出于实现原因使用 None
来表示 0
.如果返回的梯度是真正的梯度 0
,我认为这对您没有帮助,因为您想训练.您可以使用 gradient_override_map
将 Identity
操作的梯度替换为 Floor
如下
TensorFlow uses None
to represent 0
for implementation reasons. I don't think it would help you if the returned gradient was true gradient 0
, since you want to train. You could use gradient_override_map
to substitute gradient of Identity
op for Floor
as follows
tf.reset_default_graph()
x = tf.Variable(10.)
with tf.get_default_graph().gradient_override_map({"Floor": "Identity"}):
x2 = tf.floor(x)
loss = tf.square(x2)
opt = tf.train.GradientDescentOptimizer(0.1)
train_op = opt.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(10):
print(sess.run([loss, train_op]))
这篇关于tf.floor 的梯度是 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!