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
How to deep control gradient back propagation with Keras #956
Comments
You want to train the model or you need the gradients to do something else? If you want to train the model, just keep reading the docs and see the If you need the gradients to do other things you have to use Theano. You have to get the output of your model and, define a cost function and calculate the gradients with respect to each parameter. For example: D = T.matrix() # desired
Y = model.get_output()
Cost = ((D-Y)**2).mean()
gradients = [T.grad(Cost, p) for p in model.get_params()] |
My model is Recurive Neutral Network(RNN) + MLP. Based on your suggestion. I have two choices. model.add(MyRNN) Is that possible? |
run and understand this example, they do something like what you are doing https://github.com/fchollet/keras/blob/master/examples/lstm_text_generation.py |
@EderSantana : I don't think thats what Jerry asked. Is there some way to compute the gradients of backpropogation w.r.t each hidden layer (or input layer?). An equivalent of this in Caffe for example would be something like :
@jerryli1981 : Were you able to find a way to do this? |
@jerryli1981 Were you able to identify the way to calculate the gradient in a layer? (I am also originally a torch7 user and it is straightforward to do that. I am not sure about in Keras...) |
I'm trying to do backpropagation with MLP, do we have a way to do backward pass in Keras (usng Tensorflow)? |
@jemshit TensorFlow allows that using opt.apply_gradients method, as shown here: https://www.tensorflow.org/api_docs/python/tf/train/Optimizer or here: https://github.com/fchollet/keras/blob/master/keras/optimizers.py#L592 |
Were you guys able to resolve this issue? I have to back propagate the error but at each time step the derivative is different and I have to manipulate that. How is that possible in Keras/Tensor flow? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed. |
More or less the same question here: How can I backpropagate a specific error value in a Keras model? Thanks |
I couldn't figure this out. I ended up using Pytorch. Pytorch gives you this capability. |
@jemshit Hi! Have you solved this problem? |
No need to do anything manually. Optimizer algorithm (SGD, Adadelta...)
will use backpropagation.
…On Wed, Oct 3, 2018, 19:26 jiangnan ***@***.***> wrote:
@jemshit <https://github.com/jemshit> Hi! Have you solved this problem?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#956 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB28nNUU_P8qyz7iH1dyAL4hEz10ESxkks5uhOUhgaJpZM4GdEPs>
.
|
@jemshit ,i think what @jerryli1981 meant is to be able to apply a function on gradient at each stage of backprop or forwardpass. for example what if you want to binarize(quantize) gradient on each backprob which is widely used in quantized model |
|
Is there any way to do this? or with tf.keras? |
Hi @theceday I also need to manually backprop gradients in keras. Did you managed? |
ya finally but it's super slow. What are you using it for? you can batch
train if you are trying for reinforcement learning.
…On Tue, Apr 23, 2019 at 8:12 PM Mauricio ***@***.***> wrote:
Hi @theceday <https://github.com/theceday> I also need to manually
backprop gradients in keras. Did you managed?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#956 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAWKJTCMIGQVSKG6ZRO3B73PR7F25ANCNFSM4BTUIPWA>
.
|
@theceday I am in the process of. Just calculated gradients outside the computation graph (I can see them in my termnail). Now I need to update each weight accordingly, I guess I'm doing what an optimizer does for you, however, as you know RL models differ a bit from keras internals, so that's why I am 'on foot' here. That's also why model.train_on_batch() does not fit my needs either. |
did you see my example?
…On Wed, Apr 24, 2019 at 10:26 AM Mauricio ***@***.***> wrote:
@theceday <https://github.com/theceday> I am in the process of. Just
calculated gradients outside the computation graph (I can see them in my
termnail). Now I need to update each weight accordingly, I guess I'm doing
what an optimizer does for you, however, as you know RL models differ a bit
from keras internals, so that's why I am 'on foot' here. That's also why
model.train_on_batch() does not fit my needs either.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#956 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAWKJTFUV7AQFFU4P65RF5DPSCJ6FANCNFSM4BTUIPWA>
.
|
I am not sure everyone has the same case, but I was trying to backrop a custom loss value (numpy array/input tensor), I have used K.switch/tf.cond with no luck. As far I understand so far, tf doesnt backprop those seperate branches. Maybe instead of using K.switch, returning a loss expression containing both tensors (actual and custom) might work, but I am not sure if it allows such a expression I might give another try for this, if I have time Edit: There is a listed change in tensorflow 2.0 alpha release: That hints there could be some changes in tf&tf.keras that could help the issue, but I am not sure atm |
Hi All,
I would like to know how to write code to conduct gradient back propagation. Like Lua does below,
local sim_grad = self.criterion:backward(output, targets[j])
local rep_grad = self.MLP:backward(rep, sim_grad)
Keras's example teach me how to construct sequential model like below,
model = Sequential()
model.add(Dense(128, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))
However, it is not enough for me. I need generate gradient for this model. How can write code to control sequential model backward propagation?
Thanks
The text was updated successfully, but these errors were encountered: