Tensorflow Dataset API restore Iterator after completing one epoch

▼魔方 西西 提交于 2019-12-08 07:26:24

问题


I have 190 features and labels,My batch size is 20 but after 9 iterations tf.reshape is returning exception Input to reshape is a tensor with 21 values,but the requested shape has 60 and i know it is due to Iterator.get_next().How do i restore my Iterator so that it will again start serving batches from the beginning?


回答1:


If you want to restart a tf.data.Iterator from the beginning of its Dataset, consider using an initializable iterator, which has an operation you can run to re-initialize the iterator:

dataset = ...  # A `tf.data.Dataset` instance.
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()

train_op = ...  # Something that depends on `next_element`.

for _ in range(NUM_EPOCHS):
  # Initialize the iterator at the beginning of `dataset`.
  sess.run(iterator.initializer)

  # Loop over the examples in `iterator`, running `train_op`.
  try:
    while True:
      sess.run(train_op)

  except tf.errors.OutOfRangeError:  # Thrown at the end of the epoch.
    pass

  # Perform any per-epoch computations here.

For more details on the different kinds of Iterator, see the tf.data programmer's guide.



来源:https://stackoverflow.com/questions/49216946/tensorflow-dataset-api-restore-iterator-after-completing-one-epoch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!