我想使用一个无法一次性加载到内存的数据集来训练TensorFlow模型,但我不知道具体该怎么做。
我已经阅读了一些关于TFRecords
文件格式的优秀文章和官方文档。但我仍然无法弄明白该如何操作。
TensorFlow中是否有完整的解决方案计划?
回答:
可以考虑使用tf.TextLineReader
,结合tf.train.string_input_producer
,这允许你从磁盘上的多个文件中加载数据(如果你的数据集足够大,需要分散到多个文件中)。
参见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)
# 默认值,用于处理空列。同时指定解码结果的类型。
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:
# 开始填充文件名队列。
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# 检索单个实例:
example, label = sess.run([features, col5])
coord.request_stop()
coord.join(threads)