{"action":"closed","issue":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/824","repository_url":"https://api.github.com/repos/tensorflow/tensorflow","labels_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/824/labels{/name}","comments_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/824/comments","events_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/824/events","html_url":"https://github.com/tensorflow/tensorflow/issues/824","id":127789162,"number":824,"title":"Feature parity of tf.diag with np.diag","user":{"login":"yaroslavvb","id":23068,"avatar_url":"https://avatars.githubusercontent.com/u/23068?v=3","gravatar_id":"","url":"https://api.github.com/users/yaroslavvb","html_url":"https://github.com/yaroslavvb","followers_url":"https://api.github.com/users/yaroslavvb/followers","following_url":"https://api.github.com/users/yaroslavvb/following{/other_user}","gists_url":"https://api.github.com/users/yaroslavvb/gists{/gist_id}","starred_url":"https://api.github.com/users/yaroslavvb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yaroslavvb/subscriptions","organizations_url":"https://api.github.com/users/yaroslavvb/orgs","repos_url":"https://api.github.com/users/yaroslavvb/repos","events_url":"https://api.github.com/users/yaroslavvb/events{/privacy}","received_events_url":"https://api.github.com/users/yaroslavvb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tensorflow/tensorflow/labels/contributions%20welcome","name":"contributions welcome","color":"009800"},{"url":"https://api.github.com/repos/tensorflow/tensorflow/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-01-20T21:35:41Z","updated_at":"2016-03-09T22:19:44Z","closed_at":"2016-03-09T22:19:44Z","body":"np.diag can\r\n\r\n1) take 2d array and produce 1d vector of diagonal entries\r\n2) take 1d array and produce diagonal 2d array (inverse of case 1)\r\n\r\ntf.diag only supports 2) and the behavior for case 1) is undocumented\r\n\r\nCurrently to do 1) you need to do something like this:\r\n\r\n    def identity_matrix(n):\r\n      \"\"\"Returns nxn identity matrix.\"\"\"\r\n      # note, if n is a constant node, this assert node won't be executed,\r\n      # this error will be caught during shape analysis \r\n      assert_op = tf.Assert(tf.greater(n, 0), [\"Matrix size must be positive\"])\r\n      with tf.control_dependencies([assert_op]):\r\n        ones = tf.fill(n, 1)\r\n        diag = tf.diag(ones)\r\n      return diag\r\n    \r\n    def extract_diagonal(tensor):\r\n      \"\"\"Extract diagonal of a square matrix.\"\"\"\r\n      \r\n      shape = tf.shape(tensor)\r\n      n = shape[0]\r\n      assert_op = tf.Assert(tf.equal(shape[0], shape[1]), [\"Can't get diagonal of \"\r\n                                                           \"a non-square matrix\"])\r\n      \r\n      with tf.control_dependencies([assert_op]):\r\n      return tf.reduce_sum(tf.mul(tensor, identity_matrix(n)), [0])"}}