title: How to write to TensorBoard in TensorFlow 2
body: <p>I'm quite familiar in TensorFlow 1.x and I'm considering to switch to TensorFlow 2 for an upcoming project. I'm having some trouble understanding <strong>how to write scalars to TensorBoard logs with eager execution</strong>, using a <a href="https://www.tensorflow.org/beta/tutorials/eager/custom_training_walkthrough" rel="noreferrer">custom training loop</a>.</p>
  
  <h3>Problem description</h3>
  
  <p>In tf1 you would create some summary ops (one op for each thing you would want to store), which you would then merge into a single op, run that merged op inside a session and then write this to a file using a FileWriter object. Assuming <code>sess</code> is our <code>tf.Session()</code>, an example of how this worked can be seen below:</p>
  
  <pre class="lang-py prettyprint-override"><code># While defining our computation graph, define summary ops:
  # ... some ops ...
  tf.summary.scalar('scalar_1', scalar_1)
  # ... some more ops ...
  tf.summary.scalar('scalar_2', scalar_2)
  # ... etc.
  
  # Merge all these summaries into a single op:
  merged = tf.summary.merge_all()
  
  # Define a FileWriter (i.e. an object that writes summaries to files):
  writer = tf.summary.FileWriter(log_dir, sess.graph)
  
  # Inside the training loop run the op and write the results to a file:
  for i in range(num_iters):
      summary, ... = sess.run([merged, ...], ...)
      writer.add_summary(summary, i)
  </code></pre>
  
  <p>The problem is that sessions don't exist anymore in tf2 and I would prefer not disabling eager execution to make this work. The <a href="https://www.tensorflow.org/guide/summaries_and_tensorboard" rel="noreferrer">official documentation</a> is written for tf1 and all references I can find suggest using the Tensorboard keras callback. However, as far as I know, this only works if you train the model through <code>model.fit(...)</code> and not through a <a href="https://www.tensorflow.org/beta/tutorials/eager/custom_training_walkthrough" rel="noreferrer">custom training loop</a>.</p>
  
  <h3>What I've tried</h3>
  
  <ul>
  <li>The tf1 version of <code>tf.summary</code> functions, outside of a session. Obviously any combination of these functions fails, as FileWriters, merge_ops, etc. don't even exist in tf2.</li>
  <li><a href="https://medium.com/tensorflow/effective-tensorflow-2-0-best-practices-and-whats-changed-a0ca48767aff" rel="noreferrer">This medium post</a> states that there has been a "cleanup" in some tensorflow APIs including <code>tf.summary()</code>. They suggest using <code>from tensorflow.python.ops.summary_ops_v2</code>, which doesn't seem to work. This <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/summary_ops_v2.py#L125" rel="noreferrer">implies</a> using a <code>record_summaries_every_n_global_steps</code>; more on this later.</li>
  <li>A series of other posts <a href="https://medium.com/coinmonks/8-things-to-do-differently-in-tensorflows-eager-execution-mode-47cf429aa3ad" rel="noreferrer">1</a>, <a href="https://stackoverflow.com/questions/49711324/tf-contrib-summary-generic-or-tf-summary-text-in-eager-mode">2</a>, <a href="https://stackoverflow.com/questions/50257614/tensorflow-eager-and-tensorboard-graphs">3</a>, suggest using the <code>tf.contrib.summary</code> and <code>tf.contrib.FileWriter</code>. However, <code>tf.contrib</code> <a href="https://github.com/tensorflow/community/pull/18" rel="noreferrer">has been removed from the core TensorFlow repository and build process</a>. </li>
  <li><a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tf2_showcase/mnist.py" rel="noreferrer">A TensorFlow v2 showcase from the official repo</a>, which again uses the <code>tf.contrib</code> summaries along with the <code>record_summaries_every_n_global_steps</code> mentioned previously. I couldn't make this to work either (even without using the contrib library).</li>
  </ul>
  
  <h3>tl;dr</h3>
  
  <p>My questions are:</p>
  
  <ul>
  <li>Is there a way to properly use <code>tf.summary</code> in TensroFlow 2?</li>
  <li>If not, is there another way to write TensorBoard logs in TensorFlow 2, when using a custom training loop (not <code>model.fit()</code>)?</li>
  </ul>