walkerlala
walkerlala

Reputation: 1680

tf.map_fn & tf.range confusing result

Here is the reproducible code:

def _test_fn(tp):
    tp0 = tp[0]
    tp1 = tp[1]
    result = tf.range(tp0, tp1)
    return result

ll = tf.constant([[1,4], [5, 7]])

result = tf.map_fn(lambda tp: _test_fn(tp), ll)

sess = tf.Session()
sess.run(result)

This code is expected to output a [[1,2,3], [5,6]]. However, I get an error of:

InvalidArgumentError (see above for traceback): TensorArray has inconsistent shapes. Index 0 has shape: [3] but index 1 has shape: [2]

Do I misunderstand the usage of tf.range() and tf.map_fn() or is it a bug?

Upvotes: 0

Views: 457

Answers (1)

xdurch0
xdurch0

Reputation: 10475

The first application of _test_fn will return range(1,4), which is [1,2,3]. The second application will return range(5,7), which is [5,6]. Tensorflow will then attempt to put all of these into one tensor, i.e. [[1,2,3],[5,6]]. This is not a valid tensor since the two rows have different lengths, so this code crashes. What are you trying to achieve exactly?

Upvotes: 1

Related Questions