4
我想问一下使用tensorflow批量读取大文本数据的正确模式?Tensorflow - 从单个大文本文件中读取数据的正确方法
这是一行文本数据。单个txt文件中有数十亿行这样的数据。
target context label
现在我想tfrecords使用官方文档中建议。
这里是我的方式
filename_queue = tf.train.string_input_producer([self._train_data], num_epochs=self._num_epochs)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'target': tf.FixedLenFeature([], tf.int64),
'context': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
})
target = features['target']
context = features['context']
label = features['label']
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * self._batch_size
target_batch, context_batch, label_batch = tf.train.shuffle_batch(
[target, context, label], batch_size=self._batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue, num_threads=self._concurrent_steps)
之后,我用时间线做分析。结果表明这部分大部分时间都在使用。 这里是剖析图。 the profiling result
Btw。我正在使用批量大小500. 有什么建议吗?
您的CPU是否已满载?如果不是,则可能需要使用更多线程(在'tf.train.shuffle_batch()'的'num_threads'参数中)解析文件中的记录。其他可能性:你可以使用'reader.read_up_to(n)',''tf.parse_example()'而不是'tf.parse_single_example()',并且通过'enqueue_many = True'将'tf.train.shuffle_batch()'传入为了执行批量解析,这应该更有效率。 – mrry
@mrry这就是它我使用'reader.read_up_to(n)',''tf.parse_example()'每次读取多行并解决问题。 –