Error using tensorflow dataset window operation in graph mode with a Session

Viewed 129

I cant run the tensorflow dataset window examples in graph mode. This

def test_window_graph_mode():
    tf.reset_default_graph()
    dataset = tf.data.Dataset.range(7).window(3, 2, 1, True)
    iterator = dataset.make_one_shot_iterator()
    next_element = iterator.get_next()

    with tf.Session() as sess:
        print(sess.run(next_element))

crashes - while this

def test_window_graph_mode():
    tf.reset_default_graph()
    dataset = tf.data.Dataset.range(7)
    iterator = dataset.make_one_shot_iterator()
    next_element = iterator.get_next()

    with tf.Session() as sess:
        print(sess.run(next_element))

works fine. The error is

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.)

The examples dont demonstrate the user of make_one_shot_iterator and get_next -- I picked that up from google searches. Is there some other interface to using Window in old style graph mode tensorflow?

1 Answers

Your code works fine if you add the line,

dataset = dataset.flat_map(lambda x:x.batch(2))

before the line,

iterator = dataset.make_one_shot_iterator()

Complete working code is shown below:

tf.reset_default_graph()
dataset = tf.data.Dataset.range(7).window(3, 2, 1, True)
dataset = dataset.flat_map(lambda x:x.batch(2))
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()

with tf.Session() as sess:
    print(sess.run(next_element))

For more information, please refer this Stack Overflow Issue1 and Issue2.

Hope this helps. Happy Learning!

Related