Home > Mobile >  How to iterate over a tf.tensor in eager mode
How to iterate over a tf.tensor in eager mode

Time:09-24

I'm trying to iterate over a tensor in eager mode but I can't.

Naturally, you would do something like:

probs = tf.convert_to_tensor(np.array([[1,2,3], [4,5,6], [7,8,9]]))
indexs = tf.convert_to_tensor(np.array([1, 2, 3]))

@tf.function
def iterate_tensor(probs, indexs):
    return [output[label] for output, label in zip(probs, indexs)]
iterate_tensor(probs, indexs)

But this gives the error OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed:

Another thing that I tried was:

probs = tf.convert_to_tensor(np.array([[1,2,3], [4,5,6], [7,8,9]]))
indexs = tf.convert_to_tensor(np.array([1, 2, 3]))

@tf.function
def iterate_tensor(probs, indexs):
    return tf.map_fn(lambda i: i[0][i[1]], (probs, indexs), dtype=(tf.int64, tf.int64))

iterate_tensor(probs, indexs)

Gives the error ValueError: The two structures don't have the same nested structure.

CodePudding user response:

This seems to work:

probs = tf.convert_to_tensor(np.array([[1,2,3], [4,5,6], [7,8,9]]))
indexs = tf.convert_to_tensor(np.array([1,1,1]))

@tf.function
def iterate_tensor(probs, indexs):
    return tf.linalg.diag_part(tf.gather(probs, indexs, axis=1))
iterate_tensor(probs, indexs)

Output: <tf.Tensor: shape=(3,), dtype=int64, numpy=array([2, 5, 8])>

  • Related