文档中的 tf.data.Dataset.window 示例失败 [英] tf.data.Dataset.window example from the documentation fails
问题描述
我正在尝试使用 TF 文档中的示例 用于 tf.data.Dataset.window
并且文档中的示例失败.
I'm trying to use an example from the TF documentation for tf.data.Dataset.window
and the example from the documentation is failing.
源自文档的代码:
import tensorflow as tf
ds = tf.data.Dataset.range(7).window(2)
next_element = ds.make_one_shot_iterator().get_next()
with tf.Session() as sess:
print(sess.run(next_element))
产生此错误(已删除跟踪):
Produces this error (trace removed):
TypeError: Can not convert a _VariantDataset into a Tensor or Operation.
During handling of the above exception, another exception occurred:
TypeError: Fetch argument <_VariantDataset shapes: (), types: tf.int64> has invalid type <class 'tensorflow.python.data.ops.dataset_ops._VariantDataset'>, must be a string or Tensor. (Can not convert a _VariantDataset into a Tensor or Operation.)
所以 iterator.get_next()
返回的是 VariantDataset
而不是通常的张量.
So iterator.get_next()
is returning a VariantDataset
rather than the usual tensor.
TF 版本:1.13.1
推荐答案
Window 生成类似结构的数据集,在您的情况下,该结构应该返回对 {1, 2}.不知道如何正确使用它或它为什么存在,但设法让它像这样工作:将张量流导入为 tf
Window produces dataset like structure that is supposed to be returning pairs {1, 2} in your case. Have no idea how to use it properly or why it exists, but managed to make it work like that: import tensorflow as tf
import tensorflow as tf
nxt = (tf.data.Dataset
.range(7)
.window(2, 1, 2, True)
.flat_map(lambda x: x.batch(2))
.make_one_shot_iterator()
.get_next()
)
with tf.Session() as sess:
print(sess.run(nxt))
这篇关于文档中的 tf.data.Dataset.window 示例失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!