title: Get the diagonal of a matrix in TensorFlow
body: <p>Is there a way to extract the diagonal of a square matrix in TensorFlow? That is, for a matrix like this:</p>
  
  <pre><code>[
   [0, 1, 2],
   [3, 4, 5],
   [6, 7, 8]
  ]
  </code></pre>
  
  <p>I want to fetch the elements: <code>[0, 4, 8]</code></p>
  
  <p>In numpy, this is pretty straight-forward via <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.diag.html" rel="noreferrer">np.diag</a>:</p>
  
  <p>In TensorFlow, there is a <a href="http://tensorflow.org/api_docs/python/math_ops.md#diag" rel="noreferrer">diag function</a>, but it only forms a new matrix with the elements specified in the argument on the diagonal, which is not what I want.</p>
  
  <p>I could imagine how this could be done via striding... but I don't see striding for tensors in TensorFlow.</p>