本文介绍了tensorflow map_fn TensorArray 的形状不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 map_fn 函数,注意到它输出一个 TensorArray,这应该意味着它能够输出锯齿状"张量(其中内部的张量具有不同的第一维.

I am playing around with the map_fn function, and noticed that it outputs a TensorArray, which should mean it is capable of outputting "jagged" tensors (where the tensors on the inside have different first dimensions.

我尝试使用此代码查看此操作:

I tried to see this in action with this code:

import tensorflow as tf
import numpy as np

NUM_ARRAYS = 1000
MAX_LENGTH = 1000

lengths = tf.placeholder(tf.int32)
tArray = tf.map_fn(lambda x: tf.random_normal((x,), 0, 1),
                   lengths,
                   dtype=tf.float32) # Should return a TensorArray.

# startTensor =  tf.random_normal((tf.reduce_sum(lengths),), 0, 1)
# tArray = tf.TensorArray(tf.float32, NUM_ARRAYS)
# tArray = tArray.split(startTensor, lengths)
# outArray = tArray.concat()


with tf.Session() as sess:
    outputArray, l = sess.run(
        [tArray, lengths],
        feed_dict={lengths: np.random.randint(MAX_LENGTH, size=NUM_ARRAYS)})
    print outputArray.shape, l

但是得到了错误:

TensorArray 的形状不一致.索引 0 的形状为:[259],但索引 1 的形状为:[773]"

"TensorArray has inconsistent shapes. Index 0 has shape: [259] but index 1 has shape: [773]"

这当然让我感到惊讶,因为我认为 TensorArrays 应该能够处理它.我错了吗?

This of course comes as a surprise to me since I am under the impression that TensorArrays should be able to handle it. Am I wrong?

推荐答案

虽然tf.map_fn() 确实使用了 tf.TensorArray 对象内部,而一个 tf.TensorArray 可以容纳不同大小的对象,这个程序不能按原样运行,因为 tf.map_fn() 通过将元素堆叠在一起,将其 tf.TensorArray 结果转换回 tf.Tensor ,而这个操作失败了.

While the tf.map_fn() does use tf.TensorArray objects internally, and a tf.TensorArray can hold objects of different size, this program won't work as-is because tf.map_fn() converts its tf.TensorArray result back to a tf.Tensor by stacking the elements together, and it is this operation that fails.

但是,您可以使用较低级别的 tf.TensorArray"noreferrer">tf.while_loop() op 代替:

You can however implement the tf.TensorArray-based using the lower-lever tf.while_loop() op instead:

lengths = tf.placeholder(tf.int32)
num_elems = tf.shape(lengths)[0]
init_array = tf.TensorArray(tf.float32, size=num_elems)

def loop_body(i, ta):
  return i + 1, ta.write(i, tf.random_normal((lengths[i],), 0, 1))

_, result_array = tf.while_loop(
    lambda i, ta: i < num_elems, loop_body, [0, init_array])

这篇关于tensorflow map_fn TensorArray 的形状不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:47