I am starting to use the new dataset API and one thing that I want to do is not described on the doc (https://www.tensorflow.org/programmers_guide/datasets#training_workflows)
My data fit in memory so I want load it in tensorflow to make the training efficient and for this I see for now 2 way to do it:
one is loading the data in the graph directly like this:
dataset = tf.contrib.data.Dataset.from_tensor_slices((X, Y))
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
# loop on epochs
for _ in range(5):
# Initialize an iterator over the training dataset.
sess.run(iterator.initializer)
# loop over all the batch
for _ in range(1000):
s = time.time()
try:
sess.run(next_element)
except tf.errors.OutOfRangeError:
print("Finish epoch")
the other one is to load the data in a placeholder so the data is not save in the graph:
features_placeholder = tf.placeholder(features.dtype, features.shape)
labels_placeholder = tf.placeholder(labels.dtype, labels.shape)
dataset = tf.contrib.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
# loop on epochs
for _ in range(5):
# Initialize an iterator over the training dataset.
sess.run(iterator.initializer, feed_dict={features_placeholder: X, labels_placeholder: Y})
# loop over all the batch
for _ in range(1000):
s = time.time()
try:
sess.run(next_element)
except tf.errors.OutOfRangeError:
print("Finish epoch")
The second is I think the best to save memory, but I don't want to feed the data at each epoch. It is really a loss of performance for nothing.
Is there a way to initialize the iterator only one time with a placeholder?
something like this:
sess.run(iterator.initializer, feed_dict={features_placeholder: X, labels_placeholder: Y})
# loop on epochs
for _ in range(5):
# Initialize an iterator over the training dataset.
sess.run(iterator.initializer)
# loop over all the batch
for _ in range(1000):
s = time.time()
try:
sess.run(next_element)
except tf.errors.OutOfRangeError:
print("Finish epoch")
That way we can keep the performance of the first solution and saving memory like the second solution.
Note:
one solution is to define the number of epoch with
dataset.repeat()method but with it we kind of loose track of where we are in the training.I want to check after each epoch (one pass over all the data) the evolution of the loss.