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

Support of gradient for TF.MOD #6365

Closed
philipperemy opened this issue Dec 16, 2016 · 17 comments
Closed

Support of gradient for TF.MOD #6365

philipperemy opened this issue Dec 16, 2016 · 17 comments
Assignees
Labels
type:feature Feature requests

Comments

@philipperemy
Copy link

philipperemy commented Dec 16, 2016

Hello there,

When I include tf.mod in my Graph, I have this error:

LookupError: No gradient defined for operation 'RNN/while/LSTM/Mod' (op type: Mod)

Will it be handled in the future?

Thanks!

References: https://www.tensorflow.org/api_docs/python/math_ops/arithmetic_operators#mod
Tensorflow version: 0.12.0-rc1

@philipperemy
Copy link
Author

philipperemy commented Dec 17, 2016

@ops.RegisterGradient("Mod")
def _mod_grad(op, grad):
x, y = op.inputs
gz = grad
x_grad = gz
y_grad = tf.reduce_mean(-(x // y) * gz, reduction_indices=[0], keep_dims=True)
return x_grad, y_grad

@kleinma
Copy link

kleinma commented Dec 19, 2016

I would like to use tf.mod in my graph, as well, in order to define a loss function that minimizes the error in a variable that wraps (e.g. an angle with a range of [-pi,pi)). For example, if
a1 = pi - eps and a2 = -pi + eps,
then I would like
a1 - a2 = -2 * eps,
as opposed to
a1 - a2 = -2 * eps + 2 * pi.

In order to test the above solution, must I rebuild Tensorflow from source? I'm currently using 0.12.0rc1.
Thanks!

Edit - Instructions for registering a gradient function can be found here: https://www.tensorflow.org/how_tos/adding_an_op/#implement_the_gradient_in_python

@yaroslavvb
Copy link
Contributor

@kleinma For Python-only changes such as above, you need not rebuild TensorFlow from source

@aselle
Copy link
Contributor

aselle commented Dec 22, 2016

I'm not sure if mod in models is well-defined enough to know what the gradient should be in all cases. The angle case makes sense, but lots of uses of mod (binning, etc) are inherently discontinuous, certainly with integers. @girving, do you think we should provide a default mod gradient, or just let people override them appropriately for their application when they see this error?

@girving
Copy link
Contributor

girving commented Dec 30, 2016

The gradient of mod is zero a.e., so it's TensorFlow gradient is zero everywhere. We should definitely define a zero gradient (returning None).

@girving girving removed their assignment Dec 30, 2016
@girving girving removed the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Dec 30, 2016
@girving
Copy link
Contributor

girving commented Dec 30, 2016

@aselle Unassigning myself since I'm on leave.

@michaelisard
Copy link

@sherrym would you take a look since @girving is out? It sounds as if the gradient should be 0 for mod as defined, but there would be some customers for an angular_mod with a different gradient.

@philipperemy
Copy link
Author

Any news guys? @girving

@harpone
Copy link

harpone commented Mar 13, 2017

Wait a sec guys! The gradient of tf.mod(x, y) w.r.t x is 1, except for x / y = integer where it is not defined... but since these points are a set of measure zero, the gradient should always be one (?). Gradient of int division (//) or floor is of course zero...

One plot says it all:

xvals = np.linspace(0, 10, 100)
yvals = xvals % 3.14
plt.plot(xvals, yvals)

Anyway, looking forward for the gradients!

@girving
Copy link
Contributor

girving commented Mar 13, 2017

Ah, thought we were talking about integer mod, which has no gradient. Yes, float mod has gradient 1 w.r.t. x a.e., and also an a.e. defined gradient w.r.t. y.

@harpone
Copy link

harpone commented Mar 13, 2017

OK actually I just assumed tf.mod = % but didn't think about it further...

@philipperemy
Copy link
Author

@harpone check this out: http://math.stackexchange.com/questions/1849280/derivative-of-remainder-function-wrt-denominator

@girving
Copy link
Contributor

girving commented Jun 16, 2017

@yuefengz Can you take a look?

@yuefengz
Copy link
Contributor

That stackexchange link is very helpful. I will implement the gradient function.

@philipperemy
Copy link
Author

@yuefengz thanks!

@philipperemy
Copy link
Author

@yuefengz any updates on your part?

@yuefengz yuefengz assigned yuefengz and unassigned yuefengz Sep 27, 2017
@yuefengz
Copy link
Contributor

@yuntatsai is working on a change, we will check it soon. Thanks for your patience.

@sb2nov sb2nov closed this as completed in eb25081 Oct 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature Feature requests
Projects
None yet
Development

No branches or pull requests

9 participants