Python 将列表输入 TensorFlow 中的 feed_dict 时发出问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33684657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:47:09  来源:igfitidea点击:

Issue feeding a list into feed_dict in TensorFlow

pythontensorflow

提问by d-roy

I'm trying to pass a list into feed_dict, however I'm having trouble doing so. Say I have:

我正在尝试将列表传递给feed_dict,但是我在这样做时遇到了麻烦。说我有:

inputs = 10 * [tf.placeholder(tf.float32, shape=(batch_size, input_size))]

where inputs is fed into some function outputsthat I want to compute. So to run this in tensorflow, I created a session and ran the following:

其中输入被输入到outputs我想要计算的某个函数中。因此,为了在 tensorflow 中运行它,我创建了一个会话并运行以下命令:

sess.run(outputs, feed_dict = {inputs: data}) 
#data is my list of inputs, which is also of length 10

but I get an error, TypeError: unhashable type: 'list'.However, I'm able to pass the data element-wise like so:

但是我收到一个错误,TypeError: unhashable type: 'list'.但是,我可以像这样按元素传递数据:

sess.run(outputs, feed_dict = {inputs[0]: data[0], ..., inputs[9]: data[9]}) 

So I'm wondering if there's a way I can solve this issue. I've also tried to construct a dictionary(using a forloop), however this results in a dictionary with a single element, where they key is: tensorflow.python.framework.ops.Tensor at 0x107594a10

所以我想知道是否有办法解决这个问题。我还尝试构建一个字典(使用for循环),但是这会生成一个包含单个元素的字典,它们的键是: tensorflow.python.framework.ops.Tensor at 0x107594a10

采纳答案by mrry

There are two issues that are causing problems here:

这里有两个问题导致问题:

The first issue is that the Session.run()call only accepts a small number of types as the keys of the feed_dict. In particular, lists of tensors are notsupported as keys, so you have to put each tensor as a separate key.*One convenient way to do this is using a dictionary comprehension:

第一个问题是Session.run()调用只接受少量类型作为feed_dict. 特别是,支持张量列表作为键,因此您必须将每个张量作为单独的键。*一种方便的方法是使用字典理解:

inputs = [tf.placeholder(...), ...]
data = [np.array(...), ...]
sess.run(y, feed_dict={i: d for i, d in zip(inputs, data)})

The second issue is that the 10 * [tf.placeholder(...)]syntax in Python creates a list with ten elements, where each element is the same tensor object(i.e. has the same nameproperty, the same idproperty, and is reference-identical if you compare two elements from the list using inputs[i] is inputs[j]). This explains why, when you tried to create a dictionary using the list elements as keys, you ended up with a dictionary with a single element - because all of the list elements were identical.

第二个问题是10 * [tf.placeholder(...)]Python中的语法创建了一个包含 10 个元素的列表,其中每个元素都是相同的张量对象(即具有相同的name属性,相同的id属性,如果使用 比较列表中的两个元素,则引用相同inputs[i] is inputs[j]) . 这解释了为什么当您尝试使用列表元素作为键创建字典时,您最终会得到一个具有单个元素的字典 - 因为所有列表元素都是相同的。

To create 10 different placeholder tensors, as you intended, you should instead do the following:

要按照您的意图创建 10 个不同的占位符张量,您应该执行以下操作:

inputs = [tf.placeholder(tf.float32, shape=(batch_size, input_size))
          for _ in xrange(10)]

If you print the elements of this list, you'll see that each element is a tensor with a different name.

如果打印此列表的元素,您将看到每个元素都是一个具有不同名称的张量。



EDIT:*You can now pass tuplesas the keys of a feed_dict, because these may be used as dictionary keys.

编辑:*您现在可以将元组作为 a 的键传递feed_dict,因为它们可以用作字典键。

回答by Salvador Dali

Here is a correct example:

这是一个正确的例子:

batch_size, input_size, n = 2, 3, 2
# in your case n = 10
x = tf.placeholder(tf.types.float32, shape=(n, batch_size, input_size))
y = tf.add(x, x)

data = np.random.rand(n, batch_size, input_size)

sess = tf.Session()
print sess.run(y, feed_dict={x: data})

And here is a strange things I see in your approach. For some reason you use 10 * [tf.placeholder(...)], which creates 10 tensors of size (batch_size, input_size). No idea why do you do this, if you can just create on Tensor of rank 3 (where the first dimension is 10).

这是我在你的方法中看到的奇怪的事情。出于某种原因,您使用10 * [tf.placeholder(...)],这会创建 10 个大小为 的张量(batch_size, input_size)。不知道为什么要这样做,如果您可以在 3 级张量上创建(其中第一个维度是 10)。

Because you have a list of tensors (and not a tensor), you can not feed your data to this list (but in my case I can feed to my tensor).

因为您有一个张量列表(而不是张量),所以您不能将您的数据提供给该列表(但在我的情况下,我可以提供给我的张量)。

回答by tansg

feed_dict can be provided by preparing a dictionary beforehand as follows

feed_dict 可以通过预先准备字典来提供,如下所示

n = 10
input_1 = [tf.placeholder(...) for _ in range(n)]
input_2 = tf.placeholder(...)
data_1 = [np.array(...) for _ in range(n)]
data_2 = np.array(...)


feed_dictionary = {}
for i in range(n):
    feed_dictionary[input_1[i]] = data_1[i]
feed_dictionary[input_2] = data_2
sess.run(y, feed_dict=feed_dictionary)