如果我想使用无法通过 TensorFlow 加载到内存中的大型数据集,我该怎么办?



我想使用一个大型数据集,该数据集无法加载到内存中,以训练使用TensorFlow训练模型。但是我不知道我应该做什么。

我已经阅读了一些有关TFRecords文件格式和官方文档的精彩文章。巴士我仍然无法弄清楚。

是否有带有Tensorflow的完整解决方案?

考虑使用 tf.TextLineReader,它与 tf.train.string_input_producer结合使用,允许您从磁盘上的多个文件加载数据(如果数据集足够大,以至于需要将其分布在多个文件中)。p>请参阅https://www.tensorflow.org/programmers_guide/reading_data#reading_from_files

代码段从上面的链接:

filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
    value, record_defaults=record_defaults)
features = tf.stack([col1, col2, col3, col4])
with tf.Session() as sess:
  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  for     filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
    value, record_defaults=record_defaults)
features = tf.stack([col1, col2, col3, col4])
with tf.Session() as sess:
  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  for i in range(1200):
    # Retrieve a single instance:
    example, label = sess.run([features, col5])
  coord.request_stop()
  coord.join(threads)i in range(1200):
    # Retrieve a single instance:
    example, label = sess.run([features, col5])
  coord.request_stop()
  coord.join(threads)

通常,无论如何您都会使用批处理培训,因此您可以即时加载数据。例如图像:

for bid in nrBatches:
     batch_x, batch_y = load_data_from_hd(bid)
     train_step.run(feed_dict={x: batch_x, y_: batch_y})

因此,您可以即时加载每批,仅加载您需要在任何给定时刻加载的数据。自然,您的训练时间会增加,而使用硬盘而不是内存来加载数据。

相关内容

最新更新