python - 如何有效地将 Pandas Dataframe 保存到一个/多个 TFRecord 文件中?

标签 python pandas tensorflow bigdata tfrecord

首先,我想快速介绍一下背景。我最终想要实现的是在tensorflow框架下针对多类分类问题训练一个全连接的神经网络。

该问题的挑战在于训练数据的规模很大(~ 2 TB)。为了训练在有限的内存下工作,我想将训练集保存到小文件中并使用小批量梯度下降算法来训练模型。 (每次只有一个或几个文件被加载到内存中)。

现在说我已经有两个包含处理过的数据的数据框,一个带有 X_train(700 万个条目 * 200 个带有列名的特征),另一个带有 training_y(700 万个条目 * 1 个标签)。 如何有效地将其保存到 TFrecord 文件中,保留列名、行索引等,并且我可能希望每个文件包含 100,000 个条目? 我知道通过 TFrecord 下的所有内容,我可以利用在 tensorflow 中实现的一些简洁的改组和批处理功能。我可能需要一种非常有效的方式来写入这样的记录,因为稍后我需要将 2TB 的数据写入这种文件格式。

我试图在 Google 上搜索“如何将 Pandas 数据框写入 TFRecords”,但在好的例子上没有任何运气。大多数例子要求我创建一个 tf.train.Example逐列、逐行并使用 tf.python_io.TFRecordWriter 写入 tfrecord 文件.只是想确认这是我能在这里得到的最好的东西。

如果您对我正在尝试解决的问题有其他建议,也将不胜感激!

最佳答案

您可以查看 here将 pandas df 写入 tfRecord

安装 pandas-tfrecords

pip install pandas-tfrecords

试试
import pandas as pd
from pandas_tfrecords import pd2tf, tf2pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c'], 'C': [[1, 2], [3, 4], [5, 6]]})

# local
pd2tf(df, './tfrecords')
my_df = tf2pd('./tfrecords')

希望这会有所帮助。

关于python - 如何有效地将 Pandas Dataframe 保存到一个/多个 TFRecord 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679365/

相关文章:

python - 如何将两个字符串转换成单个字典?

Python 和 Selenium : How can I obtain a href element from a specific div? 我的元素已过时

python - 减少专用于 pandas dtype=object 的内存

python - 错误 "' numpy.ndarray'对象没有属性 'values'"

TensorFlow - 如何从 tf.Estimator 获取我的损失值

python - 如何在 amazon lambda 中传递来自 amazon connect 的属性?

python - 更改多个列名但不是全部 - Pandas Python

python - 如何清理 pandas DataFrame

python - 值错误 : Weights for model sequential have not yet been created

memory-management - tensorflow : Memory leak even while closing Session?