Memory Leak Eval Metrics Host Call Function in TensorFlow Custom Estimator












0















I am using the following function to calculate extra metrics for my training. I create a host_call with: host_call = (host_call_fn, metric_args) and pass it to host_call argument of estimator. However, calling this leads to memory leak and I can not figure out what is the problem. Using heap, it seems large dictionaries are being made somehow and they are not released.



p_temp = tf.reshape(policy_loss, [1], name='policy_loss_reshape')
v_temp = tf.reshape(value_loss, [1], name='value_loss_reshape')
e_temp = tf.reshape(entropy_loss, [1], name='entropy_loss_reshape')
t_temp = tf.reshape(total_loss, [1], name='total_loss_reshape')
g_temp = tf.reshape(global_step, [1], name='global_step_reshape')
#
metric_args = [p_temp, v_temp, e_temp, t_temp, g_temp]

host_call_fn = functools.partial(
eval_metrics_host_call_fn, est_mode=tf.estimator.ModeKeys.TRAIN)
host_call = (host_call_fn, metric_args)


The following function calculates the extra evaluation metrics and writes it to summary directory for Tensorboard.



def eval_metrics_host_call_fn(p_temp,
v_temp,
e_temp,
t_temp,
step,
est_mode=tf.estimator.ModeKeys.TRAIN):
#
with tf.variable_scope('metrics'):
metric_ops = {
'policy_loss': tf.metrics.mean(p_temp, name='policy_loss_metric'),
'value_loss': tf.metrics.mean(v_temp, name='value_loss_metric'),
'entropy_loss': tf.metrics.mean(e_temp, name='entropy_loss_metric'),
'total_loss': tf.metrics.mean(t_temp, name='total_loss_metric')
}
if est_mode == tf.estimator.ModeKeys.EVAL:
return metric_ops
eval_step = tf.reduce_min(step)
# Create summary ops so that they show up in SUMMARIES collection
# That way, they get logged automatically during training
summary_writer = summary.create_file_writer(FLAGS.summary_dir)
with summary_writer.as_default(
), summary.record_summaries_every_n_global_steps(FLAGS.summary_steps,
eval_step):
for metric_name, metric_op in metric_ops.items():
summary.scalar(metric_name, metric_op[1], step=eval_step)
# Reset metrics occasionally so that they are mean of recent batches.
reset_op = tf.variables_initializer(tf.local_variables('metrics'))
cond_reset_op = tf.cond(
tf.equal(eval_step % FLAGS.summary_steps, tf.to_int64(1)),
lambda: reset_op, lambda: tf.no_op())

return summary.all_summary_ops() + [cond_reset_op]









share|improve this question



























    0















    I am using the following function to calculate extra metrics for my training. I create a host_call with: host_call = (host_call_fn, metric_args) and pass it to host_call argument of estimator. However, calling this leads to memory leak and I can not figure out what is the problem. Using heap, it seems large dictionaries are being made somehow and they are not released.



    p_temp = tf.reshape(policy_loss, [1], name='policy_loss_reshape')
    v_temp = tf.reshape(value_loss, [1], name='value_loss_reshape')
    e_temp = tf.reshape(entropy_loss, [1], name='entropy_loss_reshape')
    t_temp = tf.reshape(total_loss, [1], name='total_loss_reshape')
    g_temp = tf.reshape(global_step, [1], name='global_step_reshape')
    #
    metric_args = [p_temp, v_temp, e_temp, t_temp, g_temp]

    host_call_fn = functools.partial(
    eval_metrics_host_call_fn, est_mode=tf.estimator.ModeKeys.TRAIN)
    host_call = (host_call_fn, metric_args)


    The following function calculates the extra evaluation metrics and writes it to summary directory for Tensorboard.



    def eval_metrics_host_call_fn(p_temp,
    v_temp,
    e_temp,
    t_temp,
    step,
    est_mode=tf.estimator.ModeKeys.TRAIN):
    #
    with tf.variable_scope('metrics'):
    metric_ops = {
    'policy_loss': tf.metrics.mean(p_temp, name='policy_loss_metric'),
    'value_loss': tf.metrics.mean(v_temp, name='value_loss_metric'),
    'entropy_loss': tf.metrics.mean(e_temp, name='entropy_loss_metric'),
    'total_loss': tf.metrics.mean(t_temp, name='total_loss_metric')
    }
    if est_mode == tf.estimator.ModeKeys.EVAL:
    return metric_ops
    eval_step = tf.reduce_min(step)
    # Create summary ops so that they show up in SUMMARIES collection
    # That way, they get logged automatically during training
    summary_writer = summary.create_file_writer(FLAGS.summary_dir)
    with summary_writer.as_default(
    ), summary.record_summaries_every_n_global_steps(FLAGS.summary_steps,
    eval_step):
    for metric_name, metric_op in metric_ops.items():
    summary.scalar(metric_name, metric_op[1], step=eval_step)
    # Reset metrics occasionally so that they are mean of recent batches.
    reset_op = tf.variables_initializer(tf.local_variables('metrics'))
    cond_reset_op = tf.cond(
    tf.equal(eval_step % FLAGS.summary_steps, tf.to_int64(1)),
    lambda: reset_op, lambda: tf.no_op())

    return summary.all_summary_ops() + [cond_reset_op]









    share|improve this question

























      0












      0








      0








      I am using the following function to calculate extra metrics for my training. I create a host_call with: host_call = (host_call_fn, metric_args) and pass it to host_call argument of estimator. However, calling this leads to memory leak and I can not figure out what is the problem. Using heap, it seems large dictionaries are being made somehow and they are not released.



      p_temp = tf.reshape(policy_loss, [1], name='policy_loss_reshape')
      v_temp = tf.reshape(value_loss, [1], name='value_loss_reshape')
      e_temp = tf.reshape(entropy_loss, [1], name='entropy_loss_reshape')
      t_temp = tf.reshape(total_loss, [1], name='total_loss_reshape')
      g_temp = tf.reshape(global_step, [1], name='global_step_reshape')
      #
      metric_args = [p_temp, v_temp, e_temp, t_temp, g_temp]

      host_call_fn = functools.partial(
      eval_metrics_host_call_fn, est_mode=tf.estimator.ModeKeys.TRAIN)
      host_call = (host_call_fn, metric_args)


      The following function calculates the extra evaluation metrics and writes it to summary directory for Tensorboard.



      def eval_metrics_host_call_fn(p_temp,
      v_temp,
      e_temp,
      t_temp,
      step,
      est_mode=tf.estimator.ModeKeys.TRAIN):
      #
      with tf.variable_scope('metrics'):
      metric_ops = {
      'policy_loss': tf.metrics.mean(p_temp, name='policy_loss_metric'),
      'value_loss': tf.metrics.mean(v_temp, name='value_loss_metric'),
      'entropy_loss': tf.metrics.mean(e_temp, name='entropy_loss_metric'),
      'total_loss': tf.metrics.mean(t_temp, name='total_loss_metric')
      }
      if est_mode == tf.estimator.ModeKeys.EVAL:
      return metric_ops
      eval_step = tf.reduce_min(step)
      # Create summary ops so that they show up in SUMMARIES collection
      # That way, they get logged automatically during training
      summary_writer = summary.create_file_writer(FLAGS.summary_dir)
      with summary_writer.as_default(
      ), summary.record_summaries_every_n_global_steps(FLAGS.summary_steps,
      eval_step):
      for metric_name, metric_op in metric_ops.items():
      summary.scalar(metric_name, metric_op[1], step=eval_step)
      # Reset metrics occasionally so that they are mean of recent batches.
      reset_op = tf.variables_initializer(tf.local_variables('metrics'))
      cond_reset_op = tf.cond(
      tf.equal(eval_step % FLAGS.summary_steps, tf.to_int64(1)),
      lambda: reset_op, lambda: tf.no_op())

      return summary.all_summary_ops() + [cond_reset_op]









      share|improve this question














      I am using the following function to calculate extra metrics for my training. I create a host_call with: host_call = (host_call_fn, metric_args) and pass it to host_call argument of estimator. However, calling this leads to memory leak and I can not figure out what is the problem. Using heap, it seems large dictionaries are being made somehow and they are not released.



      p_temp = tf.reshape(policy_loss, [1], name='policy_loss_reshape')
      v_temp = tf.reshape(value_loss, [1], name='value_loss_reshape')
      e_temp = tf.reshape(entropy_loss, [1], name='entropy_loss_reshape')
      t_temp = tf.reshape(total_loss, [1], name='total_loss_reshape')
      g_temp = tf.reshape(global_step, [1], name='global_step_reshape')
      #
      metric_args = [p_temp, v_temp, e_temp, t_temp, g_temp]

      host_call_fn = functools.partial(
      eval_metrics_host_call_fn, est_mode=tf.estimator.ModeKeys.TRAIN)
      host_call = (host_call_fn, metric_args)


      The following function calculates the extra evaluation metrics and writes it to summary directory for Tensorboard.



      def eval_metrics_host_call_fn(p_temp,
      v_temp,
      e_temp,
      t_temp,
      step,
      est_mode=tf.estimator.ModeKeys.TRAIN):
      #
      with tf.variable_scope('metrics'):
      metric_ops = {
      'policy_loss': tf.metrics.mean(p_temp, name='policy_loss_metric'),
      'value_loss': tf.metrics.mean(v_temp, name='value_loss_metric'),
      'entropy_loss': tf.metrics.mean(e_temp, name='entropy_loss_metric'),
      'total_loss': tf.metrics.mean(t_temp, name='total_loss_metric')
      }
      if est_mode == tf.estimator.ModeKeys.EVAL:
      return metric_ops
      eval_step = tf.reduce_min(step)
      # Create summary ops so that they show up in SUMMARIES collection
      # That way, they get logged automatically during training
      summary_writer = summary.create_file_writer(FLAGS.summary_dir)
      with summary_writer.as_default(
      ), summary.record_summaries_every_n_global_steps(FLAGS.summary_steps,
      eval_step):
      for metric_name, metric_op in metric_ops.items():
      summary.scalar(metric_name, metric_op[1], step=eval_step)
      # Reset metrics occasionally so that they are mean of recent batches.
      reset_op = tf.variables_initializer(tf.local_variables('metrics'))
      cond_reset_op = tf.cond(
      tf.equal(eval_step % FLAGS.summary_steps, tf.to_int64(1)),
      lambda: reset_op, lambda: tf.no_op())

      return summary.all_summary_ops() + [cond_reset_op]






      tensorflow memory-leaks tensorboard






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 13:27









      AmirCAmirC

      129213




      129213
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432046%2fmemory-leak-eval-metrics-host-call-function-in-tensorflow-custom-estimator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432046%2fmemory-leak-eval-metrics-host-call-function-in-tensorflow-custom-estimator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'