I have a matrix g of shape [4, 4, 2, 2] where I need to find the rank of g[0, 0], g[1, 1], g[2, 2] and g[3, 3] which are all 2x2 matrices. I used the tf.rank operator but it treats g as a single array and computes the rank and returns a single value for the whole matrix. What I need is a 2x2 matrix of ranks of the corresponding g[i, j]'s.
Following is a MWE:
import tensorflow as tf
import numpy as np
a = np.array([
 [[[ 0., 0.], [ 0., 0.]], [[-1., -1.], [-1., -1.]], [[-2., -2.], [-2., -2.]], [[-3., -3.], [-3., -3.]]], 
 [[[ 1., 1.], [ 1., 1.]], [[ 0., 0.], [ 0., 0.]], [[-1., -1.], [-1., -1.]], [[-2., -2.], [-2., -2.]]],
 [[[ 2., 2.], [ 2., 2.]], [[ 1., 1.], [ 1., 1.]], [[ 0., 0.], [ 0., 0.]], [[-1., -1.], [-1., -1.]]],
 [[[ 3., 3.], [ 3., 3.]], [[ 2., 2.], [ 2., 2.]], [[ 1., 1.], [ 1., 1.]], [[ 0., 0.], [ 0., 0.]]]
])
rank = tf.rank(a)  # Returns a number
Apart from using a for loop is there any way to get this rank matrix? Thanks.