2017-08-31 102 views
0

争取做对tf.train.string_input_producer(R1.3)创建,但有望获得的大小时,刚刚获得零尺寸的队列的大小健全性检测3种TensorFlow字符串输入监制尺寸()

import tensorflow as tf 

file_queue = tf.train.string_input_producer(['file1.csv','file2.csv','file3.csv']) 

print('type(file_queue): {}'.format(type(file_queue))) 

with tf.Session() as sess: 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    file_queue_size = sess.run(file_queue.size()) 

    coord.request_stop() 
    coord.join(threads) 

print('result of queue size operation: {}'.format(file_queue_size)) 

我的预感是,有某种延迟初始化怎么回事,所以我想我会问队列中的项目,看看大小为后

import tensorflow as tf 

file_queue = tf.train.string_input_producer(['file1.csv','file2.csv','file3.csv']) 

print('type(file_queue): {}'.format(type(file_queue))) 

with tf.Session() as sess: 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    item = sess.run(file_queue.dequeue()) 
    file_queue_size = sess.run(file_queue.size()) 

    coord.request_stop() 
    coord.join(threads) 

print('result of queue size operation: {}'.format(file_queue_size)) 

虽然大小不再是零,大小前提不是两个而是改变s每次代码运行时。

我觉得大小是一件简单的事情,但也许这只是不与data_flow_ops.FIFOQueue交互的方式。任何洞察力来解释这里发生的事情将不胜感激。

回答

1

这是队列由队列填充队列的异步特性的人工产物。试试这个代码:

import tensorflow as tf 
import time 

file_queue = tf.train.string_input_producer(['file1.csv','file2.csv','file3.csv']) 

print('type(file_queue): {}'.format(type(file_queue))) 

with tf.Session() as sess: 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 
    time.sleep(1) 
    file_queue_size = sess.run(file_queue.size()) 

    coord.request_stop() 
    coord.join(threads) 

print('result of queue size operation: {}'.format(file_queue_size)) 

输出应该说是result of queue size operation: 32。通过暂停主线程,队列运行者可以运行足够长的时间来填满队列。 为什么32而不是3?让我们来看看签名string_input_producer

string_input_producer(
    string_tensor, 
    num_epochs=None, 
    shuffle=True, 
    seed=None, 
    capacity=32, 
    shared_name=None, 
    name=None, 
    cancel_op=None 
) 

两个主要的事情,这里要注意:

  1. num_epochs=None手段不断迭代的项目列表上永远。遍历列表只需设置一次num_epochs=1。您还需要致电sess.run(tf.local_variables_initializer())。使用num_epochs=1如果在出队后运行大小操作,您将看到大小减小。
  2. capacity=32这是队列的默认容量。这就是为什么我们在上面的输出中看到32而不是3
+0

谢谢,这真的很有帮助,对TF文档来说是很好的补充。 –