title: Keras: calculating derivatives of model output wrt input returns [None]
body: <p>I need help with calculating derivatives for model output wrt inputs in Keras. </p>
  
  <p>I want to add a regularization functional to the loss function. The regularizer contains the derivative of the classifier function. So I tried to take the derivative of model output. The model is a MLP with one hidden layer. The dataset is MNIST. When I compile the model and take the derivative, I get [None] as the result instead of the derivative function.</p>
  
  <p>I have seen a similar post, but didn't get answer there either:
  <a href="https://stackoverflow.com/questions/45337371/taking-derivative-of-keras-model-wrt-to-inputs-is-returning-all-zeros">Taking derivative of Keras model wrt to inputs is returning all zeros</a></p>
  
  <p>Here is my code. Please help me to solve the problem.</p>
  
  <pre><code>import keras
  from keras.datasets import mnist
  from keras.models import Sequential
  from keras.layers import Dense
  from keras import backend as K
  
  num_hiddenNodes = 1024
  num_classes = 10
  
  (X_train, y_train), (X_test, y_test) = mnist.load_data()
  X_train = X_train.reshape(-1, 28 * 28)
  X_train = X_train.astype('float32')
  X_train /= 255
  y_train = keras.utils.to_categorical(y_train, num_classes)
  
  model = Sequential()
  model.add(Dense(num_hiddenNodes, activation='softplus', input_shape=(784,)))
  model.add(Dense(num_classes, activation='softmax'))
  
  # Compile the model
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  logits = model.output
  # logits = model.layers[-1].output
  print(logits)
  X = K.identity(X_train)
  # X = tf.placeholder(dtype=tf.float32, shape=(None, 784))
  print(X)
  print(K.gradients(logits, X))
  </code></pre>
  
  <p>Here is the output for the code. The two parameters are Tensors. The gradients function returns None.</p>
  
  <pre><code>Tensor("dense_2/Softmax:0", shape=(?, 10), dtype=float32)
  Tensor("Identity:0", shape=(60000, 784), dtype=float32)
  [None]
  </code></pre>