title: matrix determinant differentiation in tensorflow
body: <p>I am interested in computing the derivative of a matrix determinant using TensorFlow. I can see from experimentation that TensorFlow has not implemented a method of differentiating through a determinant:</p>
  
  <pre><code>LookupError: No gradient defined for operation 'MatrixDeterminant' 
  (op type: MatrixDeterminant)
  </code></pre>
  
  <p>A little further investigation revealed that it is actually possible to compute the derivative; see for example <a href="https://en.wikipedia.org/wiki/Jacobi%27s_formula" rel="noreferrer">Jacobi's formula</a>. I determined that in order to implement this means of differentiating through a determinant that I need to use the function decorator,</p>
  
  <pre><code>@tf.RegisterGradient("MatrixDeterminant")
  def _sub_grad(op, grad):
      ...
  </code></pre>
  
  <p>However, I am not familiar enough with tensor flow to understand how this can be accomplished. Does anyone have any insight on this matter?</p>
  
  <p>Here's an example where I run into this issue:</p>
  
  <pre><code>x = tf.Variable(tf.ones(shape=[1]))
  y = tf.Variable(tf.ones(shape=[1]))
  
  A = tf.reshape(
      tf.pack([tf.sin(x), tf.zeros([1, ]), tf.zeros([1, ]), tf.cos(y)]), (2,2)
  )
  loss = tf.square(tf.matrix_determinant(A))
  
  
  optimizer = tf.train.GradientDescentOptimizer(0.001)
  train = optimizer.minimize(loss)
  
  init = tf.initialize_all_variables()
  sess = tf.Session()
  sess.run(init)
  
  
  for step in xrange(100):
      sess.run(train)
      print sess.run(x)
  </code></pre>