Simple task at hand: run training for N epochs performing calculating exact validation accuracy after each epoch. Epoch size can be either equal to full training set or some predefined number of iterations. During validation every validation set input has to be evaluated exactly once.
What would be the best way to mix together one_shot_iterators, initializable iterator and/or handle for that task?
Here is scaffolding of how i see it should work:
def build_training_dataset():
pass
def build_validation_dataset():
pass
def construct_train_op(dataset):
pass
def magic(iterator):
pass
USE_CUSTOM_EPOCH_SIZE = True
CUSTOM_EPOCH_SIZE = 60
MAX_EPOCHS = 100
training_dataset = build_training_dataset()
validation_dataset = build_validation_dataset()
# Magic goes here to build a nice one-instance dataset
dataset = magic(training_dataset, validation_dataset)
train_op = construct_train_op(dataset)
# Run N epochs in which the training dataset is traversed, followed by the
# validation dataset.
with tf.Session() as sess:
for epoch in MAX_EPOCHS:
# train
if USE_CUSTOM_EPOCH_SIZE:
for _ in range(CUSTOM_EPOCH_SIZE):
sess.run(train_op)
else:
while True:
# I guess smth like this:
try:
sess.run(train_op)
except tf.errors.OutOfRangeError:
break # we are done with the epoch
# validation
validation_predictions = []
while True:
try:
np.append(validation_predictions, sess.run(train_op)) # but for validation this time
except tf.errors.OutOfRangeError:
print('epoch %d finished with accuracy: %f' % (epoch validation_predictions.mean()))
break