如何将数据读入 Tensorflow?
How to read data into Tensorflow?
我正在尝试将数据从 CSV 文件读取到 tensorflow,
官方文档中的示例代码是这样的:
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)?
def func()
return 1,2,3,4
b = func()
print b #(1, 2, 3, 4)
print [num for num in b] # [1, 2, 3, 4]
嗨,它与 tensorflow 无关,它很简单 python 不需要定义 1000 个变量。 tf.decode_csv returns 一个元组。
不知道数据库处理,我想你可以使用 python 并以数组形式将数据输入到 tensorflow。
希望对您有所帮助
绝对不需要定义col1、col2、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 is a good blog post about this topic. Or you can choose to implement a customized data reader by yourself, see the official doc这里
当然你可以实现直接从 mongo 中读取批量随机排序训练数据以提供给 tensorflow。以下是我的方式:
for step in range(self.steps):
pageNum=1;
while(True):
trainArray,trainLabelsArray = loadBatchTrainDataFromMongo(****)
if len(trainArray)==0:
logging.info("train datas consume up!")
break;
logging.info("started to train")
sess.run([model.train_op],
feed_dict={self.input: trainArray,
self.output: np.asarray(trainLabelsArray),
self.keep_prob: params['dropout_rate']})
pageNum=pageNum+1;
并且您还需要在 mongodb 中预处理训练数据,例如:为 mongodb 中的每个训练数据分配一个随机排序值...
Is there any way to read Database (such as mongoDB) in Tensorflow ?
尝试 TFMongoDB,一个 C++ 实现的 TensorFlow 数据集操作,允许您连接到您的 MongoDB:
pip install tfmongodb
GitHub 页面上有一个关于如何读取数据的示例。另见 pypi: TFMongoDB
我正在尝试将数据从 CSV 文件读取到 tensorflow,
官方文档中的示例代码是这样的:
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)?
def func()
return 1,2,3,4
b = func()
print b #(1, 2, 3, 4)
print [num for num in b] # [1, 2, 3, 4]
嗨,它与 tensorflow 无关,它很简单 python 不需要定义 1000 个变量。 tf.decode_csv returns 一个元组。
不知道数据库处理,我想你可以使用 python 并以数组形式将数据输入到 tensorflow。
希望对您有所帮助
绝对不需要定义col1、col2、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 is a good blog post about this topic. Or you can choose to implement a customized data reader by yourself, see the official doc这里
当然你可以实现直接从 mongo 中读取批量随机排序训练数据以提供给 tensorflow。以下是我的方式:
for step in range(self.steps):
pageNum=1;
while(True):
trainArray,trainLabelsArray = loadBatchTrainDataFromMongo(****)
if len(trainArray)==0:
logging.info("train datas consume up!")
break;
logging.info("started to train")
sess.run([model.train_op],
feed_dict={self.input: trainArray,
self.output: np.asarray(trainLabelsArray),
self.keep_prob: params['dropout_rate']})
pageNum=pageNum+1;
并且您还需要在 mongodb 中预处理训练数据,例如:为 mongodb 中的每个训练数据分配一个随机排序值...
Is there any way to read Database (such as mongoDB) in Tensorflow ?
尝试 TFMongoDB,一个 C++ 实现的 TensorFlow 数据集操作,允许您连接到您的 MongoDB:
pip install tfmongodb
GitHub 页面上有一个关于如何读取数据的示例。另见 pypi: TFMongoDB