title: Getting gradient of model output w.r.t weights using Keras
body: <p>I am interested in building reinforcement learning models with the simplicity of the Keras API. Unfortunately, I am unable to extract the gradient of the output (not error) with respect to the weights. I found the following code that performs a similar function (<a href="https://stackoverflow.com/questions/36968128/saliency-maps-of-neural-networks-using-keras">Saliency maps of neural networks (using Keras)</a>)</p>
  
  <pre><code>get_output = theano.function([model.layers[0].input],model.layers[-1].output,allow_input_downcast=True)
  fx = theano.function([model.layers[0].input] ,T.jacobian(model.layers[-1].output.flatten(),model.layers[0].input), allow_input_downcast=True)
  grad = fx([trainingData])
  </code></pre>
  
  <p>Any ideas on how to calculate the gradient of the model output with respect to the weights for each layer would be appreciated.</p>