问题描述
我使用 [None, None, 10]
形状的 tf.placeholder()
将我的输入传递给张量.现在我想遍历输入的第一个维度,并对该维度中的每个切片应用一些函数.但是,当我尝试使用 Python for
循环执行此操作时,我收到一条错误消息,指出 Tensor
对象不可迭代".
I am passing my input to tensors using a tf.placeholder()
of shape [None, None, 10]
. Now I want to iterate over the first dimension of the input, and apply some function to each slice in that dimension. However, when I try to do this using a Python for
loop, I get an error saying that Tensor
objects are "not iterable".
有什么方法可以将输入作为形状 [None, 10]
的张量列表传递,我如何将此列表分配给占位符?或者是否有其他方法可以迭代 Tensor
的维度?
Is there any way I can pass the input as the list of tensors of shape [None, 10]
, and how could I assign this list to the placeholder? Or is there some other way to iterate over a dimension of a Tensor
?
推荐答案
这可以使用新的 tf.map_fn()
, tf.foldl()
tf.foldr()
或(最常见的)tf.scan()
高阶运算符,它们被添加到TensorFlow 0.8 版.您将使用的特定运算符取决于您要执行的计算.例如,如果您想对张量的每一行执行相同的功能并将元素打包回单个张量,您将使用 tf.map_fn()
:
p = tf.placeholder(tf.float32, shape=[None, None, 100])
def f(x):
# x will be a tensor of shape [None, 100].
return tf.reduce_sum(x)
# Compute the sum of each [None, 100]-sized row of `p`.
# N.B. You can do this directly using tf.reduce_sum(), but this is intended as
# a simple example.
result = tf.map_fn(f, p)
这篇关于如何将张量列表作为输入传递给张量流中的图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!