python - 如何将 dataset_from_generator 与熊猫数据一起使用(Tensorflow)
问题描述
最初我使用 dataset_from_tensor_slices,但由于数据太大(超过 2gb 限制),我不得不使用 dataset_from_generator。这是我的输入函数,但我收到此错误:features should be a dictionary of Tensor
s. 给定类型:“class 'tensorflow.python.framework.ops.Tensor'”。从张量切片更改为生成器之前,没有抛出错误(直到 2gb 限制)。特征和标签都是 pandas 数据帧。
def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
features = {key:np.array(value) for key,value in dict(features).items()}
print(type(features))
print(type(targets))
def gen():
for feature, target in (features, targets):
yield feature, target
ds = Dataset.from_generator(gen,(tf.float32,tf.float32), (tf.TensorShape([]), tf.TensorShape([None])))
ds = ds.batch(batch_size).repeat(num_epochs)
if shuffle:
ds = ds.shuffle(10000)
features, labels = ds.make_one_shot_iterator().get_next()
return features, labels
解决方案
推荐阅读
- javascript - 完全删除笔记项目
- javascript - 电容器 - 如何更改手机语言上的应用名称
- php - MYSQL计算一行的不同值与另一行的不同值
- ruby-on-rails - 如何通过 rails api 的响应重新加载特定的部分值?
- flask - 如何解决 CS50 最终项目的 Flask 中的“jinja2.exceptions.UndefinedError: 'nutrition' is undefined”错误
- sql-server - 数据类型为 Money 的 MS SQL Server 字段在 MS ACCESS 链接表中显示空字段
- html - 在 jekyll 主题站点文件中找不到某些图标
- python - 即使不存在滚动,硒驱动程序也会继续滚动
- azure-devops - 用于本地数据网关的 azure Devops 管道设置
- php - PHP - WAMP 和 Web 服务器上的浮点数精度差异