读入大型 CSV 文件并输入 TensorFlow
Read in Large CSV File and feed into TensorFlow
所以我正在尝试将我的 csv 文件读入 python,然后将数据拆分为训练和测试数据(n 折交叉验证),然后将其输入到我已经制作好的深度学习架构中.然而,在阅读了关于如何读取 csv 文件的 TensorFlow 教程后,如下所示:
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.pack([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)
除了最后的 for 循环部分外,这段代码中的所有内容都有意义。
问题一:for循环中的1200有什么意义?是从数据中取出的记录数吗?
教程的下一部分讲批处理代码中的示例如下:
def read_my_file_format(filename_queue):
reader = tf.SomeReader()
key, record_string = reader.read(filename_queue)
example, label = tf.some_decoder(record_string)
processed_example = some_processing(example)
return processed_example, label
def input_pipeline(filenames, batch_size, num_epochs=None):
filename_queue = tf.train.string_input_producer(
filenames, num_epochs=num_epochs, shuffle=True)
example, label = read_my_file_format(filename_queue)
# min_after_dequeue defines how big a buffer we will randomly sample
# from -- bigger means better shuffling but slower start up and more
# memory used.
# capacity must be larger than min_after_dequeue and the amount larger
# determines the maximum we will prefetch. Recommendation:
# min_after_dequeue + (num_threads + a small safety margin) * batch_size
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * batch_size
example_batch, label_batch = tf.train.shuffle_batch(
[example, label], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
return example_batch, label_batch
我知道这是异步的,代码会阻塞,直到它收到所有内容。代码运行后查看 example 和 label 的值时,发现每个都只保存数据中一条特定记录的信息。
问题2:"read_my_file"下面的代码应该和我贴的第一个代码块一样吗?然后是 input_pipeline 函数,它将各个记录集中到某个 batch_size?如果 read_my_file 函数与第一个代码块相同,为什么没有相同的 for 循环(回到我的第一个问题)
我希望得到任何澄清,因为这是我第一次使用 TensorFlow。感谢您的帮助!
(1) 1200 是任意的——我们应该修正这个例子,让它在那里使用一个命名常量,以使其更清楚。感谢您发现它。 :) the CSV reading example is set up, continued reads will read through the two CSV files as many times as desired (the string_input_producer
保存文件名的方式没有提供 num_epochs
参数,因此它默认为永远循环)。所以 1200 只是程序员在示例中选择检索的记录数。
如果你只想读取文件中的示例数,你可以捕获如果输入者 运行 超出输入时抛出的 OutOfRangeError
,或者准确读取记录数当前的。有一个新的读取操作正在进行中,应该也有助于简化它,但我认为它不包含在 0.9 中。
(2) 它应该设置一组非常相似的操作,但实际上并没有进行读取。请记住,您在 Python 中编写的大部分内容都是构建一个图形,它是 TensorFlow 将执行的一系列操作。所以 read_my_file 中的内容几乎就是创建 tf.Session()
之前的内容。在上面的示例中,for 循环中的代码实际上是在执行 tf 图以将示例提取回 python。但在示例的第二部分,您只是设置管道以将项目读入张量,然后添加额外的操作来使用这些张量并做一些有用的事情——在这种情况下,将它们放入队列以创建更大的批次,这些批次本身可能会被其他 TF 操作人员稍后使用。
所以我正在尝试将我的 csv 文件读入 python,然后将数据拆分为训练和测试数据(n 折交叉验证),然后将其输入到我已经制作好的深度学习架构中.然而,在阅读了关于如何读取 csv 文件的 TensorFlow 教程后,如下所示:
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.pack([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)
除了最后的 for 循环部分外,这段代码中的所有内容都有意义。
问题一:for循环中的1200有什么意义?是从数据中取出的记录数吗?
教程的下一部分讲批处理代码中的示例如下:
def read_my_file_format(filename_queue):
reader = tf.SomeReader()
key, record_string = reader.read(filename_queue)
example, label = tf.some_decoder(record_string)
processed_example = some_processing(example)
return processed_example, label
def input_pipeline(filenames, batch_size, num_epochs=None):
filename_queue = tf.train.string_input_producer(
filenames, num_epochs=num_epochs, shuffle=True)
example, label = read_my_file_format(filename_queue)
# min_after_dequeue defines how big a buffer we will randomly sample
# from -- bigger means better shuffling but slower start up and more
# memory used.
# capacity must be larger than min_after_dequeue and the amount larger
# determines the maximum we will prefetch. Recommendation:
# min_after_dequeue + (num_threads + a small safety margin) * batch_size
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * batch_size
example_batch, label_batch = tf.train.shuffle_batch(
[example, label], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
return example_batch, label_batch
我知道这是异步的,代码会阻塞,直到它收到所有内容。代码运行后查看 example 和 label 的值时,发现每个都只保存数据中一条特定记录的信息。
问题2:"read_my_file"下面的代码应该和我贴的第一个代码块一样吗?然后是 input_pipeline 函数,它将各个记录集中到某个 batch_size?如果 read_my_file 函数与第一个代码块相同,为什么没有相同的 for 循环(回到我的第一个问题)
我希望得到任何澄清,因为这是我第一次使用 TensorFlow。感谢您的帮助!
(1) 1200 是任意的——我们应该修正这个例子,让它在那里使用一个命名常量,以使其更清楚。感谢您发现它。 :) the CSV reading example is set up, continued reads will read through the two CSV files as many times as desired (the string_input_producer
保存文件名的方式没有提供 num_epochs
参数,因此它默认为永远循环)。所以 1200 只是程序员在示例中选择检索的记录数。
如果你只想读取文件中的示例数,你可以捕获如果输入者 运行 超出输入时抛出的 OutOfRangeError
,或者准确读取记录数当前的。有一个新的读取操作正在进行中,应该也有助于简化它,但我认为它不包含在 0.9 中。
(2) 它应该设置一组非常相似的操作,但实际上并没有进行读取。请记住,您在 Python 中编写的大部分内容都是构建一个图形,它是 TensorFlow 将执行的一系列操作。所以 read_my_file 中的内容几乎就是创建 tf.Session()
之前的内容。在上面的示例中,for 循环中的代码实际上是在执行 tf 图以将示例提取回 python。但在示例的第二部分,您只是设置管道以将项目读入张量,然后添加额外的操作来使用这些张量并做一些有用的事情——在这种情况下,将它们放入队列以创建更大的批次,这些批次本身可能会被其他 TF 操作人员稍后使用。