我正在尝试将数据从 CSV 文件读取到 tensorflow,
https://www.tensorflow.org/versions/r0.7/how_tos/reading_data/index.html#filenames-shuffling-and-epoch-limits
官方文档中的示例代码是这样的:
col1, col2, col3, col4, col5 = tf.decode_csv(value, record_defaults=record_defaults)
要读取文件,需要事先知道文件有多少列和多少行,如果有1000列,我需要定义1000个变量,如col1, col2, col3, col4, col5,。 .., col1000 ,
这看起来不是一种有效的数据读取方式。
我的问题
将 CSV 文件读入 Tensorflow 的最佳方法是什么?
在Tensorflow中有什么方法可以读取数据库(比如mongoDB)吗?
最佳答案
你绝对不需要定义 col1, col2, to col1000...
通常,您可能会这样做:
columns = tf.decode_csv(value, record_defaults=record_defaults)
features = tf.pack(columns)
do_whatever_you_want_to_play_with_features(features)
我不知道任何直接从 MongoDB 读取数据的现成方法。也许你可以写一个简短的脚本来将 MongoDB 中的数据转换为 Tensorflow 支持的格式,我推荐二进制形式的 TFRecord
,它比 csv 记录读取速度快得多。 This是一篇关于这个主题的好博文。或者您可以选择自己实现一个定制的数据阅读器,见the official doc在这里。
https://stackoverflow.com/questions/35644264/