【问题标题】:How to window or reset streaming operations in tensorflow?如何在张量流中窗口化或重置流操作?
【发布时间】:2018-10-12 17:23:10
【问题描述】:

Tensorflow 提供了各种不错的流式操作来按批次聚合统计信息,例如 tf.metrics.mean

但是我发现从一开始就累积所有值通常没有多大意义。例如,人们可能更希望获得每个 epoch 的统计信息,或者在给定上下文中有意义的任何其他时间窗口。

是否有任何方法可以限制此类流式统计的历史记录,例如通过重置流式操作以使它们从累积开始?

解决方法:

  • 跨批次手动累积
  • 使用 EMA 使用“软”滑动窗口

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    一种方法是在流式操作中调用相关变量的初始化程序。例如,

    import tensorflow as tf
    
    x = tf.random_normal(())
    mean_x, update_op = tf.metrics.mean(x, name='mean_x')
    # get the initializers of the local variables (total and count)
    my_metric_variables = [v for v in tf.local_variables() if v.name.startswith('mean_x/')]
    # or maybe just
    # my_metric_variables = tf.get_collection('metric_variables')
    reset_ops = [v.initializer for v in my_metric_variables]
    
    with tf.Session() as sess:
      tf.local_variables_initializer().run()
      for _ in range(100):
        for _ in range(100):
          sess.run(update_op)
        print(sess.run(mean_x))
        # if you comment the following out, the estimate of the mean converges to 0
        sess.run(reset_ops)
    

    【讨论】:

      【解决方案2】:

      tf.contrib.eager.metrics 中的指标(无论是否使用 Eager Execution)都有一个 init_variable() 操作,如果您想重置其内部变量,可以调用。

      【讨论】:

        猜你喜欢
        • 2017-11-26
        • 2017-01-29
        • 2017-08-06
        • 1970-01-01
        • 2018-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多