2017-09-06 63 views
0

我无法对string_input_producer启用时代限制,但没有发生OutOfRange错误(请求的x,当前大小为0)。似乎并不重要,我有多少元素,总是有0。TensorFlow:如何在string_input_producer中使用'num_epochs'

这是我开fileQueue建设者:

def get_queue(base_directory): 
    files = [f for f in os.listdir(base_directory) if f.endswith('.bin')] 
    shuffle(files) 
    file = [os.path.join(base_directory, files[0])] 
    fileQueue = tf.train.string_input_producer(file, shuffle=False, num_epochs=1) 

    return fileQueue 

如果我从它可以创建样本罚款string_input_producer删除num_epochs = 1

我的输入管道:

def input_pipeline(instructions, fileQueue): 
    example, label, feature_name_list = read_binary_format(fileQueue, instructions) 

    num_preprocess_threads = 16 
    capacity = 20 

    example, label = tf.train.batch(
     [example, label], 
     batch_size=50000, # set the batch size way bigger so we always return the full amount of samples from the file 
     allow_smaller_final_batch=True, 
     capacity=capacity, 
     num_threads=num_preprocess_threads) 

    return example, label 

而且最后我的会议:

with tf.Session(graph=tf.Graph()) as sess: 
    train_inst_set = sf.DeserializationInstructions.from_filename(os.path.join(input_dir, "Train/config.json")) 
    fileQueue = sf.get_queue(os.path.join(input_dir, "Train")) 
    features_train, labels_train = sf.input_pipeline(train_inst_set, fileQueue) 
    sess.run(tf.global_variables_initializer()) 

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

    train_feature_batch, train_label_batch = sess.run([features_train, labels_train]) 
+0

里面'read_binary_format',使用'try ...除了OutOfRange'构造来处理异常 –

+0

什么是错误信息? –

+0

FIFOQueue'_1_batch/fifo_queue'已关闭且元素不足(请求200,当前大小为0)。似乎并不重要,我有多少元素,总是有0。 – Lukeyb

回答

0

的问题是由此引起:Issue #1045

无论出于何种原因,tf.global_variable_initialiser不初始化所有的变量。你也需要初始化局部变量。

添加

sess.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) 

到您的会话。

0

TensorFlow Documentation

num_epochs:一个整数(可选)。如果指定,则string_input_producer 会在 生成OutOfRange错误之前产生来自string_tensor num_epochs次的每个字符串。如果没有指定, string_input_producer可以通过字符串string_tensor 无限次数

周期所以一旦数据已经被消耗num_epochs次,string_input_producer将推出OutOfRange异常

为了避免突然停止,你有多个解决方案:

  • 使用更加高级API作为tf.train.MonitoredSession,该处理automaticaly的OutOfRange错误。

  • 把你的训练指令放在试试,除了块之外。捕捉异常时,请协调员关闭线程。看到这个example

+0

问题是无论我请求多少个元素都无关紧要,队列总是说有0个元素可用。 – Lukeyb