python - 文档中的 tf.data.Dataset.window 示例失败

标签 python tensorflow

我正在尝试使用 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/

相关文章:

python - 值错误: arrays must all be same length

Python 3 int 除法运算符返回一个 float ?

python - 打印没有换行符python

tensorflow - 如何使用 tensorflow.Estimator 进行强化学习

python - 洋红色多风格仿作生成器仅生成纯黑色图像?

Tensorflow计算图像梯度损失

python - 积分计算程序错误

python - 如何使用tight_layout保持轴纵横比

python - 使用 tensorflow 估计器和集线器创建暹罗网络

python - 在 Keras fit_generator 中将 shuffle 设置为 True 时精度会降低