title: How to implement TensorBoard v2 (tf.contrib.summary) with while_loop?
body: <p>I want to log the loss and accuracy value evaluated inside a nested body function of a while_loop during training. This is the structure: I have a class, a method of this class builds the graph using a while_loop (build_graph()). Another method calls build_graph() and then runs the session. It works. Or it seems to work. However, I would like to use TensorBoard to check if loss and accuracy are actually improving, but I'm not able to summary those tensors. I've tried to define a tf.contrib.summary.create_file_writer('summary') and a graph and pass them to build_graph() as parameters, so that the body function can see them. I have checked the list during graph execution coming from tf.contrib.summary.all_summary_ops() and it isn't empty. However, when I open TensorBoard I get "No dashboards are active for the current data set.". Neither the graph. I am aware that tf.summary does not work in while_loop but it seems that tf.contrib.summary works. </p>
  
  <p>Here is a working example</p>
  
  <pre><code>import tensorflow as tf
  import sys
  import datamanagement
  
  class myNet:
      def __init__(self):
          self.varlist = ["x", "y"]
          self.data = []
          self.hsize = [10, 10]
          self.batch_size = 10
          self.tr_mainsteps = 1000
          self.learnrate = 0.001
          self.sourcedatafile = "XYfit.csv"  # source file
  
          # Dataset parameters
          self.seq_params = {'dim': len(self.varlist),
                             'batch_size': self.batch_size,
                             'shuffle': True,
                             'filepath': self.sourcedatafile}
  
          # Dataset from CSV file
          self.dataset = datamanagement.CSVDataSet(**self.seq_params).finaldataset
  
          # Iterator on the CSV file
          self.dataiterator = self.dataset.make_initializable_iterator()
  
          # Optimizer
          self.optim = tf.train.RMSPropOptimizer(learning_rate=self.learnrate)
  
          # Official creation of the graph
          self.graph = tf.get_default_graph()
          with self.graph.as_default():
              # Writer creation
              self.writer = tf.contrib.summary.create_file_writer('./summary')
              with self.writer.as_default():
                  tf.contrib.summary.always_record_summaries()
  
      def mymodel(self, Zinp, reuse=False):
          # This function builds the graph of the network
          with tf.variable_scope("mymod/net", reuse=reuse):
              h1 = tf.layers.dense(Zinp, self.hsize[0], activation=tf.nn.leaky_relu, name='h1')
              h2 = tf.layers.dense(h1, self.hsize[1], activation=tf.nn.leaky_relu, name='h2')
              out = tf.layers.dense(h2, len(self.varlist), activation=None, name='final')       # None means linear activation
  
          return out
  
      def _trainepoch(self, ind):
          with self.writer.as_default():
              # Real data tensor from CSV file
              self.realdata = self.dataiterator.get_next()
  
              # random input vector
              self.Znoise = tf.random_uniform([self.batch_size, len(self.varlist)], minval=-1., maxval=1.)
  
              # Model and output tensor
              self.output = self.mymodel(self.Znoise, reuse=tf.AUTO_REUSE)
  
              # Loss
              self.loss = tf.losses.mean_squared_error(self.realdata, self.output)
  
              tf.contrib.summary.scalar("loss", self.loss)
  
              #Trainable variables
              t_vars = tf.trainable_variables()
  
              # Evaluation of the weight gradients
              grad = self.optim.compute_gradients(self.loss, var_list=t_vars)
  
              # Update weights based on gradients
              return self.optim.apply_gradients(grad), tf.contrib.summary.all_summary_ops()
  
      def _train_buildgraph(self):
          def body(ind, ops):
              train_up, ops = self._trainepoch(ind)
  
              # Ensure that the update is applied before continuing.
              with tf.control_dependencies([train_up]):
                  ind = ind + 1
                  return ind, ops
  
          def cond(ind, ops):
              return ind &lt; self.tr_mainsteps
  
          return tf.while_loop(cond, body, [tf.constant(0), [tf.Variable(False)]])
  
      def config_run(self, trepoch=50, testNet=False):
          self.tr_mainsteps = trepoch  # Number of adversarial training epoch
  
          with self.graph.as_default():
              with self.writer.as_default():
                  tr_loop, summary_ops = self._train_buildgraph()
  
          # Graph execution
          with self.graph.as_default():
              with self.writer.as_default():
                  with tf.Session() as sess:
                      sess.run(tf.initializers.global_variables())
                      sess.run(self.dataiterator.initializer)
  
                      tf.contrib.summary.initialize(
                          graph=tf.get_default_graph()
                      )
  
                      sess.run([summary_ops, tr_loop, summary_ops])
  
  def main(argv):
      hmodel = myNet()
      hmodel.config_run()
  
  
  if __name__ == "__main__":
      main(sys.argv[1:])
  </code></pre>
  
  <p>Can someone help me?</p>