如何在急切执行模式下使用 tf.data 数据集? [英] How can I use tf.data Datasets in eager execution mode?

查看:64
本文介绍了如何在急切执行模式下使用 tf.data 数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 2018 年 TensorFlow 开发者峰会的 tf.data talk 中,Derek Murray 提出了一种方法将 tf.data API 与 TensorFlow 的eager执行模式(在 10:54).我尝试了那里显示的代码的简化版本:

In the tf.data talk at the TensorFlow Dev Summit 2018, Derek Murray presented a way to combine the tf.data API with TensorFlow's eager execution mode (at 10:54). I tried out a simplified version of the code shown there:

import tensorflow as tf
tf.enable_eager_execution()

dataset = tf.data.Dataset.from_tensor_slices(tf.random_uniform([50, 10]))
dataset = dataset.batch(5)
for batch in dataset:
    print(batch)

导致

TypeError: 'BatchDataset' object is not iterable

我也尝试使用 dataset.make_one_shot_iterator()dataset.make_initializable_iterator() 来迭代数据集,但结果是

I also tried using dataset.make_one_shot_iterator() and dataset.make_initializable_iterator() to iterate over the dataset, but they result in

RuntimeError: dataset.make_one_shot_iterator is not supported when eager execution is enabled.

RuntimeError: dataset.make_initializable_iterator is not supported when eager execution is enabled.

TensorFlow 版本:1.7.0,Python 版本:3.6

如何将 tf.data API 与 Eager Execution 结合使用?

How can you use the tf.data API with eager execution?

推荐答案

make_one_shot_iterator() 应该可以在 TensorFlow 1.8 中工作,但现在(即对于 TensorFlow 1.7),请执行以下操作:

make_one_shot_iterator() should work in TensorFlow 1.8, but for now (i.e., for TensorFlow 1.7), do the following:

import tensorflow.contrib.eager as tfe

dataset = tf.data.Dataset.from_tensor_slices(tf.random_uniform([50, 10]))
dataset = dataset.batch(5)
for batch in tfe.Iterator(dataset):
     print(batch)

这篇关于如何在急切执行模式下使用 tf.data 数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆