Currently, you can extract diagonal elements using tf.diag_part . Here is an example of them:
# 'input' is [[1, 0, 0, 0] [0, 2, 0, 0] [0, 0, 3, 0] [0, 0, 0, 4]] tf.diag_part(input) ==> [1, 2, 3, 4]
The old answer (when diag_part) is unavailable (still relevant if you want to achieve something that isnβt right now):
After looking at the mathematical operations and tensor transformations , it seems that such an operation does not exist. Even if you can extract this data using matrix multiplication, it will be inefficient (get the diagonal O(n) ).
You have three approaches, starting with simple and complex.
- Evaluate the tensor, extract the diagonal using numpy, create a variable with TF
- Use tf.pack in the way Anurag suggested (also retrieve the value 3 with
tf.shape - Write your own op in C ++ , rebuild TF and use it natively.
source share