我正在尝试使用 TF documentation 中的示例对于 tf.data.Dataset.window
和文档中的示例失败。
来自文档的代码:
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))
产生此错误(删除跟踪):
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
而不是通常的张量。
TF 版本:1.13.1
最佳答案
Window 生成类似结构的数据集,在您的情况下应该返回对 {1, 2}。不知道如何正确使用它或它为什么存在,但设法让它像这样工作: 将 tensorflow 导入为 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))
关于python - 文档中的 tf.data.Dataset.window 示例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56436701/