Using Op Inputs When Defining Custom Gradients In Tensorflow
I'm trying to define a gradient method for my custom TF operation. Most of the solutions I have found online seem to based on a gist by harpone. I'm reluctant to use that approach
Solution 1:
There is no problem with your code:
Let's first do the forward pass:
var_foo = 5
-> bar = 125
-> tf.identity(bar) = 125
Now let's backpropagate:
The gradient of tf.identity(bar)
with respect to its argument bar
equals (by your definition) to bar
, that is, 125
. The gradient of bar
with respect to var_foo
equals 3 times the square of var_foo
which is 75
. Multiply, and you get 9375
, which is indeed the output of your code.
op.inputs[0]
contains the forward-pass value of the op. In this case, the forward pass of the identity
op is 125
.
Post a Comment for "Using Op Inputs When Defining Custom Gradients In Tensorflow"