title: Get gradient values in a CNN with keras/tf
body: <p>I've essentially got a keras CNN with a few layers and I want to evaluate one input then get the gradients of the backpropagation algorithm. The model is set up and compiled. But I only want the first time backpropagation is run on just one input/output set, I don't care about anything else.</p>
  
  <p>Here's the relevant part of my code:</p>
  
  <pre><code># Build the model.
  model = Sequential()
  model.add(Conv2D(16, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
  model.add(Conv2D(32, (3, 3), activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  model.add(Flatten())
  model.add(Dense(32, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(digits, activation='softmax'))
  
  # Prepare the model for training.
  model.compile(loss = keras.losses.categorical_crossentropy,
                optimizer = keras.optimizers.Adadelta(),
                metrics=['accuracy'])
  </code></pre>
  
  <p>And my one set of data input and output is in x_train and y_train. So how exactly do I run that one set of data and run backpropagation based on the output expected, then actually get the gradients that keras calculated?</p>
  
  <p>EDIT: Essentially, this is an academic endeavor and what I'm trying to do is calculate the values of the gradients myself, then compare those to what keras gets. So how do I get those values?</p>