Search code examples
tensorflowdatasetpipeline

Changing Tensorflow Input Pipeline to "Datasets" - Error


Until now i used the pipelines like this in tensorflow:

queue_filenames = tf.train.string_input_producer(data)
reader = tf.FixedLengthRecordReader(record_bytes=4*4)

class Record(object):
    pass
result = Record()
result.ley, value = reader.read(queue_filenames)
record = tf.decode_raw(value, tf.float32)
image = tf.reshape(tf.strided_slice(record,[0],[1]),[1])
label = tf.reshape(tf.strided_slice(record,[1],[4]),[3])

x, y = tf.train.shuffle_batch([image, label],
                              batch_size=batch_size,
                              capacity=batch_size*3,
                              min_after_dequeue=batch_size*2)

But now i want to change to the "dataset"-thing. I wrote this:

dataset = tf.data.FixedLengthRecordDataset(filenames=data,
                                           record_bytes=4*4)
dataset.map(_generate_x_y)
dataset.shuffle(buffer_size=batch_size*2)
dataset.batch(batch_size=batch_size)
dataset.repeat()
iterator = dataset.make_one_shot_iterator()
x, y = iterator.get_next()

with:

def _generate_x_y(sample):
    features = {"x": tf.FixedLenFeature([1], tf.float32),
                "y": tf.FixedLenFeature([3], tf.float32)}
    parsed_features = tf.parse_single_example(sample,features)
    return parsed_features["x"], parsed_features["y"]

my graph is like:

y_ = network(x)

and:

loss = tf.losses.softmax_cross_entropy(y,y_)
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss=loss)

my session is:

with tf.Session(graph=graph_train) as sess:
    tf.global_variables_initializer().run()
    for i in range(100):
        _, = sess.run([train_step])

It works fine with the old pipeline, but with the new dataset i get the following error:

File "C:/***/main.py", line 49, in <module>
x, y = iterator.get_next()
  File "C:\***\python\framework\ops.py", line 396, in __iter__
"`Tensor` objects are not iterable when eager execution is not "
TypeError: `Tensor` objects are not iterable when eager execution is not enabled. To iterate over this tensor use `tf.map_fn`.

Thanks for helping :-)


Solution

  • One obvious issue that is likely the cause of the problem is that you don't use the transformed datasets. Basically, instead of

    dataset = tf.data.FixedLengthRecordDataset(filenames=data,
                                               record_bytes=4*4)
    dataset.map(_generate_x_y)
    dataset.shuffle(buffer_size=batch_size*2)
    

    you should do:

    dataset = tf.data.FixedLengthRecordDataset(filenames=data,
                                               record_bytes=4*4)
    dataset = dataset.map(_generate_x_y)
    dataset = dataset.shuffle(buffer_size=batch_size*2)
    

    Each dataset operation returns a new, transformed, dataset. The original object is not modified by operations like map and shuffle.