Tensorflow Data Input Toggle: Train/Validation
<p>I have data that comes into my graph through queue runners, after I switched from the handy but speed-inferior placeholders.</p>

<p>After each training epoch, I wish to run a validation pass. Other than the the training pass, the validation pass uses different data, no augmentation and no shuffling.</p>

<p>The question is simple: how do I toggle these things?</p>

<p>A few observations:</p>

<ul>
<li>I cannot toggle the <code>shuffle</code> option in the <code>string_input_producer</code> through a <code>tf.placeholder</code> boolean.</li>
<li>The only examples on-the-line that I have found use the <code>placeholder</code> to seperate the training from the validation data. These in turn, do not use the superior queue runners.</li>
<li>I did manage to do the above with a <code>tf.cond()</code> here i would test for a <code>is_training</code> <code>tf.placeholder</code> boolean that i pass through the <code>feed_dict</code>. Is this solution the most optimal? How expensive is this <code>tf.conf()</code> method?</li>
</ul>

<p>The method that works well for me is to use <code>tf.placeholder_with_default</code>:</p>

<blockquote>
<pre><code>images_train, labels_train = train_data_pipeline(fnlist_train, ref_grid)
images_val, labels_val = val_data_pipeline(fnlist_val, ref_grid)
images = tf.placeholder_with_default(images_train, shape=[None, FLAGS.nx_image, FLAGS.ny_image, FLAGS.nz_image])
labels = tf.placeholder_with_default(labels_train, shape=[None, label_length])
</code></pre>
</blockquote>

<p>During training, <code>images</code> and <code>labels</code> come directly from the training queue. For the intermittent validation steps I feed <code>images</code> and <code>labels</code> through a feed_dict in a call to<code>sess.run()</code>. The only slight hack is that is that the validation data are also tensors from a queue and feed_dict doesn't accept tensors, so I call <code>sess.run([images_val, labels_val])</code> first to get numpy values and then use them in the feed_dict. Seems to work well and there is minimal delay from the tensor==>numpy==>tensor conversion, which only occurs during validation anyway. </p>

<p>And for your case where the validation data have separate processing requirements, this can be handled when you set up the separate validation queue and processing flow to it.</p>

python tensorflow