Python 如何使用tf.map_fn在张量上迭代,并为每次迭代返回不同维度的值?

Python 如何使用tf.map_fn在张量上迭代,并为每次迭代返回不同维度的值?,python,tensorflow,Python,Tensorflow,我想在不使用急切执行的情况下迭代张量,为此我必须使用tf.map\u fn()。 我想做的事情如下所示: import tensorflow as tf list_of_values = tf.constant([[1, 9, 65, 43], [8, 23, 21, 48], [11, 14, 98, 21], [98, 12, 32, 12]]) def value_finder(i): def f1(): # Some computation with a local va

我想在不使用急切执行的情况下迭代张量,为此我必须使用tf.map\u fn()。 我想做的事情如下所示:

import tensorflow as tf

list_of_values = tf.constant([[1, 9, 65, 43], [8, 23, 21, 48], [11, 14, 98, 21], [98, 12, 32, 12]])

def value_finder(i):
  def f1():
    # Some computation with a local variable 'a' occurs
    # . . .
    return a  # a = [[3, 4, 5]]

  def f2():
    # Some computation with a local variable 'b' occurs
    # . . .
    return b  # b = [[7, 1, 2], [9, 3, 11]]

  return tf.cond(tf.reduce_all(tf.less(tf.slice(i, [1], [1]), tf.constant(18))), f1, f2)

value_obtained = tf.map_fn(lambda i: value_finder(i), list_of_values))
值a和b的维度不同,因此每当我尝试运行代码时,都会出现错误。在我的例子中,返回的值不可避免地具有不均匀维度。是否有其他方法来迭代张量和获得结果而不是填充值以使它们具有相等的维度?

也许你可以考虑使用支持可变长度张量。