Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gradient of tf.floor #897

Closed
ppwwyyxx opened this issue Jan 26, 2016 · 6 comments
Closed

gradient of tf.floor #897

ppwwyyxx opened this issue Jan 26, 2016 · 6 comments
Assignees
Labels

Comments

@ppwwyyxx
Copy link
Contributor

import numpy as np
import tensorflow as tf
f = np.array([3.8], dtype='float32')
vif = tf.placeholder(tf.float32, shape=[1])

#out = vif + tf.cast(tf.cast(vif, tf.int32), tf.float32)
out = vif + tf.floor(vif)
grad = tf.gradients(tf.reduce_sum(out), vif)

sess = tf.Session()
sess.run(tf.initialize_all_variables())
print sess.run(grad, feed_dict={vif: f})  # print 2

import theano
import theano.tensor as T
vif = T.fvector()
out = vif + T.floor(vif)
grad = T.grad(out.sum(), vif)
func = theano.function([vif], grad)
print func(f) # print 1

It looks like the gradient of tf.floor(x) w.r.t x is 1. But I'm expecting it to be 0, as in theano.
Right now I could use a double casting as a work around, but why is it different?

@girving
Copy link
Contributor

girving commented Jan 28, 2016

Yes, the gradient of floor is always zero.

@girving
Copy link
Contributor

girving commented Jan 28, 2016

Well, almost always.

@ppwwyyxx
Copy link
Contributor Author

Should it be as simple as changing:

@ops.RegisterGradient("Floor")
def _FloorGrad(_, grad):
  return grad  # return 0

in python/ops/math_grad.py?

@girving
Copy link
Contributor

girving commented Feb 1, 2016

Yes, changing it to return [None] would be the fix.

@ppwwyyxx
Copy link
Contributor Author

ppwwyyxx commented Feb 1, 2016

Thanks. Could you explain why None is preferred over 0 ?
I observed that the gradient of tf.cast(tf.cast(vif, tf.int32), tf.float32) is also None, that's why I added vif in the example code.

@girving
Copy link
Contributor

girving commented Feb 1, 2016

None will be treated by the gradient code as "no connection", which is mathematically equivalent to zero but faster since it will never construct large zero matrices. There's some related discussion here: #783.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants