title: Hyperparameter tuning using tensorboard.plugins.hparams api with custom loss function
body: <p>I am building a neural network with my own custom loss function (pretty long and complicated). My network is unsupervised so my input and expected output are identical and also at the moment I am using one single input (just trying to optimize the loss for a single input). </p>
  
  <p>I am trying to use tensorboard.plugins.hparams api for hyperparameter tuning and don't know how to incorporate my custom loss function there. I'm trying to follow the code suggested on the Tensorflow 2.0 <a href="https://www.tensorflow.org/tensorboard/r2/hyperparameter_tuning_with_hparams" rel="nofollow noreferrer">website</a>.</p>
  
  <p>This is what the website suggests:</p>
  
  <pre class="lang-py prettyprint-override"><code>    HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([16, 32]))
      HP_DROPOUT = hp.HParam('dropout', hp.RealInterval(0.1, 0.2))
      HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd']))
  
      METRIC_ACCURACY = 'accuracy'
  
      with tf.summary.create_file_writer('logs/hparam_tuning').as_default():
        hp.hparams_config(
          hparams=[HP_NUM_UNITS, HP_DROPOUT, HP_OPTIMIZER],
          metrics=[hp.Metric(METRIC_ACCURACY, display_name='Accuracy')],
            )
  </code></pre>
  
  <p>I need to change that as I don't want to use the dropout layer, so I can just delete that. In terms of the METRIC_ACCURACY, I don't want to use accuracy as that has no use in my model but rather use my custom loss function. If I were to do the regular fit model it would look like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>    model.compile(optimizer=adam,loss=dl_tf_loss, metrics=[dl_tf_loss])
  </code></pre>
  
  <p>So I tried to change the suggested code into the following code but I get an error and am wondering how I should change it so that it suits my needs. Here is what I tried:</p>
  
  <pre class="lang-py prettyprint-override"><code>    HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([16, 32]))
      HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd']))
  
      #METRIC_LOSS = dl_tf_loss
  
      with tf.summary.create_file_writer('logs/hparam_tuning').as_default():
        hp.hparams_config(hparams=[HP_NUM_UNITS, HP_OPTIMIZER],metrics= 
         [hp.Metric(dl_tf_loss, display_name='Loss')])
  </code></pre>
  
  <p>It gives me the following error:</p>
  
  <pre class="lang-py prettyprint-override"><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-26-27d079c6be49&gt; in &lt;module&gt;()
        5 
        6 with tf.summary.create_file_writer('logs/hparam_tuning').as_default():
  ----&gt; 7   hp.hparams_config(hparams=[HP_NUM_UNITS, HP_OPTIMIZER],metrics=[hp.Metric(dl_tf_loss, display_name='Loss')])
        8 
  
  3 frames
  /usr/local/lib/python3.6/dist-packages/tensorboard/plugins/hparams/summary_v2.py in hparams_config(hparams, metrics, time_created_secs)
      127       hparams=hparams,
      128       metrics=metrics,
  --&gt; 129       time_created_secs=time_created_secs,
      130   )
      131   return _write_summary("hparams_config", pb)
  
  /usr/local/lib/python3.6/dist-packages/tensorboard/plugins/hparams/summary_v2.py in hparams_config_pb(hparams, metrics, time_created_secs)
      161       domain.update_hparam_info(info)
      162     hparam_infos.append(info)
  --&gt; 163   metric_infos = [metric.as_proto() for metric in metrics]
      164   experiment = api_pb2.Experiment(
      165       hparam_infos=hparam_infos,
  
  /usr/local/lib/python3.6/dist-packages/tensorboard/plugins/hparams/summary_v2.py in &lt;listcomp&gt;(.0)
      161       domain.update_hparam_info(info)
      162     hparam_infos.append(info)
  --&gt; 163   metric_infos = [metric.as_proto() for metric in metrics]
      164   experiment = api_pb2.Experiment(
      165       hparam_infos=hparam_infos,
  
  /usr/local/lib/python3.6/dist-packages/tensorboard/plugins/hparams/summary_v2.py in as_proto(self)
      532         name=api_pb2.MetricName(
      533             group=self._group,
  --&gt; 534             tag=self._tag,
      535         ),
      536         display_name=self._display_name,
  
  TypeError: &lt;tensorflow.python.eager.def_function.Function object at 0x7f9f3a78e5c0&gt; has type Function, but expected one of: bytes, unicode
  </code></pre>
  
  <p>I also tried running the following code:</p>
  
  <pre class="lang-py prettyprint-override"><code>    with tf.summary.create_file_writer('logs/hparam_tuning').as_default():
        hp.hparams_config(hparams=[HP_NUM_UNITS, HP_OPTIMIZER],metrics= 
        [dl_tf_loss])
  </code></pre>
  
  <p>but got the following error:</p>
  
  <pre class="lang-py prettyprint-override"><code>AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-28-6778bdf7f1b1&gt; in &lt;module&gt;()
        8 
        9 with tf.summary.create_file_writer('logs/hparam_tuning').as_default():
  ---&gt; 10   hp.hparams_config(hparams=[HP_NUM_UNITS, HP_OPTIMIZER],metrics=[dl_tf_loss])
  
  2 frames
  /usr/local/lib/python3.6/dist-packages/tensorboard/plugins/hparams/summary_v2.py in &lt;listcomp&gt;(.0)
      161       domain.update_hparam_info(info)
      162     hparam_infos.append(info)
  --&gt; 163   metric_infos = [metric.as_proto() for metric in metrics]
      164   experiment = api_pb2.Experiment(
      165       hparam_infos=hparam_infos,
  
  AttributeError: 'Function' object has no attribute 'as_proto'
  </code></pre>
  
  <p>Would greatly appreciate any help.
  Thanks in advance!</p>