如何理解tf.FIFOQueue的构造函数?

时间:2017-07-28 05:09:16

标签: tensorflow

我已经编写了一些代码,但我无法解释清楚。

第一个代码:

import tensorflow as tf

f = tf.FIFOQueue(10, [tf.int32, tf.int32])
en = f.enqueue([[1,2],[3,4]])
de = f.dequeue()
with tf.Session() as sess:
    en.run()
    x = sess.run(de)
    print(x)

我定义队列的元素是张量的元组,所以当我将[[1,2],[3,4]]排队时,它运行良好,输出为[array([1, 2]), array([3, 4])]

但是第二个代码,我无法理解。

import tensorflow as tf

f = tf.FIFOQueue(10, [tf.int32])
en = f.enqueue([1,2,3,4])
de = f.dequeue()
with tf.Session() as sess:
    en.run()
    x = sess.run(de)
    print(x)

根据第一个代码,我认为输出是[array(1, 2, 3, 4)],但事实上,输出是1。如何解释第二个代码?

1 个答案:

答案 0 :(得分:1)

如果将第二个代码的入队操作中的参数更改为:

en = f.enqueue([[1,2,3,4]])

你会得到你想要的结果。

您可以在shape的构造函数中使用tf.FIFOQueue参数以获得更明确的效果。例如,如果您将代码更改为:

import tensorflow as tf

f = tf.FIFOQueue(10, [tf.int32], shapes = [4])
en = f.enqueue([1,2,3,4])
de = f.dequeue()
with tf.Session() as sess:
    en.run()
    x = sess.run(de)
    print(x)

您将在f.enqueue操作时收到错误。

但是如果你使用它:

import tensorflow as tf

f = tf.FIFOQueue(10, [tf.int32], shapes = [4])
en = f.enqueue([[1,2,3,4]])
de = f.dequeue()
with tf.Session() as sess:
    en.run()
    x = sess.run(de)
    print(x)

你没有错误。