问题
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