【问题标题】:Duplicate values in read from file minibatches TensorFlow从文件小批量TensorFlow中读取重复值
【发布时间】:2016-08-31 10:12:45
【问题描述】:

我按照关于使用 TF 读取数据的教程进行了一些尝试。现在,问题是我的测试在从 CSV 读取数据时创建的批次中显示重复数据。
我的代码如下所示:

# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os 
import collections
import numpy as np

from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf

class XICSDataSet:    
    def __init__(self, height=20, width=195, batch_size=1000, noutput=15):
        self.depth = 1
        self.height = height
        self.width = width
        self.batch_size = batch_size
        self.noutput = noutput

    def trainingset_files_reader(self, data_dir, nfiles):

        fnames = [os.path.join(data_dir, "test%d"%i) for i in range(nfiles)]
        filename_queue = tf.train.string_input_producer(fnames, shuffle=False)

        reader = tf.TextLineReader()
        key, value = reader.read(filename_queue)
        record_defaults = [[.0],[.0],[.0],[.0],[.0]]
        data_tuple = tf.decode_csv(value, record_defaults=record_defaults, field_delim = ' ')
        features = tf.pack(data_tuple[:-self.noutput])
        label = tf.pack(data_tuple[-self.noutput:])

        depth_major = tf.reshape(features, [self.height, self.width, self.depth])

        min_after_dequeue = 100
        capacity = min_after_dequeue + 30 * self.batch_size
        example_batch, label_batch = tf.train.shuffle_batch([depth_major, label], batch_size=self.batch_size, capacity=capacity,
                                                                        min_after_dequeue=min_after_dequeue)

    return example_batch, label_batch

with tf.Graph().as_default():
ds = XICSDataSet(2, 2, 3, 1)
im, lb = ds.trainingset_files_reader(filename, 1)

sess = tf.Session()

init = tf.initialize_all_variables()
sess.run(init)
tf.train.start_queue_runners(sess=sess)

for i in range(1000):
    lbs = sess.run([im, lb])[1]
    _, nu = np.unique(lbs, return_counts=True)
    if np.array_equal(nu, np.array([1, 1, 1])) == False:
        print('Not unique elements found in a batch!')
        print(lbs)

我尝试了不同的批处理大小、不同的文件数量、不同的容量值和 min_after_dequeue,但我总是遇到问题。最后,我希望能够只从一个文件中读取数据,创建批次并打乱示例。 我的文件是为此测试特别创建的,每行有 5 行代表样本,有 5 列。最后一列是该样本的标签。这些只是随机数。我只使用 10 个文件来测试它。

【问题讨论】:

  • 我的预感是您最终会多次阅读条目,您不会在文件末尾停下来而是绕回。如果 min_after_deque 在大小上与文件中的条目数相当,则很有可能两个相同的条目将在同一个批次中。因为一个保留在 min_after_deque 元素中,直到第二次被读取。

标签: neural-network tensorflow conv-neural-network


【解决方案1】:

tf.train.string_input_producer(fnames) 的默认行为是在fnames 中生成无限数量的元素副本。因此,由于您的 tf.train.shuffle_batch() 容量大于输入文件中的元素总数(每个文件 5 个元素 * 10 个文件 = 50 个元素),并且 min_after_dequeue 也大于元素数,因此队列将在生成第一批之前,至少包含两个完整的输入数据副本。因此,某些批次可能会包含重复数据。

如果您只想处理每个示例一次,您可以在创建tf.train.string_input_producer() 时设置显式num_epochs=1。例如:

def trainingset_files_reader(self, data_dir, nfiles):
    fnames = [os.path.join(data_dir, "test%d" % i) for i in range(nfiles)]

    filename_queue = tf.train.string_input_producer(
        fnames, shuffle=False, num_epochs=1)

    # ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多